0

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.

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

The node displays fine as a teaser, which makes this error even more confusing as the problematic line is used during teaser display. The code below is the one for the teaser part of the template.

  <?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; ?>
  • What version of Drupal? – sheena_d Apr 16 '12 at 20:24
  • Currently using 7.12 – A_funs Apr 16 '12 at 20:39
0

I don't know if this helps, but I encountered this error because I declared an array as a string earlier in my own code.

https://php.net/manual/en/migration71.incompatible.php#migration71.incompatible.empty-string-index-operator (in PHP 7.0 this is allowed, but PHP 7.1+ will not covert stings to arrays)

I can't see enough of your code to inspect the issue, but it's possible something similar maybe happening.

https://stackoverflow.com/questions/1873970/cannot-use-string-offset-as-an-array-in-php

1

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.

0

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.

  • 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 '13 at 19:06

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.