I am currently working on a website that requires a view that should be filtered based on time. It is for a radio station. I have a view that is suppose to show the program that is currently broadcasting. I use a content type that has a basic integer field for the hour. I've tried to use the php filter to do the following.

$current_hour = date('G');

$hour = $data->field_field_program_schedule_shour[0]['raw']['value'];

if ($hour == $current_hour)
{
return false;
} else {
return true;
}

I should point out that I have tried using the variables that are given in the list, but they do not work. I thought the "$data->field_field_program_schedule_shour[0]['raw']['value']", which does work in a php field, would work in the filter also, but no matter what I try, I can't get the value of any of the cck fields.

My question is how can you get the value of the cck fields into the php filter textarea field and use that value to filter the view.

I am using Drupal 7.

CommentFileSizeAuthor
#22 1222448-filter-field-22.patch5.63 KBfizk
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

drupalnesia’s picture

Try this:

$node_program = node_load($data->nid);
$hour = $node_program->field_program_schedule_shour[0][value];
clare-ux’s picture

I have the same issue. I have added the following code to the filter and its not working. It should be right?

$sc=$_GET["sc"];
$client_account = $data->_field_data['nid']['entity']->field_customer_account['und'][0]['value'];
if ($sc == $client_account)
{return false;}
else {return true;}

How can we pull the data from a field? I know there are issues with getting the correct content using the variables, but I thought we pulled in using the above syntax rather than the variable it would work. Any suggestions on creating filters would be appreciated, since I can't get anything to work in this field!

iancawthorne’s picture

You could try:

$node = node_load($data->nid);

Then use the $node info.

johnv’s picture

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

After hours of struggle, #1 worked for me, but not #4. The solution proposed in #1140896: Variable $row does not contain correct values ($data->_field_data does) seems to only work for Views PHP as a field, not a filter.

turingfan’s picture

I'm sure this was mentioned before somewhere, but after banging my head over this issue I figured out how to use the Devel module to debug just the "PHP filter code" variables by calling "dpm($data);"

I found that $data and $row don't contain the same fields as they do in the "PHP field output".
So for now, I'm using the only useful variable $data->uid which as the user ID of record I want to evaluate.

<?php
$account = user_load($data->uid);		

$term_loc= field_get_items('user', $account, 'field_location');    
 
$term = taxonomy_term_load($term_loc[0]['tid']);

if( isset($term) && $term->name <> $static['city'] ){  
    return TRUE;       // filter out this row
}
?>
AaronELBorg’s picture

...

gregory_kapustin’s picture

Hi guys,
I'm having the same issue, and using a node_load seems to me a little bit bad for performances...

Which is the weirdest is that we already have a few data gathered from the DB at this stage : nid, node title...

Thus, for my part I build most I can in Setup Code, in order to obtain, at the end, an array of nids that I put in $static, allowing me to do only if(!in_array($row->nid, $static)) {return TRUE;} in the row (Filter Code).

This often requires some db_query in the process : that's where I save the node_load pain.

But still not as good as this issue was solved :D

So, subscribing :)

gregory_kapustin’s picture

Status: Closed (duplicate) » Active

By the way, I'm setting this issue back to active, because it's indeed NOT a duplicate, as said in #5 : we can access what we want when the PhP is as a field, not as a filter.

steveoriol’s picture

This bug it's a pain, i hope it will be fix soon :)

ChrisValentine’s picture

Hope so too - using PHP in filters is turning out to be a right pain.

$row certainly doesn't work - so ignore what it says in the "Available variables" dropdown.

$thisData=$data->_field_data['nid']['entity']; - works just fine in a field but causes errors in a filter.

$thisNode = node_load($data->nid); - works in a field but also works in a filter

Woot!

monaw’s picture

Doing a node_load() is a big performance hit since you have to reload each node again!

PLEASE FIX!

matthieu_collet’s picture

Yeah this bug is awful :(
in my case node_load() doesn't work because I'm with a file, and additional fields added with media :( impossible to retrieve....

agerson’s picture

Issue summary: View changes

Also worked around it with node_load.

idkelly’s picture

Same problem here. I was able to get around it with node_load as mentioned several times above, but have noticed that pagers then don't work properly. Pagers seem to count items filtered out. i.e:

- paging is set to 10 items per page, but only 8 are shown
- entire pages will be blank

imclean’s picture

Same issue: #1222448: Views PHP Can't Filter

I've marked that one as a duplicate as this one is older with more people following it.

Renee S’s picture

Confirmed. The $row values are there, but that's it. $data is empty.

In Global:PHP for field value, the value also is missing the pertinent view information, again: $row values are there, but $data is empty. Only in the output box on the filter are all the values present as advertised.

imclean’s picture

vulfox’s picture

Priority: Normal » Major

I just used about 6 hours of trying to get this to work.

What do you think.. Is this module something that could be used?

Alternatives?

vulfox’s picture

Category: Support request » Bug report
CProfessionals’s picture

I tried #1 and could not get it to work so I applied the following with the 'und' and all is working... Thanks for all who contribute

<?php
$node = node_load($data->nid);
$zipcode= $node->field_zipcode['und']['0']['value'];
If ($zipcode == "USA-11784") { return FALSE;} else {return TRUE;};
?>
fizk’s picture

Version: 7.x-1.x-dev » 7.x-2.x-dev
Status: Active » Needs review
FileSize
5.63 KB

Here's a patch, please let me know if it works for you.

ron_s’s picture

Thank you for posting about this. The same issue exists if attempting to filter entities.

For example, if custom entities have been built using Entity Construction Kit (https://www.drupal.org/project/eck), the only way to filter using Views PHP is by performing an entity_load for each row. One alternative is to do the filtering in a template file (views-views-fields-my-view-name.tpl.php), however this does not work for modules such as Entity Reference that wrap the results with additional code after templating is complete.

As mentioned by @monaw in #12, this is a really bad performance hit. I'd like to test @fizk's patch, but unfortunately we're committed to Views PHP 7.x-1.x at the moment. Any chance a 1.x version will be created soon? Otherwise we'll try to test once we have a bit more free time.

fizk’s picture

@ron_s I want to avoid making these changes to 7.x-1.x because many sites are use to the way it works, for better or for worse.

ron_s’s picture

I certainly understand. Will probably be a few weeks until we have time to do any testing of the patch. Thank you for your efforts!

nattyweb’s picture

Looks like the experiences described above are similar to my own. My Views Filter using Views php is simply not working.

In case I've done something wrong, though, I would appreciate it if someone can cast an eye over the following code, used as a filter, that fails. When I use an equivalent as a php field, and use 'print' instead of 'return', everything appears to work as it should. I'm wondering if that is because I've added fields before this one that exposes the two fields shown below. I don't get errors with the filter - it just has no effect.

$home_exp = $data->field_field_home_sub_expires[0]['raw']['value'];
$group_exp = $data->field_field_subscription_expires[0]['raw']['value'];
$home_exp = new DateTime($home_exp);
$group_exp = new DateTime($group_exp);
$today = new DateTime('now');
if($home_exp > $today || $group_exp > $today){
  return TRUE;
}else{
  return FALSE;
}

Comments much appreciated!

fizk’s picture

@maranjo Have you applied the patch in #22?

nattyweb’s picture

@fizk originally had problems applying patch but that was my inexperience not your patch. Finally applied it using the command:

patch -p1 < 1222448-filter-field-22.patch

Doesn't seem to make any difference though - filter still not working. Would the use of:

$home_exp = $data->field_field_home_sub_expires[0]['raw']['value'];

be correct? Is there a way of testing the value at this point to make sure it's valid?

Martin

fizk’s picture

@maranjo Yes, if you have the devel module installed, you can look at the contents of $data and $row using dpm($data), and dpm($row).

nattyweb’s picture

Fabulous - patch #22 works. Thank you so much @fizk - you are a life-saver!
Martin

nattyweb’s picture

An update for @fizk. Although the filter works with this patch, if I add any php code as a field, the display doesn't show: just a white screen. This is true even if the php code has this content:

<?php
dsm ($data);
?>

Is it possible the fix has interfered with the display of normal php code fields?

vulfox’s picture

Maybe you should not use surrounding php tags? Don't remember exactly

fizk’s picture

@maranjo I'm not sure, can you send me (email or attach here) your exported view?

nattyweb’s picture

@fizk thanks for the assistance and advice. I can confirm that your patch #22 does work correctly for me.

My issue relating to adding other php code was a red herring - it related to a complexity in my Views query that conflicted with the data structure. No wonder there were no clues in my exported view...

Great module, great support - thanks very much.

fizk’s picture

Status: Needs review » Reviewed & tested by the community

@maranjo You're welcome.

Setting to RTBC based on #34.

eigentor’s picture

I had a similar issue. A note to everyone that writes a filter function into a custom module and uses this function for the filter.
I had my function

function my_function($data) {
(...)
return TRUE;
}

I used it in the PHP filter code field like this

my_function($data);

This does not work. You have to write:

return my_function($data);

Though it said return TRUE in my function in the end, you have to return the function result once more. That may be the way a return statement works.
So check this if the problems may have nothing to do with field data but with pure syntax.

  • fizk committed c520371 on 7.x-2.x
    Issue #1222448 by fizk: Views PHP Can't Filter
    
fizk’s picture

Status: Reviewed & tested by the community » Fixed

Committed to 7.x-2.x. Thanks everyone!

Status: Fixed » Closed (fixed)

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

Arun Guddeti’s picture

[update:] with the @fizk update on the module in #37 the below mentioned issue works.

I'm having the same issue for taxonomy terms showing undefined when used in PHP filter, In PHP fields it works fine though.
I'm trying to access the #markup value.
although i can find the taxonomy name in two places when i look at $data array.

$data->field_field_taxonomy_name[0]['rendered']['#markup'];
$data->filed_field_taxonomy_name[0]['raw']['entity']->name;

both of these contains the taxonomy name.

But if I give $data->field_field_taxonomy_name; it's showing, Notice: Undefined index: value in __lambda_func() on this line of code in modules\contrib\views_php\plugins\views\views_php_handler_filter.inc(82) : runtime-created function).

fab971’s picture

#8 works for me, thank you very much.