core/lib/Drupal/Core/Render/Renderer.php | 14 ++++++++++++++ core/lib/Drupal/Core/Render/RendererInterface.php | 20 ++++++++++++++++++++ core/modules/book/src/BookManager.php | 5 ++++- core/modules/comment/src/CommentForm.php | 3 ++- core/modules/comment/src/CommentViewBuilder.php | 3 ++- .../contact/src/Controller/ContactController.php | 3 ++- .../modules/forum/src/Controller/ForumController.php | 3 ++- core/modules/user/src/Form/UserLoginForm.php | 3 ++- 8 files changed, 48 insertions(+), 6 deletions(-) diff --git a/core/lib/Drupal/Core/Render/Renderer.php b/core/lib/Drupal/Core/Render/Renderer.php index b2f6072..999bdab 100644 --- a/core/lib/Drupal/Core/Render/Renderer.php +++ b/core/lib/Drupal/Core/Render/Renderer.php @@ -624,4 +624,18 @@ public function generateCachePlaceholder($callback, array &$context) { return ''; } + /** + * {@inheritdoc} + */ + public static function addCacheContexts(array &$elements, array $cache_contexts) { + $elements['#cache']['contexts'] = Cache::mergeTags(isset($elements['#cache']['contexts']) ? $elements['#cache']['contexts'] : [], $cache_contexts); + } + + /** + * {@inheritdoc} + */ + public static function addCacheTags(array &$elements, array $cache_tags) { + $elements['#cache']['tags'] = Cache::mergeTags(isset($elements['#cache']['tags']) ? $elements['#cache']['tags'] : [], $cache_tags); + } + } diff --git a/core/lib/Drupal/Core/Render/RendererInterface.php b/core/lib/Drupal/Core/Render/RendererInterface.php index 5207215..b169ae2 100644 --- a/core/lib/Drupal/Core/Render/RendererInterface.php +++ b/core/lib/Drupal/Core/Render/RendererInterface.php @@ -371,4 +371,24 @@ public static function mergeAttachments(array $a, array $b); */ public function generateCachePlaceholder($callback, array &$context); + /** + * Adds cache contexts to the given render array. + * + * @param array $elements + * The structured array describing the data to be rendered. + * @param string[] $cache_contexts + * The cache contexts to add to $elements. + */ + public static function addCacheContexts(array &$elements, array $cache_contexts); + + /** + * Adds cache tags to the given render array. + * + * @param array $elements + * The structured array describing the data to be rendered. + * @param string[] $cache_tags + * The cache tags to add to $elements. + */ + public static function addCacheTags(array &$elements, array $cache_tags); + } diff --git a/core/modules/book/src/BookManager.php b/core/modules/book/src/BookManager.php index 26c02fa..d217c58 100644 --- a/core/modules/book/src/BookManager.php +++ b/core/modules/book/src/BookManager.php @@ -11,6 +11,8 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Render\BubbleableMetadata; +use Drupal\Core\Render\Renderer; use Drupal\Core\Session\AccountInterface; use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; @@ -353,7 +355,8 @@ protected function addParentSelectFormElements(array $book_link) { '#suffix' => '', ); } - $form['#cache']['tags'] = Cache::mergeTags(isset($form['#cache']['tags']) ? $form['#cache']['tags'] : [], $config->getCacheTags()); + + Renderer::addCacheTags($form, $config->getCacheTags()); return $form; } diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index 9003df0..207f58d 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\Renderer; 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()); + Renderer::addCacheTags($form, $config->getCacheTags()); 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..b14bea8 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\Renderer; /** * 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()); + Renderer::addCacheTags($build, $config->getCacheTags()); 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..b759c2b 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\Renderer; 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()); + Renderer::addCacheTags($form, $config->getCacheTags()); return $form; } diff --git a/core/modules/forum/src/Controller/ForumController.php b/core/modules/forum/src/Controller/ForumController.php index 18d7325..fbff4a5 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\Renderer; 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()); + Renderer::addCacheTags($build, $config->getCacheTags()); 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..75d0b53 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\Renderer; 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()); + Renderer::addCacheTags($form, $config->getCacheTags()); return $form; }