2017-10-04 135 views
0

我希望以编程方式改变在Drupal 8页标题如此,这将是在主题文件的硬编码。Drupal的8 - 钩更改页面标题

我试图用钩子函数preprocess_page_title,但它似乎不明白什么页面更改称号。

这是我到目前为止有:

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

我想通使在特定页面,这种变化的唯一方法是设置节点参数。有没有人想出一个办法来覆盖在Drupal的网页标题?

回答

1

这里的方法进行预处理你的页面:

function yourthemename_preprocess_page(&$variables) { 
    $node = \Drupal::routeMatch()->getParameter('node'); 
    if ($node) { 
    $variables['title'] = $node->getTitle(); 
    } 
} 

,并在您的模板page.html.twig

{{title}} 
1

在你template.theme文件中添加预处理,然后覆盖页面标题.html.twig在您的模板文件夹通过打印变量,象下面这样:

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

然后{{字幕}}