2

I have used this code in template.php.

function mytemplate_preprocess_node(&$variables) {
  $variables['title'] = "trial" . check_plain($node->title);
}

I was trying to alter the page title, and wanted to make a test before. When I use that code, the title disappears from the teaser. Do you know why?

3

You shouldn't be using that to alter the page title, you can just use drupal_set_title();

| improve this answer | |
3

Also inducing title to the $variables array will not change the page title in the browser tab. So as already suggested page title can be set conditionally as below.

function mytheme_preprocess_page(&$variables) {
  if(isset($variables['node']) && $variables['node']->type == "your_content_type") {
    drupal_set_title("my_title");    
  }
}
| improve this answer | |
  • Just a question, My title alteration works on teaser view only, how to make it work on full view also ? – Yaz Sep 29 '13 at 20:46
3

template_preprocess_node() uses the following code.

  $variables['node'] = $variables['elements']['#node'];
  $node = $variables['node'];

  $variables['date'] = format_date($node->created);
  $variables['name'] = theme('username', array('account' => $node));

  $uri = entity_uri('node', $node);
  $variables['node_url'] = url($uri['path'], $uri['options']);
  $variables['title'] = check_plain($node->title);

It is perfectly fine, to set $variables['title'] in hook_preprocess_node() instead of calling drupal_set_title()/drupal_get_title(). The error in the code shown by the OP is not setting the local $node variable. The correct code is the following one.

// Drupal 7
function mytemplate_preprocess_node(&$variables) {
  $variables['title'] = "trial" . check_plain($variables['node']->title);
}

In Drupal 8, the equivalent for the code I wrote would be the following one.

// Drupal 8
function mytemplate_preprocess_node(&$variables) {
  $variables['label'] = "trial" . check_plain($variables['node']->label());
}

This would not change the title used for the <title> though. If you need to change that as well, you should use the following code.

// Drupal 7
function mytemplate_preprocess_node(&$variables) {
  $variables['title'] = "trial" . check_plain($variables['node']->title);
  drupal_set_title("trial" . check_plain($variables['node']->title), PASS_THROUGH);
}
| improve this answer | |
  • I have added $node = $variables['node']; and it works perfectly. I wanted to alter the title because i have a classifieds site where people tend to insert URLs in titles so to prevent this i wanted to test a function, for removing automatically URLS from it. So in that case i cannot use drupal_set_title() i think – Yaz Sep 28 '13 at 16:37
  • You could use drupal_set_title(), and that would change the title used for the <title> tag; if you don't need to change that, then you don't need to use drupal_set_title(). – kiamlaluno Sep 28 '13 at 16:48
2

The title 'disappears' because you are assigning and undefined value to it. You're using $node, but that variable isn't in scope/defined.

Use:

$node = $variables['node'];
$variables['title'] = "trial" . check_plain($node->title);

Also, as Steven has already said, you should (ideally) use drupal_set_title() to set the page title. However, depending on when (in the page execution) you call this function, it may or may not affect the output of what you're seeing - it can be a bit tricky. It appears you're changing the title on the theme layer, at which point I'd say your approach is generally fine, but it may be nicer to do the change in something like template_preprocess_page(), instead.

| improve this answer | |
  • Why my changes appear on teasers only and not on the titles of full nodes ? – Yaz Sep 30 '13 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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