3 namespace Drupal\bootstrap\Plugin\Setting\Advanced\Cdn;
5 use Drupal\bootstrap\Bootstrap;
6 use Drupal\bootstrap\Utility\Element;
7 use Drupal\Component\Utility\Html;
8 use Drupal\Core\Form\FormStateInterface;
11 * The "cdn_jsdelivr_version" theme setting.
13 * @ingroup plugins_setting
16 * cdn_provider = "jsdelivr",
17 * id = "cdn_jsdelivr_version",
20 * title = @Translation("Version"),
21 * description = @Translation("Choose the Bootstrap version from jsdelivr"),
22 * defaultValue = @BootstrapConstant("Drupal\bootstrap\Bootstrap::FRAMEWORK_VERSION"),
24 * "advanced" = @Translation("Advanced"),
25 * "cdn" = @Translation("CDN (Content Delivery Network)"),
30 class CdnJsdelivrVersion extends CdnProvider {
35 public function alterFormElement(Element $form, FormStateInterface $form_state, $form_id = NULL) {
36 // Add autoload fix to make sure AJAX callbacks work.
37 static::formAutoloadFix($form_state);
39 $plugin_id = Html::cleanCssIdentifier($this->provider->getPluginId());
40 $setting = $this->getSettingElement($form, $form_state);
42 $setting->setProperty('options', $this->provider->getVersions());
43 $setting->setProperty('ajax', [
44 'callback' => [get_class($this), 'ajaxCallback'],
45 'wrapper' => 'cdn-provider-' . $plugin_id,
48 if (!$this->provider->hasError() && !$this->provider->isImported()) {
49 $setting->setProperty('description', t('These versions are automatically populated by the @provider API upon cache clear and newer versions may appear over time. It is highly recommended the version that the site was built with stays at that version. Until a newer version has been properly tested for updatability by the site maintainer, you should not arbitrarily "update" just because there is a newer version. This can cause many inconsistencies and undesired effects with an existing site.', [
50 '@provider' => $this->provider->getLabel(),
56 * AJAX callback for reloading CDN provider elements.
59 * Nested array of form elements that comprise the form.
60 * @param \Drupal\Core\Form\FormStateInterface $form_state
61 * The current state of the form.
63 public static function ajaxCallback(array $form, FormStateInterface $form_state) {
64 return $form['advanced']['cdn'][$form_state->getValue('cdn_provider', Bootstrap::getTheme()->getSetting('cdn_provider'))];