When to use -> to grab an element

Events happening in the community are now at Drupal community events on www.drupal.org.
aprice42's picture

Hi,

I am still learning the art of theming, and have been using theme developer, and other themes as reference in building my own theme... and I feel like I am making good progress...

Something that has come up that I don't understand, and cannot seem to find on the drupal site, is when to use the -> to grab an element

I understand how to use $variable[0]['view'] for example...
but I don't understand when to use $node->content['body']['#value']

what does the -> represent?

where can i learn more about this?

Thanks,
Andy )

Comments

You are actually into some

grantkruger's picture

You are actually into some PHP code here. This is also advanced PHP really, beyond what most designers use... but themers end up doing more of this, so let me give it a bit of a shot, but really what you need is a bit of PHP background. Others can feel free to correct me on this if I misstep or misstate. First, the tech explanation, which is going to be over your head most likely. When you see -> you are dealing with an element within a class. Brackets on their own are arrays. Two sets of brackets means an array of arrays.

So in: $node->content['body']['#value']

$node is a class. Within the class $node is an array called $content which is an array of arrays. When you show that a variable/array belongs to a class you drop the $, hence $node->content. The variable being pointed to here is within the node, at the '#value' occurrence withing the 'body' occurrence of the array $content, hence content['body']['#value'].

So what is a class? Well it's usually a collection of code and variables associated with an entity of some sort. In this case it is a node, Drupal's most basic data building block. You don't need to know all of the details, but you do need to realise that a lot of information associated with a node is held in the class $node.

What is an array? And array is a set of related data. A simple example could be an array of valid answers, for example, "Yes", "No", "Maybe", "Kinda". That would by default have numeric keys, but you can give the keys names. Eg: If I specify $answers = array("Yes", "No", "Maybe", "Kinda"); then $answers[0] would be "Yes", $answers[1] would be "No", and so on. If I want these array indexes to be more meaningful then I could do this, $answers = array("good"=>"Yes", "bad"=>"No", "silly"=>"Maybe", "confused"=>"Kinda"); Now $answers['silly'] renders "Maybe", $answers['bad'] renders "No", and so on.

So now think instead of "Yes", "No", etc in the above array I could put another array, maybe of a selection of answers, for example,
$answers = array("good"=>array("Yes", "yes", "yup"), "bad"=>array("No", "Nope", "Nah");

So now $answers['good'][0] renders "yes", $answers['good'][2] renders "yup", and so on.

So what can you do with all this? Well use print_r (e.g. print_r($node);) and there is a Drupal version of this that I can't recall at the moment, and you can dump the values for any array/class. Or easier, check out the Devel module, which can show you all variables.

Finally, from your perspective just think of $node as a giant collection of information related to a node, everything you can think of and much more. Within that bucket you have other collections of data, often held as arrays. So if you're pulling data out of a class it's $className->variableName, and if that variable is an array then you need $className->variableName[arrayIndex] or even $className->variableName[arrayIndex][innerArrayIndex]

This is a brief, rushed and yes, inadequate response, but I hope it gives you some idea. php.net is a good resource.

Grant

Sala kahle,
Grant

Not to nitpick but $node is

mikey_p's picture

Not to nitpick but $node is an object of the class stdClass which is PHP's generic basic class. Technically as a result of this the $node object has no associated methods, only properties defined on the fly.

I kinda like that nitpick.

grantkruger's picture

I kinda like that nitpick.

Sala kahle,
Grant

Thank you for the quick

aprice42's picture

Thank you for the quick response... That all makes sense, I appreciate the detailed response... It gives me a good starting place for learning more PHP anyway...

I will check out php.net... and learn some more PHP...

Thanks,
Andy )

Another way of looking at it

eliza411's picture

Hopefully what they've said already made perfect sense. It does to me now, but as a non-programmer, I had a hard time understanding what to do until I had a concrete, visual explanation and some rules.

When I look at what's available to me with dprint_r or print_r or the devel module, I follow it like a formal outline:

  1. I put -> anywhere I see (Object)
  2. The next thing directly following -> is always just the word.
  3. If there's more than just a single thing, it will usually be an Array or, less often, another Object. If it's an Array, then it will be put in brackets []
  4. If the thing in brackets also says String, you'll need '' inside the brackets

For example, in the attached screen shot taken from the Devel Load tab when looking at a node, I know to start with $node.
$node

Then, at the first level I see is (Object) stdClass. Following Rule 1, I put ->
$node->

At the second level, I see lots of things. nid, type, language, field_firstname, etc. Following Rule 2, to get the nid, I put nid, to get the type, I put type.
$node->nid or
$node->type

To get the field_firstname, I follow Rule 2.
$node->field_firstname

I see that field_first_name says, (Array), though, so I know it has more levels and needs more specificity. I click it to see what's inside. Following Rule 3, because it said Array, I put the next thing (Level 3) in brackets. It doesn't say string so I use plain brackets.
$node->field_firstname[0]

0 said Array, so I know I need to specify even more (Level 4). Following Rule 3, I use brackets and following rule 4 I add single quotes. ['value']
$node->field_firstname[0]['value']

This gets me through 95% of my theming needs.

On the rare occasion where I encounter an Object at a deeper level, I just stick in the -> and make sure the item after it isn't in brackets.

It's kind of embarrassing to describe how thoroughly I didn't get it, but once I saw it like this, I could finally understand the programmer explanations.

Thank you for the visual

aprice42's picture

Thank you for the visual breakdown, this illustration helps a lot.

Setting out to learn all of this on my own, I have found that explanations like this save me from hours of reading the same line from a technical manual over and over again until I finally see through the jargon and understand what it is saying :)

I really appreciate you taking the time to explain it in this way...

Andy )

Portland (Oregon)

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: