Version 1
[yaffs-website] / web / themes / contrib / bootstrap / src / Plugin / Setting / Advanced / Cdn / CdnJsdelivrVersion.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\bootstrap\Plugin\Setting\Advanced\Cdn\CdnJsdelivrVersion.
5  */
6
7 namespace Drupal\bootstrap\Plugin\Setting\Advanced\Cdn;
8
9 use Drupal\bootstrap\Annotation\BootstrapConstant;
10 use Drupal\bootstrap\Annotation\BootstrapSetting;
11 use Drupal\bootstrap\Bootstrap;
12 use Drupal\bootstrap\Utility\Element;
13 use Drupal\Component\Utility\Html;
14 use Drupal\Core\Annotation\Translation;
15 use Drupal\Core\Form\FormStateInterface;
16
17 /**
18  * The "cdn_jsdelivr_version" theme setting.
19  *
20  * @ingroup plugins_setting
21  *
22  * @BootstrapSetting(
23  *   cdn_provider = "jsdelivr",
24  *   id = "cdn_jsdelivr_version",
25  *   type = "select",
26  *   weight = -1,
27  *   title = @Translation("Version"),
28  *   description = @Translation("Choose the Bootstrap version from jsdelivr"),
29  *   defaultValue = @BootstrapConstant("Drupal\bootstrap\Bootstrap::FRAMEWORK_VERSION"),
30  *   groups = {
31  *     "advanced" = @Translation("Advanced"),
32  *     "cdn" = @Translation("CDN (Content Delivery Network)"),
33  *     "jsdelivr" = false,
34  *   },
35  * )
36  */
37 class CdnJsdelivrVersion extends CdnProvider {
38
39   /**
40    * {@inheritdoc}
41    */
42   public function alterFormElement(Element $form, FormStateInterface $form_state, $form_id = NULL) {
43     $plugin_id = Html::cleanCssIdentifier($this->provider->getPluginId());
44     $setting = $this->getSettingElement($form, $form_state);
45
46     $setting->setProperty('options', $this->provider->getVersions());
47     $setting->setProperty('ajax', [
48       'callback' => [get_class($this), 'ajaxCallback'],
49       'wrapper' => 'cdn-provider-' . $plugin_id,
50     ]);
51
52     if (!$this->provider->hasError() && !$this->provider->isImported()) {
53       $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.', [
54         '@provider' => $this->provider->getLabel(),
55       ]));
56     }
57   }
58
59   /**
60    * AJAX callback for reloading CDN provider elements.
61    *
62    * @param array $form
63    *   Nested array of form elements that comprise the form.
64    * @param \Drupal\Core\Form\FormStateInterface $form_state
65    *   The current state of the form.
66    */
67   public static function ajaxCallback(array $form, FormStateInterface $form_state) {
68     return $form['advanced']['cdn'][$form_state->getValue('cdn_provider', Bootstrap::getTheme()->getSetting('cdn_provider'))];
69   }
70
71 }