Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / themes / contrib / bootstrap / src / Plugin / Setting / Advanced / Cdn / CdnJsdelivrVersion.php
1 <?php
2
3 namespace Drupal\bootstrap\Plugin\Setting\Advanced\Cdn;
4
5 use Drupal\bootstrap\Bootstrap;
6 use Drupal\bootstrap\Utility\Element;
7 use Drupal\Component\Utility\Html;
8 use Drupal\Core\Form\FormStateInterface;
9
10 /**
11  * The "cdn_jsdelivr_version" theme setting.
12  *
13  * @ingroup plugins_setting
14  *
15  * @BootstrapSetting(
16  *   cdn_provider = "jsdelivr",
17  *   id = "cdn_jsdelivr_version",
18  *   type = "select",
19  *   weight = -1,
20  *   title = @Translation("Version"),
21  *   description = @Translation("Choose the Bootstrap version from jsdelivr"),
22  *   defaultValue = @BootstrapConstant("Drupal\bootstrap\Bootstrap::FRAMEWORK_VERSION"),
23  *   groups = {
24  *     "advanced" = @Translation("Advanced"),
25  *     "cdn" = @Translation("CDN (Content Delivery Network)"),
26  *     "jsdelivr" = false,
27  *   },
28  * )
29  */
30 class CdnJsdelivrVersion extends CdnProvider {
31
32   /**
33    * {@inheritdoc}
34    */
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);
38
39     $plugin_id = Html::cleanCssIdentifier($this->provider->getPluginId());
40     $setting = $this->getSettingElement($form, $form_state);
41
42     $setting->setProperty('options', $this->provider->getVersions());
43     $setting->setProperty('ajax', [
44       'callback' => [get_class($this), 'ajaxCallback'],
45       'wrapper' => 'cdn-provider-' . $plugin_id,
46     ]);
47
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(),
51       ]));
52     }
53   }
54
55   /**
56    * AJAX callback for reloading CDN provider elements.
57    *
58    * @param array $form
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.
62    */
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'))];
65   }
66
67 }