Skip to main content
knackforge blog knowledge base

Setting browser tab/ window title from Drupal menu

One of the Drupal sites we did recently had a requirement to set browser tab (or window) title from Drupal menu. Usually it will be h1 title of the web page in Drupal that will be shown as tab title. Even the modules like page_title module doesn't seem to have any configuration for our case. But there is always some APIs and hooks to rescue the developer. For our case, the following eight line snippet did the trick,

  1. function themename_preprocess_html(&$vars) {
  2.   $menu = menu_get_active_trail();
  3.   if (isset($menu[1]['title']) && !drupal_is_front_page()) {
  4.     $title = $menu[1]['title'];
  5.     $vars['head_title_array']['title'] = $title;
  6.     $vars['head_title'] = implode(' - ', $vars['head_title_array']);
  7.   }
  8. }

Essentially the above code implements hook_preprocess_html(), in that makes a call to menu_get_active_trail() API and overrides the page title if the page being viewed is associated to main menu.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.