I am a newbie at Drupal tweaking. I have copied Bartik theme into sites/all/themes and am modifying it as per our requirement.

One such requirement now is to programmatically modify the node title by appending value of some other field in the node to it before displaying the node. I have identified that the right way to achieve it is by updating the variables in hook_process_page() or hook_preprocess_node() function in template.php file of the theme.

For testing purpose I have done the following change:

 * Override or insert variables into the page template.
function MyThemeName_process_page(&$variables) {
    // Raj: Experimenting
    $vars['site_name'] = 'New title';
    drupal_set_title('Test changed title');
    echo "<script type=\"text/javascript\">alert('MyThemeName_process_page');</script>";

I could see that none of the changes are affected. Since I am just beginning to tweak, I have no idea how to debug a php function as it is a server side script. For the same reason I have tried to display alert which fails to display too!

I thought that the function is not being triggered at all, so I intentionally induced error in the 'echo' line by removing a character:

ech "<script type=\"text/javascript\">alert('MyThemeName_process_page');</script>";

The interpreter complains with this error:

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /Applications/XAMPP/xamppfiles/htdocs/MyWebsite/cms/sites/all/themes/MyThemeName/template.php on line 48

This means that the interpreter is triggering the function, but why does not it echo and put out the alert?

Also, what is the approach to update the node title? I am assuming that there is some variable which I should be interested in the array "&$variables" and conditionally update it, am I right?

Awaiting help!

  • 4
    Preprocess/process functions are use to pass variables to template files - writing to the output buffer in them will have no effect; or at least not the effect you expect. See Setting up variables for use in a template (preprocess and process functions)
    – Clive
    Sep 2, 2013 at 15:53
  • I swear I didnt get anything from that link :P I guess the level of detail is too much for a beginner, perhaps it would help me if I am told which method to override and look for? Thanks. Sep 2, 2013 at 16:16
  • 1
    He means you can not use echos in a preprocess function, and even if you did, you would not echo script tags. Try installing the devel module, then you can use the dpm('MyThemeName_process_page') instead to debug.
    – 2pha
    Sep 3, 2013 at 3:38
  • Thank you, I installed Themer devel module and now am digging into the function calls. I have implemented the candidate function which is being suggested by the Themer devel but that too ain't making any difference. Sep 3, 2013 at 3:46
  • Thank you both, I installed Themer devel module and it was hugely helpful. I could understand the code flow better with the help of it and finally provide the functionality which was required. Sep 13, 2013 at 11:47

2 Answers 2


To modify the node title while displaying, I had to do changes in the theme.

Implement the hook (don't remember if it was already there) in template.php file:

function themeName_process_page(&$variables)

And then had to add this part of custom code at the end of the function:

// Raj: To Append the scientific name and it's desciber to the title
$nodesArray = $variables['page']['content']['system_main']['nodes'];
if (!empty($nodesArray))
    foreach ($nodesArray as $speciesNode)
        if (!empty($speciesNode['field_common_names']))
            $scientificNameArray = $speciesNode['field_scientific_name_1'];
            if (!empty($scientificNameArray))
                $theScientificName = '<span id="ScientificNameDIV" class="ScientificNameSuffixClass">' . $scientificNameArray[0]['#item']['first'] . '</span>';
                $theDesciber = '<span id="ScientificNameSimpleDIV" class="ScientificNameSimpleClass">' . $scientificNameArray[0]['#item']['second'] . '</span>';
                $prominentName = $variables['title'];
                $sciFiNameAndDescriber = $theScientificName . ' ' . $theDesciber;
                $variables['title'] = $prominentName. ' ' . '<span id="ScientificNameSimpleDIV" class="ScientificNameSimpleClass"> ( </span>' . $sciFiNameAndDescriber . '<span id="ScientificNameSimpleDIV" class="ScientificNameSimpleClass"> ) </span>';
                $variables['title'] = '<div id="CommonNameAndScientificName" class="CommonAndScientificClass">'.$variables['title'].'</div>';
    unset ($speciesNode);

Please see that in the above code, most of it belongs to the context of my website where I had to append the scientific name of the species to the title of the species inside brackets. The point here is, you need to modify the $variables['title'] inside process_page() hook.

Also most important, to see the changes in affect, clear the cache. I had a rough time thinking that the changes are not working, only very late did I realize that I was doing everything right but did not clear cache. It's a learning experience for me.


Page title or node title?

  1. To construct page titles (which show on the element on the head section), try to install the page_title module, which allows to make patterns for page titles based on any tokens (install the token module first).
  2. To construct node titles, there is the auto_nodetitle modules which allows to construct the node title in a similar way, using a token-based pattern.

Doing it programmatically is possible, but less maintainable and takes lots more effort, so try to find a module which works before taking the hard way.

  • Thank you for your inputs, I have done it programmatically since the field which I am using to fetch the value from is a double field and I don't think it is possible to pick up values using tokens in this case. I will post the answer shortly, maybe it will be helpful to anybody else out there. Sep 5, 2013 at 10:08
  • I have added the answer which explains how I have done it programmatically. If am not troubling you too much, please take time to review it :) Sep 13, 2013 at 11:45

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.