12

I'm overriding a field template for one of my views and I want to be able to access another field value from the $row variable. The documentation on the template file shows this:

When fetching output from the $row, this construct should be used: $data = $row->{$field->field_alias}

My field is called field_calendar_title, but the following doesn't work as expected:

$row->{$field->field_calendar_title}

I get the following error:

Fatal error: Cannot access empty property...

Is the field "alias" different from the name of the field in the content type?

"Cleansed" output from var_dump of $row:

stdClass Object
(
    [node_title] => ...
    [nid] => 568
    [field_data_field_performance_date_delta] => 0
    [field_data_field_performance_date_language] => und
    [field_data_field_performance_date_bundle] => event
    [field_data_field_performance_date_field_performance_date_val] => 2012-03-02 19:00:00
    [field_data_field_performance_date_node_entity_type] => node
    [_field_data] => Array
        (
            [nid] => Array
                (
                    [entity_type] => node
                    [entity] => stdClass Object
                        (
                            [vid] => 878
                            [uid] => 0
                            [title] => ...
                            [log] => 
                            [status] => 1
                            [comment] => 0
                            [promote] => 0
                            [sticky] => 0
                            [nid] => 568
                            [type] => event
                            [language] => und
                            [created] => 1329332968
                            [changed] => 1331836509
                            [tnid] => 0
                            [translate] => 0
                            [revision_timestamp] => 1331836509
                            [revision_uid] => 1
                            [body] => Array
                                (
                                    [und] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [value] => ...
                                                    [summary] => ...
                                                    [safe_summary] => ...
                                                )
                                        )
                                )
                                [field_resident_company] => Array ( [und] => Array ( [0] => Array ( [tid] => 3 ) ) )
                                [field_series] => Array ( [und] => Array ( [0] => Array ( [tid] => 36 ) ) )
                                [field_venue] => Array ( )
                                [field_rotator_image] => Array ( )
                                [field_exclude] => Array ( [und] => Array ( [0] => Array ( [value] => 0 ) ) )
                                [field_performance_image] => Array ( )
                                [field_premiere] => Array ( [und] => Array ( [0] => Array ( [value] => 1330714800 [timezone] => UTC [timezone_db] => UTC [date_type] => datestamp ) ) )
                                [field_closing] => Array ( [und] => Array ( [0] => Array ( [value] => 1330714800 [timezone] => UTC [timezone_db] => UTC [date_type] => datestamp ) ) )
                                [field_exclude_update] => Array ( [und] => Array ( [0] => Array ( [value] => 0 ) ) )
                                [field_performance_date] => Array ( [und] => Array ( [0] => Array ( [value] => 2012-03-02 19:00:00 [timezone] => UTC [timezone_db] => UTC [date_type] => datetime ) ) )
                                [field_performance_id] => Array ( [und] => Array ( [0] => Array ( [value] => 1436 [format] => [safe_value] => 1436 ) ) ) [field_event_status] => Array ( [und] => Array ( [0] => Array ( [value] => open ) ) )
                                [field_calendar_title] => Array ( [und] => Array ( [0] => Array ( [value] => PSYCHIC EXP [format] => [safe_value] => PSYCHIC EXP ) ) )
                                ...
                        )
) 
10

From the looks of the var_dump you should be able to find your field value with this:

$row->_field_data['nid']['entity']->field_calendar_title['und'][0]['value'];

I really hope there's a better way than that though!

  • Sorry I corrected the syntax in my question and it still didn't work. Tried this too and no luck $row->field_calendar_title; – SomethingOn Mar 15 '12 at 18:16
  • Hmmm...I think from the error message $field is undefined...have you tried doing a var_dump($row) to see what's actually in there? You'll probably spot the field you're after pretty easily :) – Clive Mar 15 '12 at 18:20
  • Hey Clive, yup I did a var_dump and the field exists under _field_data along with the rest of the field values... – SomethingOn Mar 15 '12 at 18:31
  • If you can pop the output of var_dump($row) into your question as an edit I might be able to spot the value you need – Clive Mar 15 '12 at 18:33
  • @SomethingOn I've updated the answer, it's not pretty but it should work :) – Clive Mar 15 '12 at 19:18
7

A simpler approach:

$rendered_field = $view->render_field($fieldname, $view->row_index);

Here, $fieldname is the original (not the view-aliased) fieldname, e.g. 'field_myfield'.

3

Maybe a bit late, but for future reference here's my answer:

The description is pretty vague, but you should literally use:

$row->{$field->field_alias}

In your template file it is also noted that:

Variables available:

  • $view: The view object
  • $field: The field handler object that can process the input
  • $row: The raw SQL result that can be used
  • $output: The processed output that will normally be used.

In $field there's a field called field_alias. So $row->{$field->field_alias} is actually pointing to, in your case, field_calendar_title.

Try making a var_dump of $field and you'll see for yourself.

Hope this helps anyone coming across this post.

Gerben

  • It's never too late, still trending question. 1. I don't want to use monstrous @Clive's example, though it works 2. I do want to understand how to work with given variables: $row, $filed, etc. Your suggestion returns only a number (maybe nid or smth), but how can I access precisely my field. What if I have two different fields that I want to get? – vladkras Jan 23 '14 at 12:37
-2

The simplest way is : $fields["field_name"]->content; Where field_name -> cck field name.

Hope that will help !!

Cheers ! Raj

  • 1
    there is no $fields variable available in field template – Ejaz Aug 20 '14 at 16:53

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.