Drupal 8 所有页面渲染相同页眉标题问题求助
Hey there, let's break down this frustrating page title problem you're facing. This almost always boils down to missing cache contexts in how your site generates or renders page titles—let's walk through the fixes step by step:
1. Audit Custom Title Generation Logic
If you're using a custom module or theme hook (like hook_preprocess_page) to modify page titles, check if you're skipping critical cache context declarations. A common mistake is hardcoding or caching the title without tying it to the current route.
For example, this flawed code would cause the title to stick across pages:
function mytheme_preprocess_page(&$variables) { // Generates a title but doesn't add cache context $variables['title'] = "My Custom Title"; }
Fix it by adding the route cache context (and any relevant cache tags if the title depends on entities):
function mytheme_preprocess_page(&$variables) { $node = \Drupal::routeMatch()->getParameter('node'); $variables['title'] = $node ? $node->getTitle() : "Default Title"; // Ensure the title refreshes per route $variables['#cache']['contexts'][] = 'route'; // Add entity cache tags if title depends on a node/user/etc. if ($node) { $variables['#cache']['tags'][] = 'node:' . $node->id(); } }
2. Check Your Theme's page.html.twig Template
Some custom themes bypass Drupal's core title rendering system, which breaks cache handling. Make sure your template uses the core page.title render array instead of a raw title variable.
Bad practice (loses cache context):
<h1>{{ title }}</h1>
Correct approach (inherits core cache settings):
{% if page.title %} <h1{{ title_attributes }}>{{ page.title }}</h1> {% endif %}
3. Test for Module/Theme Conflicts
Disable all custom modules and switch to a core theme (like Bartik or Claro), then clear cache and test. If the issue disappears, re-enable modules/themes one by one to pinpoint the culprit. Pay extra attention to modules that modify page titles, SEO, or cache behavior.
4. Verify Cache Contexts with Drupal's Debug Tools
Enable Drupal's cache debugging (via settings.php or the Devel module) to check if your pages include the route or url.path cache context. If these are missing, your site is treating all pages as identical for caching purposes.
You can enable basic cache debugging by adding this to settings.php:
$settings['cache']['bins']['render'] = 'cache.backend.null'; $settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';
(Don't leave this on production—use it only for testing!)
5. Audit Custom Route Title Callbacks
If you have custom routes defined in your module, ensure the title callback is dynamic and the route includes proper cache contexts. For example:
Flawed route definition (static title or missing cache):
mymodule.static_route: path: '/my-static-page' defaults: _controller: '\Drupal\mymodule\Controller\MyController::content' _title: "Static Title"
Fixed route with dynamic title and cache context:
mymodule.dynamic_route: path: '/my-dynamic-page/{node}' defaults: _controller: '\Drupal\mymodule\Controller\MyController::content' _title_callback: '\Drupal\mymodule\Controller\MyController::getTitle' requirements: _permission: 'access content' options: cache: contexts: ['route']
Once you fix the missing cache contexts or incorrect title rendering, your page titles should update per page instead of sticking to the first one you visit after clearing cache. Let me know if you hit any roadblocks while troubleshooting!
内容的提问来源于stack exchange,提问作者Hassan

