core/includes/theme.inc | 9 +++++++-- core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php | 15 ++++++++++++++- .../system/src/Plugin/Block/SystemPageTitleBlock.php | 2 +- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 0cf5fa2..1d297ab 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -1234,8 +1234,13 @@ function template_preprocess_html(&$variables) { $site_config = \Drupal::config('system.site'); // Construct page title. if (isset($variables['page']['#title']) && is_array($variables['page']['#title'])) { - // Do an early render if the title is a render array. - $variables['page']['#title'] = (string) \Drupal::service('renderer')->render($variables['page']['#title']); + if (isset($variables['page']['#title']['#plain_text'])) { + $variables['page']['#title'] = $variables['page']['#title']['#plain_text']; + } + else { + // Do an early render if the title is a render array. + $variables['page']['#title'] = (string) \Drupal::service('renderer')->render($variables['page']['#title']); + } } if (!empty($variables['page']['#title'])) { $head_title = array( diff --git a/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php b/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php index cd0d482..2a80fd4 100644 --- a/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php +++ b/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Render\MainContent; use Drupal\Component\Plugin\PluginManagerInterface; +use Drupal\Component\Utility\SafeStringInterface; use Drupal\Core\Cache\Cache; use Drupal\Core\Controller\TitleResolverInterface; use Drupal\Core\Display\PageVariantInterface; @@ -195,7 +196,19 @@ protected function prepare(array $main_content, Request $request, RouteMatchInte // Determine the title: use the title provided by the main content if any, // otherwise get it from the routing information. $get_title = function (array $main_content) use ($request, $route_match) { - return isset($main_content['#title']) ? $main_content['#title'] : $this->titleResolver->getTitle($request, $route_match->getRouteObject()); + $title = isset($main_content['#title']) ? $main_content['#title'] : $this->titleResolver->getTitle($request, $route_match->getRouteObject()); + // Ensure the title is always a render array, so we can associate the + // main content's cacheability with it. + if ($title instanceof SafeStringInterface) { + $title = ['#markup' => $title]; + } + elseif (is_string($title)) { + $title = ['#plain_text' => $title]; + } + if (isset($main_content['#cache'])) { + $title['#cache'] = $main_content['#cache']; + } + return $title; }; // If the _controller result already is #type => page, diff --git a/core/modules/system/src/Plugin/Block/SystemPageTitleBlock.php b/core/modules/system/src/Plugin/Block/SystemPageTitleBlock.php index 0f431df..0e82940 100644 --- a/core/modules/system/src/Plugin/Block/SystemPageTitleBlock.php +++ b/core/modules/system/src/Plugin/Block/SystemPageTitleBlock.php @@ -47,7 +47,7 @@ public function defaultConfiguration() { * {@inheritdoc} */ public function build() { - if (empty($this->title) || (is_array($this->title) && empty($this->title['#markup']))) { + if (empty($this->title) || (is_array($this->title) && empty($this->title['#markup']) && empty($this->title['#plain_text']))) { return []; } return [