I am trying to use hook_node_view() to replace, in the front page, <div>:placeholder1:</div> with the content of $string.

function mymodule_node_view(&$node, $view_mode, $langcode){
  $string = "I am here";
  $node->field_column_second['und'][0]['value']['#markup'] = array(
     '#markup' => str_replace(':placeholder1:', $string, $node->field_column_second['und'][0]['value'])

I also cleared Drupal cache, but I don't see any change.

What am I doing wrong?

  • Are you writing this snippet in mymodule.module with mymodule.info existing as well? – Jimmy Ko Jun 30 '16 at 10:01
  • Why do you think hook_node_view() would be invoked for the front page too. What did you set as front page? – kiamlaluno Jun 30 '16 at 11:32
  1. According to the doc, the first parameter should be $node, rather than &$node.
  2. The replacement code should look something like this: $node->content['body'][0]['#markup'] = str_replace($old, $new, $subject); so you missed [0] in your code, when in doubt, do a dsm($node) and check the variable you want.
  3. Complete code:

    function test_node_view($node, $view_mode, $langcode){ if (drupal_is_front_page()) { // check if necessary $string = "new"; $node->content['body'][0]['#markup'] = str_replace('old', $string, $node->content['body'][0]['#markup']); } //dsm($node); }

  • Can you please check, I edit my question. – Dev Jun 30 '16 at 8:44
  • Updated, I believe the value gets displayed comes from $node->content – cchen Jun 30 '16 at 13:16

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.