I'm trying to figure out how to access parts of the $content array in my template files, but I'm not having any luck.

First I did this:

hide($content['field_review_nationality']);

I want to only output the country name (taxonomy term) without any other code, but this is what happens:

This:
print render($content['field_review_nationality']);

outputs:

<div class="field field-name-field-review-nationality field-type-taxonomy-term-reference field-label-inline clearfix"><div class="field-label">Nationality:&nbsp;</div><div class="field-items"><div class="field-item even"><a href="/taxonomy/term/75" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Switzerland</a></div></div></div>

This:
print render($content['field_review_nationality'][0]);

outputs:

<span thmr="thmr_64"><a href="/taxonomy/term/75" typeof="skos:Concept" property="rdfs:label skos:prefLabel">Switzerland</a></span>

Things like this output nothing:
render($content['field_review_nationality'][0]['taxonomy_term']['tid']);

This is the relevant section from print_r($content); -- I want to only print "Switzerland" without any other code or links:

[field_review_nationality] => Array
                        (
                            [und] => Array
                                (
                                    [0] => Array
                                        (
                                            [tid] => 75
                                            [taxonomy_term] => stdClass Object
                                                (
                                                    [tid] => 75
                                                    [vid] => 11
                                                    [name] => Switzerland
                                                    [description] => 
                                                    [format] => 
                                                    [weight] => 0
                                                    [vocabulary_machine_name] => countries23239
                                                    [rdf_mapping] => Array
                                                        (
                                                            [rdftype] => Array
                                                                (
                                                                    [0] => skos:Concept
                                                                )

                                                            [name] => Array
                                                                (
                                                                    [predicates] => Array
                                                                        (
                                                                            [0] => rdfs:label
                                                                            [1] => skos:prefLabel
                                                                        )

                                                                )

                                                            [description] => Array
                                                                (
                                                                    [predicates] => Array
                                                                        (
                                                                            [0] => skos:definition
                                                                        )

                                                                )

                                                            [vid] => Array
                                                                (
                                                                    [predicates] => Array
                                                                        (
                                                                            [0] => skos:inScheme
                                                                        )

                                                                    [type] => rel
                                                                )

                                                            [parent] => Array
                                                                (
                                                                    [predicates] => Array
                                                                        (
                                                                            [0] => skos:broader
                                                                        )

                                                                    [type] => rel
                                                                )

                                                        )

                                                    [uri] => Array
                                                        (
                                                            [path] => taxonomy/term/75
                                                            [options] => Array
                                                                (
                                                                    [entity_type] => taxonomy_term
                                                                    [entity] => stdClass Object
 *RECURSION*
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

Any ideas on how to get things out of the array? I'm not even sure where my output is coming from since it doesn't appear in the array...

Comments

WorldFallz’s picture

If you don't want the html, then don't use render (that's what it does). So based on the example you provided, you should be able to output just the term itself with:

print $content['field_review_nationality']['und'][0][taxonomy_term]->name;

The structure is all there in the sample you provided.

Z2222’s picture

Thanks for your reply. I tried this, also trying it with single quotes added around 'taxonomy_term':

print $content['field_review_nationality']['und'][0]['taxonomy_term']->name;

I get this error:

Notice: Undefined index: und in include() (line 10 of /home/*****/public_html/*****/sites/all/themes/pt/templates/node--review.tpl.php).
Notice: Trying to get property of non-object in include() (line 10 of /home/*****/public_html/*****/sites/all/themes/pt/templates/node--review.tpl.php).

Update: I found this.

This prints what I want, but also displays an error message:

print $content['field_review_nationality']['#items'][0]['taxonomy_term']->name;

Error:

Notice: Undefined index: taxonomy_term in include() (line 11 of /home/*****/public_html/*****/sites/all/themes/pt/templates/node--review.tpl.php).
Notice: Trying to get property of non-object in include() (line 11 of /home/*****/public_html/*****/sites/all/themes/pt/templates/node--review.tpl.php).
Z2222’s picture

Actually, that error message only showed up once and then disappeared. I think it's working now.

Thanks for your help. If I get it sorted out I will write a tutorial...

Useful Idiot’s picture

Hi WorldFallz :)

I need similar advice. I need to know how to create the right string to display the name of the taxonomy which is BnB Italia in this case.

[2] => Array
                        (
                            [tid] => 4
                            [taxonomy_term] => stdClass Object
                                (
                                    [tid] => 4
                                    [vid] => 2
                                    [name] => BnB Italia
                                    [description] => B&B Italia 
                                    [format] => filtered_html
                                    [weight] => 0
                                    [vocabulary_machine_name] => catalog
                                    [uc_catalog_image] => Array
                                        (
                                        )

Im trying to pass the values from a product page to a webform field, so far the first portion is showing correctly but the second is showing only the number of the taxonomy. Could you help at all in showing me the correct structure for this?

Here is how my link in node--product.tpl.php is formatted.

<a href="http://www.ivanmilite.com/lba/quote?product_name=<?php print $title; ?>&product_manufacturer=<?php print $vid['name']; ?>">Request Quote</a>

For some reason it tells me that $tid is not a defined variable, but it allows me to use vid.
Just so you know, I'm using Drupal 7, Ubercart, and Webforms.

I thank you in advance for any time taken to see if you can help, it's been about 2 days of struggling :/

jeromewiley’s picture

Have you received any help on this?

Since 'taxonomy_term' is an object (see the label there?), you'll have to use "object notation" which is the single hyphen plus angle bracket to produce an arrow.

So, in this case, assuming this is the output of the $content array, it would be $content['taxonomy_term']->name, so your code would read:

print $content['taxonomy_term']->name;