I'm trying to get the values of some field variables that are in a collection using php code in a PHP Custom Box in Views. Views suggests I have these variables to get the values:

$row->field_name.

This works for fields not in a collection, but in a collection, I get some weird number which is likely some sort of ID for the collection (all fields return this number).

How can I get the actual value of the field in PHP? I want to output something based on the value, but not the value itself, so I need to run some code on it.

CommentFileSizeAuthor
#6 field_collection_duplicatio.png4.97 KBChrisValentine

Comments

Juryiel’s picture

Status: Active » Needs review

I was able to figure this out using the Devel module. I used the dsm() command, as dsm($data) in the php code of the view and found that if you add the field collection field with name NameOfField to the view with teh appropriate field collection relationship, then its data is stored in:

$data->field_field_NameOfField[0]['raw']['value']

and this will return the correct value in the PHP box. I'm not sure if there's a better way, or if there are issues with this for different field types (I was using booleans here) so I'll mark this as 'needs review' but it works for my purposes.

tim.plunkett’s picture

Status: Needs review » Fixed

That seems fine.

directory1182’s picture

I receive the following error:

Notice: Undefined property: stdClass::$field_field_youtube_url in __lambda_func() (line 1 of \sites\all\modules\views_php\plugins\views\views_php_handler_field.inc(131) : runtime-created function).

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

isramv’s picture

Comment #1 worked for me thanks

ChrisValentine’s picture

StatusFileSize
new4.97 KB

I can only get the solution in #1 to give a single value. I see mention of setting a relationship but if I add one, I get duplicates of my containing records.

My collection is called: field_hydro_prop_readings
It contains a single float numeric field called: field_river_hydroprop_time

I will have up to three of these per containing content type record - I want to extract and use all of those values in a Global: PHP field in a view.

The array dump that comes back from $data->field_field_hydro_prop_readings seems absurdly complex.

$data->field_field_river_hydroprop_time[0]['raw']['value']; gives me nothing.

The annoying thing is that even without a relationship, if I add "field_hydro_prop_readings" as a field in my table, I get all three correct values of field_river_hydroprop_time for that record (admittedly with some other formatting stuff that I can't get rid of completely) so I know the data is there - I just need to grab the values in PHP (so I can do an average).

pmol123’s picture

I have two work arounds for this type of problem by the Drupal 7 Views PHP field/filter not working.

(1) I added a computed field to the bundle and do my calculation there.

For example, I needed to take an action by whether a Unix timestamp was set (and > 0) OR not set. This was kinda link using the NULL and NOT NULL in the pre-drupal SQL days of build web apps.

(2) Use Views Templates and over ride the "display", "row" and "fields", then add my code that is needed on the output.

For example, I was storing a URL in a field. I needed to change a url-parameter before another process would load it after calling the view. I found that over riding the views template for that field , making the one line (str_replace) command worked great.

Note: Some changes, executions won't display in the preview or before saving.

Patrick