天天看點

Drupal菜鳥筆記之修改頁面浏覽器Title

當我們使用drupal建立網站的時候,頁面的标題可能需要根據不用的頁面而改變;而drupal中改變頁面title的方法有如下幾種:

1、hook_preprocess_html(),

/**
 * hook_preprocess_html
 */
function ixtend_report_preprocess_html(&$variables) {
  $current_path = \Drupal::service('path.current')->getPath();
  if ($current_path == '/ixtend/manager/xxx') {
    $variables["head_title"]["title"] = t('XXX');
  }
}
           

參考:主題預處理函數

Drupal菜鳥筆記之修改頁面浏覽器Title

前文是通過module來使用hook_preprocess_html(),hook可以是子產品名,也可以是主題名theme;

在theme下面的主題檔案下的xxx.theme中使用該函數同樣可以修改title。

/**
 * hook_preprocess_html
 */
function ixtend_preprocess_html(&$variables) {
  $current_path = \Drupal::service('path.current')->getPath();
  if ($current_path == '/ixtend/manager/PatientsReport') {
    $variables["head_title"]["title"] = t('XXX');
  }
}
           
Drupal菜鳥筆記之修改頁面浏覽器Title

PS:在預處理函數中還可以做其他事情,比如drupalSettings,本質上都是hook_preprocess_HOOK()。

擴充:

修改頁面中的title

/**
 * hook_preprocess_page_title
 */
function ixtend_preprocess_page_title(&$variables) {
  $current_url = \Drupal\Core\Url::fromRoute('<current>');
  $url = $current_url->getInternalPath();

  if($url == 'ixtend/manager/XXX') {
    $variables['title'] = 'TEST';
  }
}
           
Drupal菜鳥筆記之修改頁面浏覽器Title