挂钩更改页面标题

won*_*v22 3 drupal drupal-8

我想以编程方式更改Drupal 8中的页面标题,以便将其硬编码在主题文件中。

我正在尝试使用一个钩子函数来preprocess_page_title,但似乎不明白要在哪个页面上更改标题。

这是我到目前为止的内容:

function test_preprocess_page_title(&$variables) {
  if (arg(0) == 'node/12') {
    $variables['title'] = 'New Title';
  }
}
Run Code Online (Sandbox Code Playgroud)

我想在一个特定页面上进行此更改的唯一方法是设置node参数。有没有人找到一种方法来覆盖Drupal上的页面标题?

小智 5

在template.theme文件中添加预处理器,然后通过打印变量来覆盖template文件夹中的page-title.html.twig,如下所示:

function theme_preprocess_page_title(&$variables) {
  $node = \Drupal::request()->attributes->get('node');
  $nid = $node->id();
  if($nid == '14') {
    $variables['subtitle'] = 'Subheading';
  }
}
Run Code Online (Sandbox Code Playgroud)

然后{{字幕}}