file CdnTheme.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\Utility\Element;
  9. use Drupal\Core\Form\FormStateInterface;
  10. /**
  11. * The "cdn_theme" theme setting.
  12. *
  13. * @ingroup plugins_setting
  14. *
  15. * @BootstrapSetting(
  16. * id = "cdn_theme",
  17. * type = "select",
  18. * weight = 3,
  19. * title = @Translation("Theme"),
  20. * description = @Translation("Choose a theme provided by the CDN Provider."),
  21. * groups = {
  22. * "cdn" = @Translation("CDN (Content Delivery Network)"),
  23. * "cdn_provider" = false,
  24. * },
  25. * )
  26. */
  27. class CdnTheme extends CdnProviderBase {
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function alterFormElement(Element $form, FormStateInterface $form_state, $form_id = NULL) {
  32. $setting = $this->getSettingElement($form, $form_state);
  33. $setting->setProperty('suffix', '<div id="bootstrap-theme-preview"></div>');
  34. // Immediately return if the provider doesn't support themes.
  35. $provider = $this->getProvider();
  36. if (!$provider->supportsThemes()) {
  37. $setting->access(FALSE);
  38. return;
  39. }
  40. $version = $form_state->getValue('cdn_version', $this->theme->getSetting('cdn_version'));
  41. $exceptions = $provider->trackCdnExceptions(function () use ($provider, $setting, $version) {
  42. $options = [];
  43. $themes = $provider->getCdnThemes($version);
  44. foreach ($themes as $theme => $assets) {
  45. $options[ucfirst($assets->getLibrary())][$theme] = $assets->getLabel();
  46. }
  47. $setting->setProperty('options', $options);
  48. });
  49. // Check for any CDN failure(s).
  50. if ($exceptions) {
  51. $setting->setError($this->t('Unable to parse the @provider API to determine available themes.', [
  52. '@provider' => $provider->getLabel(),
  53. ]));
  54. }
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function processDeprecatedValues(array $values, array $deprecated) {
  60. // @todo Remove deprecated setting support in a future release.
  61. $deprecated = "cdn_{$this->getProvider()->getPluginId()}_theme";
  62. return isset($values[$deprecated]) ? $values[$deprecated] : NULL;
  63. }
  64. }

Classes

Name Description
CdnTheme The "cdn_theme" theme setting.