I googled for this solution for several hours before devising the solution, so I think it would be handy to have this documented here:
I wanted to filter my results by the value of a CCK field, field_year. I wanted this value to be passed in via the URL as an argument. In Views 2.8, arguments and filters don't talk so we need some trickery.
To achieve this, I created the filter on the CCK field with a static value (e.g., [arg0], it doesn't matter as the value gets replaced in the next step). Then, creating an argument and using PHP as the validation mode, we replace the filter's value with the value of the argument.
foreach ($view->query->where[0]['args'] as &$arg) {
if ($arg === '[arg0]') { $arg = $argument; }
}
And we're done.
Comment | File | Size | Author |
---|---|---|---|
#4 | View configuration | 29.8 KB | aidanlis |
#4 | Validator configuration | 50.05 KB | aidanlis |
Comments
Comment #1
dawehnerDid you created a blog post or a handbook page already?
Comment #2
aidanlis CreditAttribution: aidanlis commentedI haven't done anything other than this, I'm not sure what the procedure is.
Comment #3
drubage CreditAttribution: drubage commentedHow do I add an argument and then use the "PHP Validator"? I don't see that as an option for any arguments. I actually need the argument to search the node body so I think I can use your same replacement technique but I can't figure out what argument to add that will allow me to use the "PHP Validator". Thanks!
Comment #4
aidanlis CreditAttribution: aidanlis commentedSee the screenshots below, hope this helps :)
Comment #5
esmerel CreditAttribution: esmerel commentedComment #6
deng17 CreditAttribution: deng17 commentedIt didn't work for me.
I tried doing it to my "field_series". I copy pasted your code to the Validator in the argument (added as field_series of course) and added a filter for field_series with a value of [arg0]
Comment #7
aidanlis CreditAttribution: aidanlis commentedDupe of #357082: Pull filter value from an argument?
Comment #8
greta_drupal CreditAttribution: greta_drupal commentedWould this work the same with a Taxonomy argument (parent term)? I've tried it and am not seeing it work, but then again I have a lot of moving parts to my view pages. I am sending a url which is contructed of og name + taxonomy parent (of an og taxonomy).
Comment #9
subu.purohit CreditAttribution: subu.purohit commentedHi aidanlis,
I have a cck field Posting date. I added filter and argument according to your steps. But I am getting this error message :
warning: date_create() [function.date-create]: Failed to parse time string ([arg0]) at position 0 ([): Unexpected character in /sites/all/modules/date/includes/date_api_filter_handler.inc on line 322.
I am using views 6.x-2.12. version.
Thanks