(Newbie, but have read "Drupal 7 Module Development" and searched api and forums)
I want to change the node titles in a book using a user-defined field. But for starters, I'm just trying to change them to "changeX." Easy things first. Or so I thought. Here's my code:

function mod01_preprocess_node(&$variables) {
$variables['title'] = "change1";
$variables['node']->title = "change2";
drupal_set_message('node:

' . print_r($variables, TRUE) . '

');

I can see that both changes work, and the second manages to replace 7 occurrences of the original title (??) so that the original title is completely wiped out of the $variables array. But still the original title shows up on my web pages. I know the title is output using $title, but I thought that variable would be derived form some element of $variables.

Any suggestions for how to really change the title? Thanks.

Comments

sstoft’s picture

So after 3 days of struggle, I ask the forum. And an hour later the answer hits me. Bad luck or telepathic help; don't know which.
So the answer was: use module_preprocess_page (not ..._node).

And, as a bonus, to replace the title with a custom field, just do this:

function mod01_preprocess_page(&$variables) {
$variables['title'] = $variables['node']->field_yourfieldname['und'][0]['safe_value'];

Now I have to make it stop trying to do this on non-node pages. But I think I'll figure that one out.
It seems to me this allow long titles on book pages without cluttering up the book navigation.
Case closed.

pavlosdan’s picture

Hi sstoft,

Just came across your post and wanted to chip in that its better to use field_view_value() to find and display your title rather than field_yourfieldname['und'][0]['safe_value'].

For reasons why field_view_value() is better and how to use it exactly I will just point you to this blog post:
http://www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way

Hope this is useful to you or someone else :)