Change title of a node programmatically before displaying it 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(&amp;$variables) {
    // Raj: Experimenting
    $vars['site_name'] = 'New title';
    drupal_set_title('Test changed title');
    echo "&lt;script type=\"text/javascript\"&gt;alert('MyThemeName_process_page');&lt;/script&gt;";
.
.
.
}


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 "&lt;script type=\"text/javascript\"&gt;alert('MyThemeName_process_page');&lt;/script&gt;";


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 "&amp;$variables" and conditionally update it, am I right?

Awaiting help!
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(&amp;$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 = '&lt;span id="ScientificNameDIV" class="ScientificNameSuffixClass"&gt;' . $scientificNameArray[0]['#item']['first'] . '&lt;/span&gt;';
                $theDesciber = '&lt;span id="ScientificNameSimpleDIV" class="ScientificNameSimpleClass"&gt;' . $scientificNameArray[0]['#item']['second'] . '&lt;/span&gt;';
                $prominentName = $variables['title'];
                $sciFiNameAndDescriber = $theScientificName . ' ' . $theDesciber;
                $variables['title'] = $prominentName. ' ' . '&lt;span id="ScientificNameSimpleDIV" class="ScientificNameSimpleClass"&gt; ( &lt;/span&gt;' . $sciFiNameAndDescriber . '&lt;span id="ScientificNameSimpleDIV" class="ScientificNameSimpleClass"&gt; ) &lt;/span&gt;';
                $variables['title'] = '&lt;div id="CommonNameAndScientificName" class="CommonAndScientificClass"&gt;'.$variables['title'].'&lt;/div&gt;';
                break;
            }
        }
    }
    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.