0

Drupal 7

I am attempting to print just the city value within the location array.

I have created a content type with location and created a custom node template node--content.tpl.php. Inside of my if ($teaser) statement I have the following code:

hide($content['field_event_location']);
print render($content)

Which effectively hides the full field_event_location and renders all other content listed in the display settings of drupal 7 admin. I am attempting to render the city field with the following line:

print render($content['field_event_location']['und'][0]['city'])

I used

print_r($content);echo 

to print the array with the following results:

[field_event_location] => Array
    (
        [und] => Array
            (
                [0] => Array
                    (
                        [lid] => 167
                        [street] => 4501 Main Street
                        [additional] => 
                        [city] => Kansas City
                        [province] => 
                        [postal_code] => 64130
                        [country] => us
                        [source] => 3
                        [is_primary] => 0
                        [province_name] => 
                        [country_name] => United States
                        [email] => 
                        [phone] => 
                    )

            )

    )

I did some basic de-bugging I found from this post (How to print render Location's sub-fields?), and was able to print render($content['body']) as well as get the full location output from print render($content['field_event_location'])

2 Answers 2

1

The dirty way is : don't user "render".

Just print it:

print $content['field_event_location']['und'][0]['city'];
3
  • I was unable to get print $content to work, however I did get a log message for this one Notice: Undefined index: und in include() (line 94 of /sites/all/themes/theme-name/templates/node--content-type.tpl.php).
    – Jeff
    Commented Dec 17, 2013 at 17:51
  • Do you have the devel module installed? If yes, just send a dsm($content); to get the $content variable structure. But, regarding your post and the print_r you posted, this should work. And also you should check before that the variable exists of course...
    – Matt
    Commented Dec 17, 2013 at 17:54
  • Thanks @Matt, using devel, I was able to see the location values listed under #items, and not und. After making that change in the tpl file it works as intended.
    – Jeff
    Commented Dec 17, 2013 at 20:39
1

Have you tried to grab the data from the node? Try this:

print $node->field_event_location['und'][0]['city'];

1
  • I was unable to get a different result from this
    – Jeff
    Commented Dec 17, 2013 at 17:38

Your Answer

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

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