In trying to learn about theming and alter hooks. I'm looking at the render array passed to hook_node_view_alter() with the following code.

function hook_node_view_alter(&$build) {

I want to set a variable based on $build['body']['#object']['vid']), but I get this error:

Fatal error: Cannot use object of type stdClass as array

I don't understand this error. Can someone explain it?

  • What are you trying to achieve? – Gulok Jun 22 '16 at 0:52
  • i'm just studying it, pulling it apart. trying to learn to be an advanced themer ;-) – TopTomato Jun 24 '16 at 3:51
  • 1
    Remember that hook_node_view_alter param is passed by reference. But of course you can create a variable like this $myVar = $build['body']['#object']['vid’]; given that the vid is set. After that you have to integrate everything back to $build in order for your hook to take in effect. – Gulok Jun 24 '16 at 5:06
  • Please see my example. I hope you found it useful. gist.github.com/geraldvillorente/… – Gulok Jun 24 '16 at 5:14
  • that won't work, that generates the fatal error – TopTomato Jun 25 '16 at 3:25

The #object in the $build array is an object, therefor it's properties have to be referenced with an arrow, ->

$orig_vid = $build['body']['#object']->vid;
$build['body']['#object']->orig_val = $orig_vid;
$build['body']['#object']->vid = 100;

thanks Gulok for answering this

|improve this answer|||||
  • this was purely an excercise in helping to understand the data used in the hook_node_view_alter function – TopTomato Jun 25 '16 at 20:05

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