2

I am trying to alter the title of my nodes to prevent URLS appearing in any title. This is the code I am using.

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

cleaner() removes URLs from nodes. The above works perfectly on teasers, but not on full view or when the title is displayed in a list with other articles inside a block.

How can I make sure that the URL disappear whenever the concerned node's title is listed?

2

2 Answers 2

0

You need to implement both

  • template_preprocess_page() : this will take care of node full view mode.
  • temaplte_proprocess_node() : this will take care of node teaser view mode

In your theme template.php

function mythemename_preprocess_page(&$variables) {
  if(isset($variables['node']) && $variables['node']->type == "your content type") {
    drupal_set_title("your_title");    
  }
}

/**
 * Override or insert variables into the node template.
 */
function mythemename_preprocess_node(&$variables) {
  if($variables['type'] == "your content type") {
    $variables['title'] = "your title";
  }
}

Note:

  • only changing in template_preprocess_node(), will now reflect on node teaser view as it will be overridden by page template.
  • in order to change you should use drupal_set_title() instead of $variables['title'].
2
  • Thanks this works perfectly for the existing nodes, but for the new ones i ll use the above solution with node_presave
    – yazzou
    Commented Oct 2, 2013 at 13:58
  • true, but its wiser according to me having one solution as keeping the other two proprocess hooks will be fired anyway! so take your pick honestly I do not see difference between existing and new if you have all the codes put to work.Preferably you can dry run with node_save function keeping code mentioned in hook_node_presave() and remove the preprocess function from template file
    – arpitr
    Commented Oct 2, 2013 at 14:10
-1

Use template_preprocess_page() in the template.php to update the page title for full view, However please don't your code for hook_preprocess_node() as that code would be required for updating the page title for teaser view.

Examples:

// Example of updating the nodes title for all content types.
function MYTHEME_preprocess_page(&$vars) {
  // Check if the variable is of node type.
  if (!empty($vars['node']) {
    $updated_title = cleaner(check_plain($node->title));
    drupal_set_title($updated_title);
  }
}

To update the page title of specific page just update the if condition as below:

if (!empty($variables['node']) && $variables['node']->type == 'CONTENT_TYPE') {
  $updated_title = cleaner(check_plain($node->title));
  drupal_set_title($updated_title);
}

The above things will update the page title at the time of rendering, however the better solution is to clear the URL's from the node title before saving (insert/update) the node so that you wont need to modified at the time of rendering:

// Using the hook_node_presave() for removing the URL's from title.
function MyModule_node_presave($node) {
  $node->title = cleaner(check_plain($node->title));
}

Note: The hook_node_presave() will help you for new nodes and the nodes those you are going to update.

4
  • 1
    You shouldn't really call drupal_set_title() from a template like that. If you must, then put it in template_preprocess_page() in the tempalte.php, otherwise in a module in hook_node_view() or similar.
    – rooby
    Commented Oct 1, 2013 at 4:12
  • 1
    hook_view_pre_render is not suppose to use here!this hook is invoked jus before rendering a view
    – arpitr
    Commented Oct 1, 2013 at 4:33
  • @arpitr He wants to update the title of his views too Commented Oct 1, 2013 at 4:35
  • 1
    you might be confusing with "The above works perfectly on teasers, but not on full view" node full view and view are two different things :)
    – arpitr
    Commented Oct 1, 2013 at 4:42

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.