3

I've been stuck on getting a custom preprocess_node function to work in a Drupal 6 install. I can get it to echo out things based on values but I just can't seem to get overrides working. The latest thing I've tried is the answer described in the answer on this page. If I do:

$node->title = "Test";

and do a print_r on the $node I can see that it is overridden in the array with the new value but the title that's being output is still the old one. I've also tried setting $variables[title] but no luck. I'm actually trying to target something deeper than the title but thought I'd start with this. Here's an example I've tried putting in the template.php file. I've replaced my actual theme name with mythemename.

function mythemename_preprocess_node(&$variables) {

//Profiles
if($variables['type'] == "child_profile"){

    $node = $variables['node'];

        $node->title = "Test";
        //This echo correctly shows the new title but it's not actually overriding anything
        echo $node->title;


    $variables['node']=$node;
}}
  • Depending on the theme, some themes output node titles in the page, so the title could be overridden using hook_preprocess_page. Try to override the things that you want to override and let us know if it's still not working. – Beebee Aug 28 '13 at 15:43
  • I actually have tried a few things and it's the same situation. If I look at the array I can see that it's overridden but the page is still rendering the old value. Trying to change the following depending on a different field value: $node->content[group_requirements][group]['#description'] = "Test"; – Bjorn Aug 28 '13 at 16:05
  • I can't remember D6 so well, can you see an array content inside $variables? It could be that the content is already rendered and stored in $variables['content'] when it gets to the preprocess function. And that's what's being output, and not the $variables['node'] contents. – Beebee Aug 28 '13 at 16:08
3

If you look at node.tpl.php, the title is being printed like this:

<?php if (!$page): ?>
  <h2><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></h2>
<?php endif; ?>

As you can see that's using the $title variable, not $node->title.

There's a one-to-one mapping between the array keys of the template's variables, and names of variables available in the template file. If you have $variables['my_awesome_var'] in the preprocess function, that variable will be available as $my_awesome_var in the template file.

So with that in mind, all you should need to do in your preprocess function is this:

function mythemename_preprocess_node(&$variables) {
  //Profiles
  if ($variables['type'] == "child_profile") {
    $variables['title'] = 'Test';
  }
}

To complete the thought - you could also change the node template to print $node->title instead, and your current code would work fine. But since the original template file uses a separate $title variable, it would be better to stick to convention and use that method.

  • 1
    Considering that other preprocess functions from modules would change the value of $variables['title'], it doesn't make sense to change $node->title instead. This is indeed the correct answer. – kiamlaluno Aug 28 '13 at 16:51
  • This is actually what I tried first but went down the node route when I couldn't get it to work. Any ideas of what else I could check? – Bjorn Aug 28 '13 at 18:42
  • 1
    Ah, looks like I should have been using preprocess_page and not preprocess_node - feel kind of ridiculous now but this is the first time I've ventured into this area of Drupal. – Bjorn Aug 28 '13 at 19:33
  • 1
    Oops my bad, the above method will work for the node title in a teaser, the main page title is always in page.tpl.php. Pretty confusing until you know, then you'll never forget! – Clive Aug 28 '13 at 19:35

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.