file CdnProvider.php

Namespace

Drupal\bootstrap\Plugin\Setting\Advanced\Cdn
  1. <?php
  2. namespace Drupal\bootstrap\Plugin\Setting\Advanced\Cdn;
  3. /**
  4. * Due to BC reasons, this class cannot be moved.
  5. *
  6. * @todo Move namespace up one.
  7. */
  8. use Drupal\bootstrap\Bootstrap;
  9. use Drupal\bootstrap\Plugin\Form\FormInterface;
  10. use Drupal\bootstrap\Plugin\Form\SystemThemeSettings;
  11. use Drupal\bootstrap\Plugin\Provider\Broken;
  12. use Drupal\bootstrap\Plugin\Provider\ProviderInterface;
  13. use Drupal\bootstrap\Plugin\ProviderManager;
  14. use Drupal\bootstrap\Utility\Element;
  15. use Drupal\Component\Utility\Html;
  16. use Drupal\Core\Access\AccessResultAllowed;
  17. use Drupal\Core\Form\FormStateInterface;
  18. use Drupal\Core\Link;
  19. use Drupal\Core\Render\Markup;
  20. use Drupal\Core\Url;
  21. /**
  22. * The "cdn_provider" theme setting.
  23. *
  24. * @ingroup plugins_provider
  25. * @ingroup plugins_setting
  26. *
  27. * @BootstrapSetting(
  28. * id = "cdn_provider",
  29. * type = "select",
  30. * title = @Translation("CDN Provider"),
  31. * description = @Translation("Choose the CDN Provider used to load Bootstrap resources."),
  32. * defaultValue = "jsdelivr",
  33. * empty_option = @Translation("None (compile locally)"),
  34. * empty_value = "",
  35. * weight = -1,
  36. * groups = {
  37. * "cdn" = @Translation("CDN (Content Delivery Network)"),
  38. * "cdn_provider" = false,
  39. * },
  40. * options = { },
  41. * )
  42. */
  43. class CdnProvider extends CdnProviderBase {
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function alterForm(array &$form, FormStateInterface $form_state, $form_id = NULL) {
  48. parent::alterForm($form, $form_state, $form_id);
  49. // Allow the provider to participate.
  50. if ($this->provider instanceof FormInterface) {
  51. $this->provider->alterForm($form, $form_state);
  52. }
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function alterFormElement(Element $form, FormStateInterface $form_state, $form_id = NULL) {
  58. // Wrap the default group so it can be replaced via AJAX.
  59. $group = $this->getGroupElement($form, $form_state);
  60. $group->setProperty('prefix', '<div id="cdn-providers">');
  61. $group->setProperty('suffix', '</div>');
  62. // Set available CDN Providers.
  63. $setting = $this->setCdnProvidersAjax($this->getSettingElement($form, $form_state));
  64. $setting->setProperty('options', array_map(function (ProviderInterface $provider) {
  65. return $provider->getLabel();
  66. }, $this->theme->getCdnProviders()));
  67. // Add the CDN Provider description.
  68. $provider = $this->getProvider();
  69. $description = $provider->getDescription();
  70. $group->description = [
  71. '#access' => AccessResultAllowed::allowedIf(!empty((string) $description) && !($provider instanceof Broken)),
  72. '#type' => 'container',
  73. '#theme_wrappers' => ['container__help_block'],
  74. 0 => ['#markup' => $description],
  75. ];
  76. // Add CDN Provider cache reset functionality.
  77. $group->cache = [
  78. '#access' => AccessResultAllowed::allowedIf(!($provider instanceof Broken)),
  79. '#type' => 'details',
  80. '#title' => $this->t('Advanced Cache'),
  81. '#description' => $this->t('All @provider data is intelligently and automatically cached using the various settings below. This allows the @provider data to persist through cache rebuilds. This data will invalidate and rebuild automatically, however a manual reset can be invoked below.', [
  82. '@provider' => $provider->getPluginId() === 'custom' ? $this->t('CDN Provider') : $provider->getLabel(),
  83. ]),
  84. '#weight' => 1000,
  85. ];
  86. $ttl_settings = [
  87. 'cdn_cache_ttl_versions',
  88. 'cdn_cache_ttl_themes',
  89. 'cdn_cache_ttl_assets',
  90. 'cdn_cache_ttl_library',
  91. ];
  92. // Because these settings are used for all providers, any current value set
  93. // in the input array is a result of a provider switch via AJAX. Go ahead
  94. // and unset the value from the current form state and then add the setting
  95. // to the form.
  96. $input = $form_state->getUserInput();
  97. $values = $form_state->getValues();
  98. foreach ($ttl_settings as $ttl_setting) {
  99. if (!empty($input['_triggering_element_name']) && $input['_triggering_element_name'] === 'cdn_provider') {
  100. unset($input[$ttl_setting], $values[$ttl_setting]);
  101. }
  102. $this->theme->getSettingPlugin($ttl_setting)->alterForm($form->getArray(), $form_state);
  103. }
  104. $form_state->setUserInput($input);
  105. $form_state->setValues($values);
  106. $group->cache->reset = $this->setCdnProvidersAjax(Element::createStandalone([
  107. '#weight' => 100,
  108. '#type' => 'submit',
  109. '#description' => $this->t('Note: this will not reset any cached HTTP requests; see the "Advanced" section.'),
  110. '#value' => $this->t('Reset @provider Cache', [
  111. '@provider' => $provider->getLabel(),
  112. ]),
  113. '#submit' => [
  114. [get_class($this), 'submitResetProviderCache'],
  115. ],
  116. ]));
  117. // Intercept possible manual import of API data via AJAX callback.
  118. // @todo Import functionality is deprecated, remove in a future release.
  119. $this->importProviderData($group, $form_state);
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public static function submitForm(array &$form, FormStateInterface $form_state) {
  125. parent::submitForm($form, $form_state);
  126. $theme = SystemThemeSettings::getTheme(Element::create($form), $form_state);
  127. $provider = ProviderManager::load($theme, $form_state->getValue('cdn_provider'));
  128. if ($provider instanceof FormInterface) {
  129. $provider->submitForm($form, $form_state);
  130. }
  131. }
  132. /**
  133. * Submit callback for resetting CDN Provider cache.
  134. *
  135. * @param array $form
  136. * Nested array of form elements that comprise the form.
  137. * @param \Drupal\Core\Form\FormStateInterface $form_state
  138. * The current state of the form.
  139. */
  140. public static function submitResetProviderCache(array $form, FormStateInterface $form_state) {
  141. $form_state->setRebuild();
  142. $theme = SystemThemeSettings::getTheme(Element::create($form), $form_state);
  143. $provider = ProviderManager::load($theme, $form_state->getValue('cdn_provider', $theme->getSetting('cdn_provider')));
  144. $provider->resetCache();
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public static function validateFormElement(Element $form, FormStateInterface $form_state) {
  150. parent::validateFormElement($form, $form_state);
  151. $theme = SystemThemeSettings::getTheme($form, $form_state);
  152. $provider = ProviderManager::load($theme, $form_state->getValue('cdn_provider'));
  153. // Validate the provider.
  154. if (!($provider instanceof Broken)) {
  155. $cdnVersion = $form_state->getValue('cdn_version', $theme->getSetting('cdn_version', Bootstrap::FRAMEWORK_VERSION));
  156. $cdnTheme = $form_state->getValue('cdn_theme', $theme->getSetting('cdn_theme'));
  157. $assets = $provider->getCdnAssets($cdnVersion, $cdnTheme);
  158. // Now validate that each asset is reachable.
  159. $unreachable = [];
  160. foreach ($assets->toArray() as $asset) {
  161. $url = $asset->getUrl();
  162. if (!Bootstrap::checkUrlIsReachable($url)) {
  163. $unreachable[] = Link::fromTextAndUrl($url, Url::fromUri($url)->setOption('attributes', ['target' => '_blank']));
  164. }
  165. }
  166. if ($unreachable) {
  167. $form_state->setErrorByName('cdn_provider', t('Unable to reach the following @provider assets: <ul><li>@unreachable</li>', [
  168. '@provider' => $provider->getLabel(),
  169. '@unreachable' => Markup::create(implode('</li><li>', $unreachable)),
  170. ]));
  171. return;
  172. }
  173. // Check for exceptions (API HTTP request errors).
  174. if (static::checkCdnExceptions($provider)) {
  175. $form_state->setErrorByName('cdn_provider', t('Unable to use @provider assets. Please choose a different CDN Provider.', [
  176. '@provider' => $provider->getLabel(),
  177. ]));
  178. return;
  179. }
  180. }
  181. if ($provider instanceof FormInterface) {
  182. $provider->validateFormElement($form, $form_state);
  183. }
  184. }
  185. /**
  186. * Imports data for a provider that was manually uploaded in theme settings.
  187. *
  188. * @param \Drupal\bootstrap\Utility\Element $group
  189. * The setting group Element.
  190. * @param \Drupal\Core\Form\FormStateInterface $form_state
  191. * The current state of the form.
  192. *
  193. * @todo Import functionality is deprecated, remove in a future release.
  194. */
  195. protected function importProviderData(Element $group, FormStateInterface $form_state) {
  196. if ($form_state->getValue('clicked_button') === t('Save provider data')->render()) {
  197. $provider_path = ProviderManager::FILE_PATH;
  198. file_prepare_directory($provider_path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  199. $provider = $form_state->getValue('cdn_provider', $this->theme->getSetting('cdn_provider'));
  200. $file = "$provider_path/$provider.json";
  201. if ($import_data = $form_state->getValue('cdn_provider_import_data', FALSE)) {
  202. file_unmanaged_save_data($import_data, $file, FILE_EXISTS_REPLACE);
  203. }
  204. elseif ($file && file_exists($file)) {
  205. file_unmanaged_delete($file);
  206. }
  207. // Clear the cached definitions so they can get rebuilt.
  208. $providerManager = new ProviderManager($this->theme);
  209. $providerManager->clearCachedDefinitions();
  210. $form_state->setRebuild();
  211. return;
  212. }
  213. $provider = $this->getProvider();
  214. $plugin_id = Html::cleanCssIdentifier($provider->getPluginId());
  215. // To avoid triggering unnecessary deprecation messages, extract these
  216. // values from the provider definition directly.
  217. // @todo Import functionality is deprecated, remove in a future release.
  218. $definition = $provider->getPluginDefinition();
  219. $hasError = !empty($definition['error']);
  220. $isImported = !empty($definition['imported']);
  221. // Indicate there was an error retrieving the provider's API data.
  222. if ($hasError || $isImported) {
  223. if ($isImported) {
  224. Bootstrap::deprecated('\Drupal\bootstrap\Plugin\Provider\ProviderInterface::isImported');
  225. }
  226. if ($hasError) {
  227. // Now a deprecation message can be shown as the provider clearly is
  228. // using the outdated "process definition" method of providing assets.
  229. Bootstrap::deprecated('\Drupal\bootstrap\Plugin\Provider\ProviderInterface::hasError');
  230. $description_label = $this->t('ERROR');
  231. $description = $this->t('Unable to reach or parse the data provided by the @title API. Ensure the server this website is hosted on is able to initiate HTTP requests. If the request consistently fails, it is likely that there are certain PHP functions that have been disabled by the hosting provider for security reasons. It is possible to manually copy and paste the contents of the following URL into the "Imported @title data" section below.<br /><br /><a href=":provider_api" target="_blank">:provider_api</a>.', [
  232. '@title' => $provider->getLabel(),
  233. ':provider_api' => $provider->getApi(),
  234. ]);
  235. $group->error = [
  236. '#markup' => '<div class="alert alert-danger messages error"><strong>' . $description_label . ':</strong> ' . $description . '</div>',
  237. '#weight' => -20,
  238. ];
  239. }
  240. $group->import = [
  241. '#type' => 'details',
  242. '#title' => t('Imported @title data', ['@title' => $provider->getLabel()]),
  243. '#description' => t('The provider will attempt to parse the data entered here each time it is saved. If no data has been entered, any saved files associated with this provider will be removed and the provider will again attempt to request the API data normally through the following URL: <a href=":provider_api" target="_blank">:provider_api</a>.', [
  244. ':provider_api' => $provider->getPluginDefinition()['api'],
  245. ]),
  246. '#weight' => 10,
  247. '#open' => FALSE,
  248. ];
  249. $group->import->cdn_provider_import_data = [
  250. '#type' => 'textarea',
  251. '#default_value' => file_exists(ProviderManager::FILE_PATH . '/' . $plugin_id . '.json') ? file_get_contents(ProviderManager::FILE_PATH . '/' . $plugin_id . '.json') : NULL,
  252. ];
  253. $group->import->submit = $this->setCdnProvidersAjax([
  254. '#type' => 'submit',
  255. '#value' => t('Save provider data'),
  256. '#executes_submit_callback' => FALSE,
  257. ]);
  258. }
  259. }
  260. }

Classes

Name Description
CdnProvider The "cdn_provider" theme setting.