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.

CommentFileSizeAuthor
#4 View configuration29.8 KBaidanlis
#4 Validator configuration50.05 KBaidanlis
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dawehner’s picture

Did you created a blog post or a handbook page already?

aidanlis’s picture

I haven't done anything other than this, I'm not sure what the procedure is.

drubage’s picture

How 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!

aidanlis’s picture

See the screenshots below, hope this helps :)

esmerel’s picture

Assigned: Unassigned » esmerel
deng17’s picture

It 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]

aidanlis’s picture

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

Would 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).

subu.purohit’s picture

Hi 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