2

I am very new to Drupal programming and I am now trying to take my first steps there. I want to start off with the very basics such as iterate thought the items of a node and print them.

Based on this node object reference I have something like this in my code

$value = $node->title;
dpm($value , $name = "Node's title");

but how would I iterate though the node's fields. Here's an example of some fields printed from devel

field_event (Array, 1 element)

0 (Array, 5 elements)
    value (String, 19 characters ) 2012-03-17 08:00:00
    value2 (String, 19 characters ) 2012-03-18 15:00:00
    timezone (String, 13 characters ) Europe/Athens
    timezone_db (String, 3 characters ) UTC
    date_type (String, 8 characters ) datetime

field_city (Array, 1 element)

0 (Array, 2 elements)
    tid (String, 1 characters ) 1
    taxonomy_term (Object) stdClass
        tid (String, 1 characters ) 1
        vid (String, 1 characters ) 2
        name (String, 10 characters ) London
        description (String, 0 characters )
        format (String, 13 characters ) filtered_html
        weight (String, 1 characters ) 0
        vocabulary_machine_name (String, 4 characters ) city
        rdf_mapping (Array, 5 elements)

The first one is a date object and the second one is a taxonomy object. How would I access their data? Let's say I want the value2 for the date field and the name of the taxonomy term. Is this how you would go grab them?

$value = $node->field_event[0]->value2;
$value = $node->field_city[0]->taxonomy_term->name;

I am pretty confused on how you iterate though this maze of objects and arrays. Any simple explanations with examples?

  • It seems to me that you are exactly right with your syntax, so I don't understand where the trouble is. – Countzero Mar 2 '12 at 17:38
  • The problem is that the statements I wrote don't work. So the syntax can't be right. Are you sure that this is the way to retrieve these values? – Perisdr Mar 3 '12 at 15:27
  • 1
    In what context do you use these statements ? To be sure about what's available, you can dpm what you want in a progressive way, first $node, then $node->field_whateverand so on. You'll see where the error is. I can assure you I use that kind of syntax successfully on a daily basis. – Countzero Mar 3 '12 at 15:39
  • That is useful If I try dpm($node->field_event , $name = "Event"); I get as a result ... (Array, 1 element) und (Array, 1 element) 0 (Array, 5 elements) value (String, 19 characters ) 2012-04-26 09:00:00 value2 (String, 19 characters ) 2012-04-28 09:00:00 timezone (String, 13 characters ) Europe/Athens timezone_db (String, 3 characters ) UTC date_type (String, 8 characters ) datetime How do I keep going even further down? If I try $node->field_event[0] I get Undefined offset: 0. It is an array shouldn't it work? – Perisdr Mar 5 '12 at 10:08
  • 1
    I think i finally figured it out. This syntax seems to work dpm($node->field_event['und'][0]['value'] , $name = "Event"); – Perisdr Mar 5 '12 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.