file CdnCacheTtlBase.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\Plugin\Provider\ProviderInterface;
  9. use Drupal\bootstrap\Utility\Element;
  10. use Drupal\Core\Form\FormStateInterface;
  11. /**
  12. * Base class for "cdn_cache_ttl_*" settings.
  13. *
  14. * @ingroup plugins_setting
  15. */
  16. abstract class CdnCacheTtlBase extends CdnProviderBase {
  17. /**
  18. * The DateFormatter service.
  19. *
  20. * @var \Drupal\Core\Datetime\DateFormatterInterface
  21. */
  22. static protected $dateFormatter;
  23. /**
  24. * A list of TTL options.
  25. *
  26. * @var array
  27. */
  28. static protected $ttlOptions;
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function alterFormElement(Element $form, FormStateInterface $form_state, $form_id = NULL) {
  33. $setting = $this->getSettingElement($form, $form_state);
  34. $setting->setProperty('options', $this->getTtlOptions());
  35. // @todo This really shouldn't be here, but there isn't a great way of
  36. // setting this from the provider.
  37. if ($this->provider->getPluginId() === 'custom') {
  38. $setting->setProperty('disabled', TRUE);
  39. $setting->setProperty('description', '');
  40. $group = $this->getGroupElement($form, $form_state);
  41. $group->setProperty('description', $this->t('All caching is forced to "Forever" when using the "Custom" CDN Provider. This is because the provided Custom URLs above are used as part of the cache identifier. Anytime the above Custom URLs are modified, all of the caches are rebuilt automatically.'));
  42. }
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function autoCreateFormElement() {
  48. // Don't auto create these; they are created as part of CDN Provider.
  49. // @see \Drupal\bootstrap\Plugin\Setting\Advanced\Cdn\CdnProvider::alterFormElement()
  50. return FALSE;
  51. }
  52. /**
  53. * Retrieves the TTL options.
  54. */
  55. protected function getTtlOptions() {
  56. if (!isset(static::$ttlOptions)) {
  57. $dateFormatter = $this->getDateFormatter();
  58. $intervals = [
  59. ProviderInterface::TTL_NEVER,
  60. ProviderInterface::TTL_ONE_DAY,
  61. ProviderInterface::TTL_ONE_WEEK,
  62. ProviderInterface::TTL_ONE_MONTH,
  63. ProviderInterface::TTL_THREE_MONTHS,
  64. ProviderInterface::TTL_SIX_MONTHS,
  65. ProviderInterface::TTL_ONE_YEAR,
  66. ];
  67. static::$ttlOptions = array_map([$dateFormatter, 'formatInterval'], array_combine($intervals, $intervals));
  68. static::$ttlOptions[ProviderInterface::TTL_NEVER] = (string) $this->t('Never');
  69. static::$ttlOptions[ProviderInterface::TTL_FOREVER] = (string) $this->t('Forever');
  70. }
  71. return static::$ttlOptions;
  72. }
  73. /**
  74. * Retrieves the DateFormatter service.
  75. *
  76. * @return \Drupal\Core\Datetime\DateFormatterInterface
  77. * The DateFormatter service.
  78. */
  79. protected function getDateFormatter() {
  80. if (!isset(static::$dateFormatter)) {
  81. static::$dateFormatter = \Drupal::service('date.formatter');
  82. }
  83. return static::$dateFormatter;
  84. }
  85. }

Classes

Name Description
CdnCacheTtlBase Base class for "cdn_cache_ttl_*" settings.