I am theming a node for a specific content type and split the theme into whether the node is being displayed as a teaser or full. This error only comes up when you view the full node. The node displays fine as a teaser, which makes this error even more confusing as the problematic line is used during teaser display...Below is the code for the teaser part ofthe template, any help would be appreciated

Error: Cannot use string offset as an array in XXXXXX/node--event.tpl.php on line 100

code:

  <?php if ($teaser): ?>
  <div id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>

        <h2<?php print $title_attributes; ?>>
      <a href="<?php print $node_url; ?>"><?php print $title; ?> - 

        <span class="eventdate"><?php print(substr($node->field_date[und][0][value], 0, -9));?></span>
        </a></h2>
  <div class="content clearfix"<?php print $content_attributes; ?>>
     <?php
      // We hide the comments and links now so that we can render them later.
      hide($content['comments']);
      hide($content['links']);
      //*********LINE100**************
print($node->body[und][0][safe_value]);
    ?>
  </div>

    <div class="link-wrapper">
      <?php print $links; ?>
    </div>
    </div>
  <?php endif; ?>
share|improve this question
What version of Drupal? – sheena_d Apr 16 '12 at 20:24
Currently using 7.12 – A_funs Apr 16 '12 at 20:39

2 Answers

In Drupal 7, you should be utilizing the render() function rather than directly printing a node field's value. So, line 100 of your node.tpl.php file should read:

print render($content['body']);

If you need to make changes to the data in the $content['body'] variable for teaser views, that sort of processing should be done in an implementation of template_preprocess_node(), not in the node template.

More information about your motivation behind the code on line 100 and your desired result would help me to provide a solution more specific to your situation.

share|improve this answer
Hi Sheena, thanks for your help. – A_funs Apr 16 '12 at 21:51
I am trying to gain complete control over each value in the $node object and how it's displayed. I looked at the drupal documentation for the preprocess function and it's a little unclear. Within this preprocess function am I simply to turn $node->body[und][0][safe_value] into a variable and that variable will be accessible in the node.tpl.php ? – A_funs Apr 16 '12 at 21:55
I'm not sure why you are trying to utilize $node->body[und][0][safe_value] at all. – sheena_d Apr 16 '12 at 22:03
Each field in the node template has a corresponding variable you can use, for the body you can use $body[0]['safe_value']. – enzipher Jan 11 at 19:06

If you get that error, it means that $node->body is not an array, but a string. In that case, PHP would take $node->body[und] as a string offset, and you could not use $node->body[und][0][safe_value].

To notice that both und, and safe_value are considered from PHP two constants, but Drupal doesn't define those constants. In that case, PHP would use as value of the constants und, and safe_value, the values of the strings 'und', and 'safe_value', but you should not rely on that.

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.