class CdnCustom

The "cdn_custom" theme setting.

Plugin annotation

@BootstrapSetting(
  id = "cdn_custom",
  type = "textarea",
  description = @Translation("One complete URL per line. All URLs are validated and parsed to determine available version(s) and/or theme(s). A URL can be any file ending in <code>.css</code> or <code>.js</code> (with matching response MIME type). Minified URLs can also be supplied and the will be used automatically."),
  groups = {
    "cdn" = @Translation("CDN (Content Delivery Network)"),
    "cdn_provider" = false,
    "custom" = @Translation("Custom URLs"),
  },
)

Hierarchy

Expanded class hierarchy of CdnCustom

Related topics

Namespace

Drupal\bootstrap\Plugin\Setting\Advanced\Cdn
Source
class CdnCustom extends CdnProviderBase {

  /**
   * {@inheritdoc}
   */
  public function access() {
    return parent::access()->andIf(AccessResultAllowed::allowedIf($this->getProvider()->getPluginId() === 'custom'));
  }

  /**
   * {@inheritdoc}
   */
  public function alterFormElement(Element $form, FormStateInterface $form_state, $form_id = NULL) {
    $group = $this->getGroupElement($form, $form_state);
    $group->setProperty('weight', 99);
    $group->access($this->access());

    $setting = $this->getSettingElement($form, $form_state);
    $setting->setProperty('smart_description', FALSE);

    $rows = count(array_filter(array_map('trim', preg_split("/\r\n|\n/", $form_state->getValue('cdn_custom', '')))));
    $setting->setProperty('rows', $rows > 20 ? 20 : $rows);

    $group->apply = $this->setCdnProvidersAjax(Element::createStandalone([
      '#weight' => 100,
      '#type' => 'submit',
      '#value' => $this->t('Apply'),
      '#submit' => [
        [get_class($this), 'submitApplyCss'],
      ],
    ]));
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultValue() {
    return implode("\n", [
      'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.css',
      'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css',
      'https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.js',
      'https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js',
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function processDeprecatedValues(array $values, array $deprecated) {
    // Merge the deprecated settings together to form a new line for each value.
    // @todo Remove deprecated setting support in a future release.
    return implode("\n", $values) ? : NULL;
  }

  /**
   * Submit callback for resetting CDN Provider cache.
   *
   * @param array $form
   *   Nested array of form elements that comprise the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public static function submitApplyCss(array $form, FormStateInterface $form_state) {
    $theme = SystemThemeSettings::getTheme(Element::create($form, $form_state), $form_state);
    $theme->setSetting('cdn_custom', $form_state->getValue('cdn_custom'));
    $form_state->setRebuild();
  }

  /**
   * {@inheritdoc}
   */
  public static function validateFormElement(Element $form, FormStateInterface $form_state) {
    // Immediately return if this isn't the currently selected CDN Provider.
    if ($form_state->getValue('cdn_provider') !== 'custom') {
      return;
    }

    $theme = SystemThemeSettings::getTheme($form, $form_state);

    /** @var \Drupal\bootstrap\Plugin\Provider\Custom $provider */
    $provider = ProviderManager::load($theme, 'custom');

    $urls = array_filter(array_map('trim', preg_split("/\r\n|\n/", $form_state->getValue('cdn_custom', ''))));

    $invalid = [];
    foreach ($urls as $url) {
      try {
        $provider->validateUrl($url);
      }
      catch (\Exception $e) {
        $invalid[] = $e->getMessage();
      }
    }
    if ($invalid) {
      $form_state->setErrorByName('cdn_custom', t('Invalid Custom URLs: <ul><li>@invalid</li></ul>', [
        '@invalid' => new FormattableMarkup(implode('</li><li>', $invalid), []),
      ]));

      // Unfortunately, any errors set during validation prevents the form
      // rebuilding functionality from working. This has to be changed here.
      $form->cdn->cdn_provider->custom->setProperty('open', TRUE);
    }
  }

}

Members

Contains filters are case sensitive
Name Modifiers Type Description
CdnCustom::access public function Indicates whether a setting is accessible. Overrides SettingBase::access
CdnCustom::alterFormElement public function The alter method to store the code. Overrides SettingBase::alterFormElement
CdnCustom::getDefaultValue public function Retrieves the setting's default value. Overrides SettingBase::getDefaultValue
CdnCustom::processDeprecatedValues public function Retrieves the value from other deprecated settings. Overrides SettingBase::processDeprecatedValues
CdnCustom::submitApplyCss public static function Submit callback for resetting CDN Provider cache.
CdnCustom::validateFormElement public static function Form validation handler. Overrides SettingBase::validateFormElement
CdnProviderBase::$provider protected property The active provider based on form value or theme setting.
CdnProviderBase::ajaxCdnProvidersCallback public static function AJAX callback for reloading CDN Providers.
CdnProviderBase::alterForm public function The alter method to store the code. Overrides SettingBase::alterForm
CdnProviderBase::checkCdnExceptions protected static function Handles any CDN Provider exceptions that may have been thrown.
CdnProviderBase::getCacheTags public function The cache tags associated with this object. Overrides SettingBase::getCacheTags
CdnProviderBase::getProvider protected function Retrieves the active CDN Provider.
CdnProviderBase::setCdnProvidersAjax protected function Sets the #ajax property to rebuild the entire CDN Providers container.
FormAutoloadFixTrait::formAutoloadFix public static function Adds the autoload fix include file to the form state.
PluginBase::$theme protected property The currently set theme object.
PluginBase::__construct public function
SettingBase::$autoUserInterface public static property
SettingBase::autoCreateFormElement public function Indicates whether a form element should be created automatically. Overrides SettingInterface::autoCreateFormElement
SettingBase::drupalSettings public function Determines whether a theme setting should added to drupalSettings. Overrides SettingInterface::drupalSettings
SettingBase::getDescription public function Retrieves the setting's description, if any. Overrides SettingInterface::getDescription
SettingBase::getElement Deprecated public function Overrides SettingInterface::getElement
SettingBase::getElementProperties public function Retrieves all the form properties from the setting definition.
SettingBase::getGroup Deprecated public function Overrides SettingInterface::getGroup
SettingBase::getGroupElement public function Retrieves the group form element the setting belongs to. Overrides SettingInterface::getGroupElement
SettingBase::getGroups public function Retrieves the setting's groups. Overrides SettingInterface::getGroups
SettingBase::getOptions public function Retrieves the settings options, if set. Overrides SettingInterface::getOptions
SettingBase::getSettingElement public function Retrieves the form element for the setting. Overrides SettingInterface::getSettingElement
SettingBase::getSettingValue protected function Retrieves the setting value used to populate the form.
SettingBase::getTitle public function Retrieves the setting's human-readable title. Overrides SettingInterface::getTitle
SettingBase::submitForm public static function Form submission handler. Overrides FormInterface::submitForm
SettingBase::submitFormElement public static function Form submission handler. Overrides FormInterface::submitFormElement
SettingBase::validateForm public static function Form validation handler. Overrides FormInterface::validateForm