0

Currently its annoying to have to manually parse through the views results data by either doing a print_r() or a dsm() via devel. Does anyone know how to reference a field via PHP when you put in a Global PHP field in either the header or footer of the View?

0

enter image description here

To reference a Views field result here's the format of the variable when using a PHP Global in the footer/header in a Views display:

$results[0]->field_**<FIELD NAME>**[0]['raw']['**<VARIES DEPENDING ON TYPE OF FIELD>**']

This is in reference to a taxonomy id (tid) reference:

$results[0]->field_field_device_type[0]['raw']['tid']

This is in reference to a text field called full_name:

$results[0]->field_field_full_name[0]['raw']['value']

The [0] represents each result row and the last array value usually changes depending on the field type (text,reference,image, etc.) So its better to dsm or print_r() if you really need to reference something that your not sure of.

Here's a sample output of the array structure so you can get an idea of how its laid out:

enter image description here


Here's a better view of the array output of a view's result when you do a print_r() within the header. I had to apply this to another view and forgot how to reference the variables. So here's the full output when you do a print_r($result[0]->field_field_machine_type): (keep in mind it has to be "field_field" when you reference a field.

     Array
(
    [0] => Array
        (
            [rendered] => Array
                (
                    [#markup] => Tablet
                    [#access] => 1
                )

            [raw] => Array
                (
                    [tid] => 105
                    [taxonomy_term] => stdClass Object
                        (
                            [tid] => 105
                            [vid] => 9
                            [name] => Tablet
                            [description] => 
                            [format] => plain_text
                            [weight] => 0
                            [vocabulary_machine_name] => machine_type
                            [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
                                        )

                                )

                        )

                )

        )

)

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.