Home > 7 > Changing article text and summary with template_preprocess_node()

Changing article text and summary with template_preprocess_node()

November 18Hits:1
Advertisement

I use Drupal 7.9 with Views module (if the latter is important). I've created a small module for my site that changes transliteration of Japanese words in articles from Latin to Cyrillics and vice versa (yes, I need such a thing). User sees a form with radio button set, chooses transliteration, his choice goes to $_COOKIE and then I change transliteration in article summary and body based on user's choice. Or better to say I'm trying to change it but it doesn't work. The code goes like this:

function mymodule_preprocess_node(&$variables) {     /* Suppose we have a summary containing only one word 'Yamamoto' that has to be changed to 'Ямамото' */     $variables['node']->body['und'][0]['summary'] = 'Ямамото'; } 

After this code is executed, the changed variable has the correct value of 'Ямамото'. But in the main page for some reason I see the unchanged 'Yamamoto'. What am I doing wrong? Do I change the wrong field? Do I mess up working with parameters passed by reference? Do I use the wrong hook?

Answers

I believe that the node has already been rendered by the time template_preprocess_node() is called so changing any values on the node object will have no effect. You can try looking at $variables['node']->content to see if you can modify the text in that (I think this is the full HTML rendered node though, so lots of extra text/markup to deal with here).

You're probably better off using hook_node_load() instead. This gives you a chance to alter the node object directly before it gets rendered.

Tags:nodes, hooks

Related Articles

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.499 s.