file CdnCustom.php

Namespace

Drupal\bootstrap\Plugin\Setting\Advanced\Cdn
  1. <?php
  2. namespace Drupal\bootstrap\Plugin\Setting\Advanced\Cdn;
  3. use Drupal\bootstrap\Plugin\Form\SystemThemeSettings;
  4. use Drupal\bootstrap\Plugin\ProviderManager;
  5. use Drupal\bootstrap\Utility\Element;
  6. use Drupal\Component\Render\FormattableMarkup;
  7. use Drupal\Core\Access\AccessResultAllowed;
  8. use Drupal\Core\Form\FormStateInterface;
  9. /**
  10. * Due to BC reasons, this class cannot be moved.
  11. *
  12. * @todo Move namespace up one.
  13. */
  14. /**
  15. * The "cdn_custom" theme setting.
  16. *
  17. * @ingroup plugins_setting
  18. *
  19. * @BootstrapSetting(
  20. * id = "cdn_custom",
  21. * type = "textarea",
  22. * description = @Translation("One complete URL per line. All URLs are validated and parsed to determine available version(s) and/or theme(s). A URL can be any file ending in <code>.css</code> or <code>.js</code> (with matching response MIME type). Minified URLs can also be supplied and the will be used automatically."),
  23. * groups = {
  24. * "cdn" = @Translation("CDN (Content Delivery Network)"),
  25. * "cdn_provider" = false,
  26. * "custom" = @Translation("Custom URLs"),
  27. * },
  28. * )
  29. */
  30. class CdnCustom extends CdnProviderBase {
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function access() {
  35. return parent::access()->andIf(AccessResultAllowed::allowedIf($this->getProvider()->getPluginId() === 'custom'));
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function alterFormElement(Element $form, FormStateInterface $form_state, $form_id = NULL) {
  41. $group = $this->getGroupElement($form, $form_state);
  42. $group->setProperty('weight', 99);
  43. $group->access($this->access());
  44. $setting = $this->getSettingElement($form, $form_state);
  45. $setting->setProperty('smart_description', FALSE);
  46. $rows = count(array_filter(array_map('trim', preg_split("/\r\n|\n/", $form_state->getValue('cdn_custom', '')))));
  47. $setting->setProperty('rows', $rows > 20 ? 20 : $rows);
  48. $group->apply = $this->setCdnProvidersAjax(Element::createStandalone([
  49. '#weight' => 100,
  50. '#type' => 'submit',
  51. '#value' => $this->t('Apply'),
  52. '#submit' => [
  53. [get_class($this), 'submitApplyCss'],
  54. ],
  55. ]));
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function getDefaultValue() {
  61. return implode("\n", [
  62. 'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.css',
  63. 'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css',
  64. 'https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.js',
  65. 'https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js',
  66. ]);
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function processDeprecatedValues(array $values, array $deprecated) {
  72. // Merge the deprecated settings together to form a new line for each value.
  73. // @todo Remove deprecated setting support in a future release.
  74. return implode("\n", $values) ?: NULL;
  75. }
  76. /**
  77. * Submit callback for resetting CDN Provider cache.
  78. *
  79. * @param array $form
  80. * Nested array of form elements that comprise the form.
  81. * @param \Drupal\Core\Form\FormStateInterface $form_state
  82. * The current state of the form.
  83. */
  84. public static function submitApplyCss(array $form, FormStateInterface $form_state) {
  85. $theme = SystemThemeSettings::getTheme(Element::create($form, $form_state), $form_state);
  86. $theme->setSetting('cdn_custom', $form_state->getValue('cdn_custom'));
  87. $form_state->setRebuild();
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public static function validateFormElement(Element $form, FormStateInterface $form_state) {
  93. // Immediately return if this isn't the currently selected CDN Provider.
  94. if ($form_state->getValue('cdn_provider') !== 'custom') {
  95. return;
  96. }
  97. $theme = SystemThemeSettings::getTheme($form, $form_state);
  98. /** @var \Drupal\bootstrap\Plugin\Provider\Custom $provider */
  99. $provider = ProviderManager::load($theme, 'custom');
  100. $urls = array_filter(array_map('trim', preg_split("/\r\n|\n/", $form_state->getValue('cdn_custom', ''))));
  101. $invalid = [];
  102. foreach ($urls as $url) {
  103. try {
  104. $provider->validateUrl($url);
  105. }
  106. catch (\Exception $e) {
  107. $invalid[] = $e->getMessage();
  108. }
  109. }
  110. if ($invalid) {
  111. $form_state->setErrorByName('cdn_custom', t('Invalid Custom URLs: <ul><li>@invalid</li></ul>', [
  112. '@invalid' => new FormattableMarkup(implode('</li><li>', $invalid), []),
  113. ]));
  114. // Unfortunately, any errors set during validation prevents the form
  115. // rebuilding functionality from working. This has to be changed here.
  116. $form->cdn->cdn_provider->custom->setProperty('open', TRUE);
  117. }
  118. }
  119. }

Classes

Name Description
CdnCustom The "cdn_custom" theme setting.