5

With Drupal API I found you can get the values of a node with noad_load like so:

$thisnode = node_load($nid = $child2, $vid = NULL, $reset = FALSE);

Now it is easy to echo the title in this way

$thisnode->title;

But the array begins to get quite complex when I try and get data from the custom image_thumbnail field because it is array within array. See the full printed array for one node:

stdClass Object ( [vid] => 4 [uid] => 1 [title] => Who we are [log] => [status] => 1 [comment] => 0 [promote] => 0 [sticky] => 0 [nid] => 4 [type] => level_2 [language] => und [created] => 1345561152 [changed] => 1346982536 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1346982536 [revision_uid] => 1 [body] => Array ( [und] => Array ( [0] => Array ( [value] => blah blah blah [summary] => [format] => plain_text [safe_value] => 

blah blah blah
 [safe_summary] => ) ) ) [field_parent2] => Array ( [und] => Array ( [0] => Array ( [target_id] => 2 ) ) ) [field_level2_thumbnail_image] => Array ( [und] => Array ( [0] => Array ( [fid] => 4 [alt] => this is a photo [title] => this is a photo [width] => 308 [height] => 198 [uid] => 1 [filename] => article-image.png [uri] => public://article-image.png [filemime] => image/png [filesize] => 105660 [status] => 1 [timestamp] => 1346982536 [rdf_mapping] => Array ( ) ) ) ) [rdf_mapping] => Array ( [rdftype] => Array ( [0] => sioc:Item [1] => foaf:Document ) [title] => Array ( [predicates] => Array ( [0] => dc:title ) ) [created] => Array ( [predicates] => Array ( [0] => dc:date [1] => dc:created ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [changed] => Array ( [predicates] => Array ( [0] => dc:modified ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [body] => Array ( [predicates] => Array ( [0] => content:encoded ) ) [uid] => Array ( [predicates] => Array ( [0] => sioc:has_creator ) [type] => rel ) [name] => Array ( [predicates] => Array ( [0] => foaf:name ) ) [comment_count] => Array ( [predicates] => Array ( [0] => sioc:num_replies ) [datatype] => xsd:integer ) [last_activity] => Array ( [predicates] => Array ( [0] => sioc:last_activity_date ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) ) [name] => slawrence10 [picture] => 0 [data] => b:0; )

What's the best way to extract for example alt from *field_level2_thumbnail_image*?

I can do it like so:

$thisnode->field_level2_thumbnail_image['und'][0]['alt'];

But is this stable? Will 'und' always be 0? Am I missing something here?

2 Answers 2

6

If it is for page view of some kind, I think you can maybe try for this more robust way of getting at field values:

$output = field_view_field('node', $node, 'field_name');

An important trick that shows one of the ways to deal with field data properly. Initially learned a while back off this site.

Another method of getting at the data I just thought of and use quite a bit in a recent project is entity api. Highly recommended add-on. Consider the following borrowed snippet that shows how to deal with mail field. Other fields are the same.

   $wrapper = entity_metadata_wrapper('node', $node);
   $wrapper->author->mail->value();

To update the user's mail address one could use

   $wrapper->author->mail->set('[email protected]');

   or

   $wrapper->author->mail = '[email protected]';

Last but not least, there are still yet other alternatives surely?

Best practice for language key for "und" in hook_form_alter. I did not flag as duplicate as I did not check into detail if other question was detailed or not, but it should be enough to give you ideas.

Good-luck.

0
1

It is sorta like this:

$node->FIELDNAME[LANGUAGE][DELTA][INFORMATION]

So, in your example, it would be:

$node->field_level2_thumbnail_image['und'][0]['filename'];

Some details:

['und'] =======> LANGUAGE_NONE or none defined for this node
[0] ===========> The first value.  If this were a multiple value field, you could have more
['filename'] ==> The filename you are looking for. 
['alt'] =======> The alt tag 
2
  • Thanks for that. So because this is a custom field for one image. I'm safe to assume language none (und) and always first value of '0'? Sep 7, 2012 at 3:26
  • yep, and definitely look into the links @stefgosselin mentions, too.
    – Jimajamma
    Sep 7, 2012 at 3:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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