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');
  }
}

参考:主题预处理函数
在这里插入图片描述
前文是通过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');
  }
}

在这里插入图片描述
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';
  }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值