2017-10-04 90 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'; 
    } 
} 

然後{{字幕}}