天天看点

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