I'm currently working on a project on which I'm trying to dynamically shorten the pages titles as following:

"This is very long text blahblahblah | Site Name" becomes "This is very ... | Site Name" but the rendered page doesn't take this into account.

Here's my problem: I tried several hooks to implement this behaviour, using hook_menu_alter(), hook_preprocess_html(), hook_node_preprocess(), etc. and I followed the indications given to update the pages titles when using these hooks, but it always falls back to using the original long title.

Actually, when I set the arrays accurate keys/values, would it be $vars['head_title'], $vars['title'], etc. I can var_dump the array and verify the needed variable is updated, but the title is untouched. Using drupal_set_title($new_title) didn't help either.

I'm quite puzzled; would this be because I'm using Custom Field, Metatag, or a specific module? Or would this be because I'm not updating the page at the correct moment during its generation? Drupal is somehow new to me!

Thanks a lot :)


  • I want know how you have created page by (view,panel or something else) Commented Oct 12, 2016 at 12:37

2 Answers 2


OK! After going back and forth between var_dump's, installed modules list and hooks, I discovered that the ONLY hook that will update the page's tags is hook_metatag_metatags_view_alter(&$output), in which something like this needs to be done:

$output['title']['#attached']['metatag_set_preprocess_variable'][0][2] = "Custom title";

My n00b diagnosis is that this hook is called right after the last Metatag module's function that rebuilds the metatags from scratch, and therefore it's the only one that matters.

Hope this will help others! :)



Did you try template_preprocess_page? You can modify the $variables['title'] value there. Inside your theme's template.php file,

function YOUR-THEME_preprocess_page(&$variables) {
  $variables['title'] = YOUR-TRUNCATE-FUNCTION($variables['title']) . ' | ' . $variables['site_name'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.