core/lib/Drupal/Core/Render/BubbleableMetadata.php | 19 +++++++++++++++++++ core/modules/book/src/BookManager.php | 3 ++- core/modules/comment/src/CommentForm.php | 3 ++- core/modules/comment/src/CommentViewBuilder.php | 3 ++- .../contact/src/Controller/ContactController.php | 3 ++- core/modules/forum/src/Controller/ForumController.php | 3 ++- core/modules/user/src/Form/UserLoginForm.php | 3 ++- 7 files changed, 31 insertions(+), 6 deletions(-) diff --git a/core/lib/Drupal/Core/Render/BubbleableMetadata.php b/core/lib/Drupal/Core/Render/BubbleableMetadata.php index 2f62b44..79fef01 100644 --- a/core/lib/Drupal/Core/Render/BubbleableMetadata.php +++ b/core/lib/Drupal/Core/Render/BubbleableMetadata.php @@ -9,6 +9,7 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Core\Cache\Cache; +use Drupal\Core\Cache\CacheableInterface; /** * Value object used for bubbleable rendering metadata. @@ -103,4 +104,22 @@ public static function createFromRenderArray(array $build) { return $meta; } + /** + * Creates a bubbleable metadata object from a cacheable object. + * + * @param \Drupal\Core\Cache\CacheableInterface $object + * A cacheable object. + * + * @return static + */ + public static function createFromCacheableObject(CacheableInterface $object) { + $meta = new static(); + // With the cache context bubbling issue in: + //$meta->contexts = $object->getCacheContexts(); + $meta->tags = $object->getCacheTags(); + // With a #cache['max-age'] property in the future: + //$meta->maxAge = $object->getCacheMaxAge(); + return $meta; + } + } diff --git a/core/modules/book/src/BookManager.php b/core/modules/book/src/BookManager.php index 26c02fa..d8cf58d 100644 --- a/core/modules/book/src/BookManager.php +++ b/core/modules/book/src/BookManager.php @@ -11,6 +11,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Render\BubbleableMetadata; use Drupal\Core\Session\AccountInterface; use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; @@ -353,7 +354,7 @@ protected function addParentSelectFormElements(array $book_link) { '#suffix' => '', ); } - $form['#cache']['tags'] = Cache::mergeTags(isset($form['#cache']['tags']) ? $form['#cache']['tags'] : [], $config->getCacheTags()); + BubbleableMetadata::createFromCacheableObject($config)->applyTo($form); return $form; } diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index 9003df0..3f864a9 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -16,6 +16,7 @@ use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageInterface; +use Drupal\Core\Render\BubbleableMetadata; use Drupal\Core\Session\AccountInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -203,7 +204,7 @@ public function form(array $form, FormStateInterface $form_state) { '#access' => $is_admin, ); - $form['#cache']['tags'] = Cache::mergeTags(isset($form['#cache']['tags']) ? $form['#cache']['tags'] : [], $config->getCacheTags()); + BubbleableMetadata::createFromCacheableObject($config)->applyTo($form); return parent::form($form, $form_state, $comment); } diff --git a/core/modules/comment/src/CommentViewBuilder.php b/core/modules/comment/src/CommentViewBuilder.php index bf0f526..d4af8dd 100644 --- a/core/modules/comment/src/CommentViewBuilder.php +++ b/core/modules/comment/src/CommentViewBuilder.php @@ -11,6 +11,7 @@ use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityViewBuilder; +use Drupal\Core\Render\BubbleableMetadata; /** * Render controller for comments. @@ -104,7 +105,7 @@ public function buildComponents(array &$build, array $entities, array $displays, $account = comment_prepare_author($entity); $config = \Drupal::config('user.settings'); - $build['#cache']['tags'] = Cache::mergeTags(isset($build['#cache']['tags']) ? $build['#cache']['tags'] : [], $config->getCacheTags()); + BubbleableMetadata::createFromCacheableObject($config)->applyTo($build); if ($config->get('signatures') && $account->getSignature()) { $build[$id]['signature'] = array( '#type' => 'processed_text', diff --git a/core/modules/contact/src/Controller/ContactController.php b/core/modules/contact/src/Controller/ContactController.php index 648a064..40259ba 100644 --- a/core/modules/contact/src/Controller/ContactController.php +++ b/core/modules/contact/src/Controller/ContactController.php @@ -12,6 +12,7 @@ use Drupal\Core\Datetime\DateFormatter; use Drupal\Core\Flood\FloodInterface; use Drupal\contact\ContactFormInterface; +use Drupal\Core\Render\BubbleableMetadata; use Drupal\user\UserInterface; use Drupal\Component\Utility\String; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -106,7 +107,7 @@ public function contactSitePage(ContactFormInterface $contact_form = NULL) { $form = $this->entityFormBuilder()->getForm($message); $form['#title'] = String::checkPlain($contact_form->label()); - $form['#cache']['tags'] = Cache::mergeTags(isset($form['#cache']['tags']) ? $form['#cache']['tags'] : [], $config->getCacheTags()); + BubbleableMetadata::createFromCacheableObject($config)->applyTo($form); return $form; } diff --git a/core/modules/forum/src/Controller/ForumController.php b/core/modules/forum/src/Controller/ForumController.php index 18d7325..6789f7d 100644 --- a/core/modules/forum/src/Controller/ForumController.php +++ b/core/modules/forum/src/Controller/ForumController.php @@ -11,6 +11,7 @@ use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Entity\EntityAccessControlHandlerInterface; use Drupal\Core\Entity\EntityStorageInterface; +use Drupal\Core\Render\BubbleableMetadata; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; use Drupal\forum\ForumManagerInterface; @@ -191,7 +192,7 @@ protected function build($forums, TermInterface $term, $topics = array(), $paren if (empty($term->forum_container->value)) { $build['#attached']['feed'][] = array('taxonomy/term/' . $term->id() . '/feed', 'RSS - ' . $term->getName()); } - $build['#cache']['tags'] = Cache::mergeTags(isset($build['#cache']['tags']) ? $build['#cache']['tags'] : [], $config->getCacheTags()); + BubbleableMetadata::createFromCacheableObject($config)->applyTo($build); return [ 'action' => $this->buildActionLinks($config->get('vocabulary'), $term), diff --git a/core/modules/user/src/Form/UserLoginForm.php b/core/modules/user/src/Form/UserLoginForm.php index a0c0876..e112a20 100644 --- a/core/modules/user/src/Form/UserLoginForm.php +++ b/core/modules/user/src/Form/UserLoginForm.php @@ -11,6 +11,7 @@ use Drupal\Core\Flood\FloodInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Render\BubbleableMetadata; use Drupal\user\UserAuthInterface; use Drupal\user\UserStorageInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -112,7 +113,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { $form['#validate'][] = '::validateAuthentication'; $form['#validate'][] = '::validateFinal'; - $form['#cache']['tags'] = Cache::mergeTags(isset($form['#cache']['tags']) ? $form['#cache']['tags'] : [], $config->getCacheTags()); + BubbleableMetadata::createFromCacheableObject($config)->applyTo($form); return $form; }