diff --git a/core/lib/Drupal/Core/Cache/UncacheableDependencyTrait.php b/core/lib/Drupal/Core/Cache/UncacheableDependencyTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..a2f00e8c0f07c1ef870971df0c806119d54ffc6a --- /dev/null +++ b/core/lib/Drupal/Core/Cache/UncacheableDependencyTrait.php @@ -0,0 +1,40 @@ +<?php + +/** + * @file + * Contains \Drupal\Core\Cache\UncacheableDependencyTrait. + */ + +namespace Drupal\Core\Cache; + +/** + * Trait to implement CacheableDependencyInterface for uncacheable objects. + * + * Use this for objects that are never cacheable. + * + * @see \Drupal\Core\Cache\CacheableDependencyInterface + */ +trait UncacheableDependencyTrait { + + /** + * {@inheritdoc} + */ + public function getCacheContexts() { + return []; + } + + /** + * {@inheritdoc} + */ + public function getCacheTags() { + return []; + } + + /** + * {@inheritdoc} + */ + public function getCacheMaxAge() { + return 0; + } + +} diff --git a/core/modules/history/src/Plugin/views/filter/HistoryUserTimestamp.php b/core/modules/history/src/Plugin/views/filter/HistoryUserTimestamp.php index 92e1acddd7865371e3d19a4fc1c287bed4feec4e..5803c92da33818863d02c3ecc6df966c9abe4c38 100644 --- a/core/modules/history/src/Plugin/views/filter/HistoryUserTimestamp.php +++ b/core/modules/history/src/Plugin/views/filter/HistoryUserTimestamp.php @@ -2,6 +2,7 @@ namespace Drupal\history\Plugin\views\filter; +use Drupal\Core\Cache\UncacheableDependencyTrait; use Drupal\Core\Form\FormStateInterface; use Drupal\views\Plugin\views\filter\FilterPluginBase; @@ -17,6 +18,8 @@ */ class HistoryUserTimestamp extends FilterPluginBase { + use UncacheableDependencyTrait; + // Don't display empty space where the operator would be. var $no_operator = TRUE; @@ -93,12 +96,4 @@ public function adminSummary() { } } - /** - * {@inheritdoc} - */ - public function getCacheMaxAge() { - // This filter depends on the current time and therefore is never cacheable. - return 0; - } - } diff --git a/core/modules/system/tests/modules/early_rendering_controller_test/src/CacheableTestDomainObject.php b/core/modules/system/tests/modules/early_rendering_controller_test/src/CacheableTestDomainObject.php index 843d785843fd541140ee3152c93b4456eb760e77..9da6fd8bea7365af27eeb69f7f2bc58f2a2e7ed9 100644 --- a/core/modules/system/tests/modules/early_rendering_controller_test/src/CacheableTestDomainObject.php +++ b/core/modules/system/tests/modules/early_rendering_controller_test/src/CacheableTestDomainObject.php @@ -3,28 +3,10 @@ namespace Drupal\early_rendering_controller_test; use Drupal\Core\Cache\CacheableDependencyInterface; +use Drupal\Core\Cache\UncacheableDependencyTrait; class CacheableTestDomainObject extends TestDomainObject implements CacheableDependencyInterface { - /** - * {@inheritdoc} - */ - public function getCacheContexts() { - return []; - } - - /** - * {@inheritdoc} - */ - public function getCacheTags() { - return []; - } - - /** - * {@inheritdoc} - */ - public function getCacheMaxAge() { - return 0; - } + use UncacheableDependencyTrait; } diff --git a/core/modules/views/src/Plugin/views/sort/Random.php b/core/modules/views/src/Plugin/views/sort/Random.php index 11e216bff9ccb9284b94127da9c7b8c7564685e0..c9a552f1b38c22d03731fb394c8677f6a4a127b2 100644 --- a/core/modules/views/src/Plugin/views/sort/Random.php +++ b/core/modules/views/src/Plugin/views/sort/Random.php @@ -3,6 +3,7 @@ namespace Drupal\views\Plugin\views\sort; use Drupal\Core\Cache\CacheableDependencyInterface; +use Drupal\Core\Cache\UncacheableDependencyTrait; use Drupal\Core\Form\FormStateInterface; /** @@ -12,6 +13,8 @@ */ class Random extends SortPluginBase implements CacheableDependencyInterface { + use UncacheableDependencyTrait; + /** * {@inheritdoc} */ @@ -28,25 +31,4 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { $form['order']['#access'] = FALSE; } - /** - * {@inheritdoc} - */ - public function getCacheMaxAge() { - return 0; - } - - /** - * {@inheritdoc} - */ - public function getCacheContexts() { - return []; - } - - /** - * {@inheritdoc} - */ - public function getCacheTags() { - return []; - } - }