0

I am currently struggling to alter the output of a field inside a specific view template. I have a Taxonomy term I want to render using a <h3> over the default <div> that Views default gives us, and to accomplish this a Field template needs to be implemented, that encapsulates this field with said <h3>.

Okay, not a problem. But when I work with the template's row output, it gets hairy. I can't find a catch-all print statement that drills down into the field (which is a taxonomy term value) and retrieves the #title attribute.

$data = $row->{'field_field_article_type'};
print_r($data);

The Print_R of the $data, which itself polls the field inside each row statement is as follows:

Array
(
    [0] => Array
        (
            [rendered] => Array
                (
                    [#type] => link
                    [#title] => Health
                    [#href] => taxonomy/term/18
                    [#options] => Array
                        (
                            [entity_type] => taxonomy_term
                            [entity] => stdClass Object
                                (
                                    [tid] => 18
                                    [vid] => 3
                                    [name] => Health
                                    [description] => 
                                    [format] => 
                                    [weight] => 0
                                    [vocabulary_machine_name] => content
                                    [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
                                                )

                                        )

                                )

                            [attributes] => Array
                                (
                                    [typeof] => typeof="skos:Concept"
                                    [property] => property="rdfs:label skos:prefLabel"
                                    [datatype] => 
                                )

                        )

                    [#access] => 1
                    [#printed] => 1
                    [#pre_render] => Array
                        (
                            [0] => drupal_pre_render_link
                            [1] => drupal_pre_render_markup
                        )

                    [#markup] => 

I've tried a few variations to access the variable, but it typically returns null and with the mix of StdClasses and Arrays, its becoming tumultuous to access.

Does anyone here know any surefire ways to theme a field output? Am I going about this in the wrong manner? Should I alter an additional theme call in Views to accomplish this simple goal?

0

You should be able to do that inside the view itself instead of using a template. Click on the field of the term. Scroll down a little you will see Rewrite results. Expand that and click Rewrite the output of this field. Then expand replacement patterns and you will see tokens that are able to be used. Your rewrite would look similar to this:

<h3>[field_term_field_name]</h3> 

Your Answer

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

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