Posted By

stiobhart on 11/11/09


php node array loop drupal override

Versions (?)

Who likes this?

2 people have marked this snippet as a favorite


drupal - find available variable names and content for node

 / Published in: PHP

because i always forget [or sometimes didnae know in the first place], what variables are available, and what values they hold, when overriding a drupal node.tpl file.

since drupal tends to hold all the info for constructing a page inside nested arrays... inside nested arrays... inside nested arrays... etc. etc. you need to dig down quite a few levels before you actually start getting to the meat and potatoes.

stick this code in the top of one of your node_xxxxx.tpl.php files and it will dig four nested arrays deep and print out what it finds in a logical format:

arrayname["nested arrayname"]["nested arrayname"]["nested arrayname"] = VALUE

any arrays which dinnae go four levels deep will return a "Invalid argument supplied for foreach().." error and you'll have to not dig so deep to find their values. any values hidden deeper than four nested levels will just return as ARRAY and you'll need to dig even deeper!

when you get the value you're after, you generally use it in a node template by using everything after the 'node' bit. so if my value was at:

field_productimage["field"]["#node"]["title"] = some product name

i'd add it into my drupal node template using something like:

<div> $node->title </div>

OK it ain't very elegant and it's a bit of a clunker, as you need to hack the code about for different levels of digging, but it helps me find the crap i'm looking for, so it'll do me 'til i write sommit better.

  1. <?php
  2. foreach($node->content as $outerarray=>$outerarrayvalue)
  3. {
  4. foreach($outerarrayvalue as $innerarray=>$innerarrayvalue)
  5. {
  6. foreach($innerarrayvalue as $centrearray=>$centrearrayvalue)
  7. {
  8. foreach($centrearrayvalue as $key => $value)
  9. {
  10. echo("<em>".$outerarray."[\"".$innerarray."\"][\"".$centrearray."\"][\"".$key."\"] = </em><b>".$value."</b><br />");
  11. }
  12. }
  13. }
  14. }
  15. ?>

Report this snippet  

You need to login to post a comment.