file CdnProviderBase.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\Provider\ProviderInterface;
  10. use Drupal\bootstrap\Plugin\ProviderManager;
  11. use Drupal\bootstrap\Plugin\Setting\SettingBase;
  12. use Drupal\bootstrap\Traits\FormAutoloadFixTrait;
  13. use Drupal\bootstrap\Utility\Element;
  14. use Drupal\Component\Utility\Html;
  15. use Drupal\Core\Form\FormStateInterface;
  16. use Drupal\Core\Url;
  17. /**
  18. * A base class for CDN Provider settings.
  19. *
  20. * @ingroup plugins_provider
  21. * @ingroup plugins_setting
  22. */
  23. abstract class CdnProviderBase extends SettingBase {
  24. use FormAutoloadFixTrait;
  25. /**
  26. * The active provider based on form value or theme setting.
  27. *
  28. * @var \Drupal\bootstrap\Plugin\Provider\ProviderInterface
  29. */
  30. protected $provider;
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function alterForm(array &$form, FormStateInterface $form_state, $form_id = NULL) {
  35. // Add autoload fix to make sure AJAX callbacks work.
  36. static::formAutoloadFix($form_state);
  37. // Attempt to extract the active provider from submitted values. Note: in
  38. // some cases, it needs to be extracted from the raw input if the values
  39. // haven't yet been populated.
  40. $input = $form_state->getUserInput();
  41. $provider = $form_state->getValue('cdn_provider', isset($input['cdn_provider']) ? Html::escape($input['cdn_provider']) : NULL);
  42. $this->provider = ProviderManager::load($this->theme, $provider);
  43. // Invoke the original alter.
  44. parent::alterForm($form, $form_state, $form_id);
  45. }
  46. /**
  47. * Handles any CDN Provider exceptions that may have been thrown.
  48. *
  49. * @param \Drupal\bootstrap\Plugin\Provider\ProviderInterface $provider
  50. * A CDN Provider to check.
  51. * @param bool $reset
  52. * Flag indicating whether to remove the Exceptions once they have been
  53. * retrieved.
  54. *
  55. * @return bool
  56. * TRUE if there are exceptions, FALSE otherwise.
  57. */
  58. protected static function checkCdnExceptions(ProviderInterface $provider, $reset = TRUE) {
  59. $exceptions = $provider->getCdnExceptions($reset);
  60. if ($exceptions) {
  61. Bootstrap::message(t('Unable to parse @provider data. <a href=":logs">Check the logs for more details.</a> If your issues are network related, consider using the "custom" CDN Provider instead to statically set the URLs that should be used.', [
  62. ':logs' => Url::fromRoute('dblog.overview')->toString(),
  63. '@provider' => $provider->getLabel(),
  64. ]), 'error');
  65. foreach ($exceptions as $exception) {
  66. watchdog_exception('bootstrap', $exception);
  67. }
  68. }
  69. return !!$exceptions;
  70. }
  71. /**
  72. * AJAX callback for reloading CDN Providers.
  73. *
  74. * @param array $form
  75. * Nested array of form elements that comprise the form.
  76. * @param \Drupal\Core\Form\FormStateInterface $form_state
  77. * The current state of the form.
  78. *
  79. * @return array
  80. * The form element to render.
  81. */
  82. public static function ajaxCdnProvidersCallback(array $form, FormStateInterface $form_state) {
  83. return $form['cdn']['cdn_provider'];
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function getCacheTags() {
  89. return ['library_info'];
  90. }
  91. /**
  92. * Retrieves the active CDN Provider.
  93. *
  94. * @return \Drupal\bootstrap\Plugin\Provider\ProviderInterface
  95. * A CDN Provider.
  96. */
  97. protected function getProvider() {
  98. if (!isset($this->provider)) {
  99. $this->provider = $this->theme->getCdnProvider();
  100. }
  101. return $this->provider;
  102. }
  103. /**
  104. * Sets the #ajax property to rebuild the entire CDN Providers container.
  105. *
  106. * @param \Drupal\bootstrap\Utility\Element|array $element
  107. * An Element to modify.
  108. *
  109. * @return \Drupal\bootstrap\Utility\Element
  110. * The Element passed.
  111. */
  112. protected function setCdnProvidersAjax($element) {
  113. return Element::create($element)->setProperty('ajax', [
  114. 'callback' => [get_class($this), 'ajaxCdnProvidersCallback'],
  115. 'wrapper' => 'cdn-providers',
  116. ]);
  117. }
  118. }

Classes

Name Description
CdnProviderBase A base class for CDN Provider settings.