Add field data to the result set for easier access.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

casey’s picture

Something like:

$row->field_${field_name}[$row->node_language][0]['value'];

Or maybe even (when does a field contain data for multiple languages?):

$row->field_${field_name}[0]['value'];

Or

$fields->${field_name}[0]['value'];

Any ideas/preferences?

johnv’s picture

The shorter, the better :-)
With D7 you can translate a node per field, so a number-field will only have the untranslated version/original languge; a textfield will be available in 3 languages. I do not know how this is 'published' in the $data, though.
I would the prefer $row->${field_name}[0]['value']; (I skipped the '_field'-part). Is it possible to have the language as an option, e.g. $row->${field_name}['en'][0]['value'] would work, too?

I have three alternatives:
- use the conventions from the css-files, e.g. views-view-field.tpl.php; ideally you can publish existing data, or 'mimic' it.
- use tokens/ replacement patterns instead of own variables.
- mimic the Views-field 'Global: Math expression'. I suppose it is from Views3 itself. I uses tokens/replacements patterns like: [entity_id_5] . I don't like the unclear name of this, but using Views, I'll have to get used to it.

marta_yo’s picture

Issue tags: +undefined variable
robidigital’s picture

FreeFox’s picture

+1

molave’s picture

subscribe

farald’s picture

Subscribe:)

lightstring’s picture

Subscribing

gillesv’s picture

Subscribing

johnv’s picture

Casey cannot provide more data as long as Views isn't passing it through..
FYI, Issue #1063418: views_get_view_result drupal 7 returns "node" for fields gives some background and tips from the Views-guys.

akagroundhog’s picture

Subscribing.

Now we have to use $data->_field_data['nid']['entity']->my_field['en'][0]['value'] or $data->_field_data['nid']['entity']->my_field['en'][0]['safe_value'] to get the field value. Plus, this code works only in the "Output code" section. $row->my_field['en'][0]['value'] looks definitely better.

TripX’s picture

Subscribe

sachbearbeiter’s picture

subscribe

rv0’s picture

subscribe

casey’s picture

Jippii, we have a views_handler#post_execute() now. I can fix this now. Hold tight.

guybedford’s picture

Does hold tight mean a week or a month?

Would really appreciate so I can know whether I should take this advice or not!!

(Specifically in relation to this bug, which may or may not be a duplicate of this - http://drupal.org/node/1140896)

geerlingguy’s picture

Subscribing from #1140896: Variable $row does not contain correct values ($data->_field_data does) - right now, for certain fields, I need to access the information inside the $data object, which is only available properly in the output field. It feels hackish to do everything inside that field...

carlos.macao’s picture

subscribe

ealtman’s picture

subscribe

annikaC’s picture

subscribe

kenianbei’s picture

+1

jayhariani’s picture

Sub.

guile2912’s picture

subscibing.
Unfortunatly php filtering is quite useless without that.
I am using a huggly hack to get around this : put the params in the title and hide it, but at least I can access title content in the php filter.
*ashamed*

pjcard’s picture

sub

timtunbridge’s picture

FileSize
124.58 KB
32.62 KB

I am posting this as it may help other non-coders like myself.

I arrived here as a result of not being able to reference fields using the notation (for example) $row->field_client. This is despite it being listed under available variables (see Available Variables screenshot). The workaround to this is elsewhere in these threads for which I am hugely grateful, but it wasn't readily apparent to me how to make this work in my situation. Here is what I did ...

I started by trying to find out the internal names of everything in my view - for this I firstly entered the following into the Output code field:

<?php
 print var_export($data, TRUE); 
?>

This gave me a rather scary result at first but what I needed was in there (see Output Data screenshot). I have always struggled with how to read these types of results but eventually I was able to deduce that the notation I needed to reference my Client field was:

$data->_field_data['nid']['entity']->field_client['und'][0]['value'];

This returns the value of the Client field which in the case of the screenshot example would be ABB Grain.

Hope this helps someone. If there is a better way of achieving the same then please do share this.

Tim

oobie11’s picture

I was trying to use the suggestion in #25, but it was not working. I hd to make the quotations double instead of single. Also, print var_export($data, TRUE); does not work and gives an ajax error. I had to use "var_dump" instead. But looking at this did not help me, considering I had no idea what I was looking at.

partyp’s picture

Thanks to #25 and #26 I was able to find my variable as well. For me I ended up getting the variable with the exact same code as

$data->_field_data['nid']['entity']->field_client['und'][0]['value'];

but I replaced the "field_client" part with my custom field.

goldlilys’s picture

#27, that long line works, but it's not completely a fix to why we can't just call $row->fieldname without all that hassle. Because that would make the "Available variables" for this module useless.

Yup, the shorter the better.

trevorkjorlien’s picture

I've followed the instructions for #25 on some fields that are integers and text, and it's worked great! Thanks.

My problem now is that I want to print the values of a term reference field. I've made a taxonomy with a few terms a user can choose, and used a term reference to access them in my content type. The field is called "PV", so my output code looks like:

<?php

print $data->_field_data['nid']['entity']->field_pv['und'][0]['value'];

?>

Following the same steps above in #25, the values don't appear. It just comes up blank. Any idea on how to get them to print? (Also, I know that I can simply add them in the usual Views way, but I need to do something a bit more complex, and I think this is one of the first steps).

ardnet’s picture

subscribed

johnv’s picture

I t might have been solved by Views itself: since views 7.x-3.0-rc1 [#1192186] , the following function exists:

Added ::get_value() method to all fields to make it easier for systems like services that need to access raw field data. It should now be much more consistent.

I now used this (somewhere else, not in Views PHP yet):

foreach ($records as $row_index => $row) {
  foreach ($this->view->field as $key => $field) {
    $value = $field->get_value($row);
    $value = isset($object_value[0]['value']) ? $value[0]['value'] : 0 ;  // this would be nicer in 1 line.

    ... use $value...
  }
}
mmilo’s picture

I ran into this problem too, and there are alot of duplicate issues.

Using #1140896: Variable $row does not contain correct values ($data->_field_data does) as the main one.

mmilo’s picture

Status: Active » Closed (duplicate)
arekanderu’s picture

subscribe

break9’s picture

this worked for me BUT**** it only woks if you use the "Output code". Will not work in the "Value code" section.

<?php
print $data->_field_data['nid']['entity']->field_your_field['und'][0]['value'];
?>
iwerksom’s picture

The solution in #25 also works for term reference fields. Just beware that you should change the code accordingly. It is not always you should write ['value'] at the end.

My field was named tags and the code that worked:

print $data->_field_data['nid']['entity']->field_tags['und'][0]['tid'];

If you can get it to work. Then look at the output data as mentioned in #25. The picture in the post marks what lines you should be looking for.

mxh’s picture

When you want to use the Value Code field section 'the more proper' way, I'd recommend to load your needed field data by using field_get_items() provided by Field API or with field_extract_value() provided by field_extract module.

Todd Young’s picture

Have we accepted the workaround as the only option here? Discussion seems to have tapered off...

Todd Young’s picture

*ping*

pipep’s picture

#35 does the trick for me on D7

jordan8037310’s picture

#25 saved me a lot of time, thanks timtunbridge!

note for people struggling with views that aren't nodes, make sure that you change nid to the appropriate referral in the array. I was working with profiles so i had to use pid.

Cheers

joegl’s picture

Issue summary: View changes

Having the same issue as #35. The $data object is not getting set in the "Value Code" section which is required for PHP sorting.