Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
The normal way a view gets values for contextual filters is by the view path. The easiest way of doing this is to append filter values to the path, separated by slashes. It is also possible to have contextual filter values within a path. This is done by using a percent sign where the view expects a contextual filter value in the path of the view (for example user/%/comments).
Having contextual filter values inside paths is particularly useful when creating menu tabs, which is the subject of the next section.
Comments
Passing arguments to block views for contextual filter
I spent hours and hours and hours.. maybe days on this problem.. so thought I could help others out in how to achieve this. Not sure if it's the right way to do this..
I have a page with several fields as well as the block that loads my view with the contextual filters set in it. Basically, I wanted to pass a field value (which was a taxonomy term/id) to this contextual filter.. here's what to do.
1. Click add contextual filters and specify the taxonomy term you want to filter by.
2. In contextual filters, in the WHEN THE FILTER VALUE IS NOT AVAILABLE, click provide default value.
3. Under type, select PHP.
4. In "Php contextual filter code" write this code:
$node=menu_get_object();
return $node->field_your_file_name_here['und'][0]['tid']; (where "field_your_field_name_here" is the 'machine readable name' of the field on your page).
This is the taxonomy id that I have in my content.
5. In the WHEN THE FILTER VALUE IS AVAILABLE OR A DEFAULT IS PROVIDED, click 'specify validation criteria', validator = taxonomy term, filter value type = Term ID.
Voila - the taxonomy field's id on the page (that loads this block) gets passed to the block's taxonomy contextual field requirement (set in step 1).. Hope that helps..
Thank you
This helped me immensely, thanks for posting your solution!
Thanks, just one question.
Thanks, just one question. When I get to Step 5, and I click "Specify Validation Criteria", nothing else pops up. How do I finish step 5 (validator = taxonomy term, filter value type = Term ID.)
Thanks
may the gods smile on your life
Oh freak1321
you have finally provided me with the taxonomy based contextual filter holy grail solution I was seeking for the past 3 days, pulling hair out in large clumps, and alas, right here, the answer i was looking for...thankyou thankyou thankyou
Great!
Uow, I finally learned to do this!
Tanks :)
This is not safe
I'm with all of you; and still pulling hair out. I've been trying to do this for 3 days and counting.
However, I believe inserting PHP into your database is a bad idea for security and performance reasons.
Has anyone else come up with a solution that's purely done through the interface?
Solution using URL on StackExchange
Hi,
This article is relevant: http://drupal.stackexchange.com/questions/27818/contextual-filter-with-a... .
It describes how to obtain a filter value from the URL without executable PHP. It's now working for me!
How from exposed filters URL to contextual filters for block?
I have a Views map page which correctly shows markers filtered by Destination and Category through exposed filter criteria of the kind: /en/full-map?field_geographic_hierarchy_tid%5B%5D=18&field_location_type_tid%5B%5D=1
Under that map I want to place a block that uses the provided term-ID's (in bold) to create a LIST that shows the same selection as on the map.
My guess was to achieve that through "Contextual Filters" (block is same view as page). I have tried several options under "configure contextual filter - when filter value is NOT available" but couldn't work it out. I am not familiar with PHP, so I didn't try it, but now I have the feeling "something like" your solution should be possible.
Contextual Filters just have to be told to read those term-ID's after the:
?field_geographic_hierarchy_tid%5B%5D=
and
&field_location_type_tid%5B%5D=
BTW I have the impression those "%5B%5D" make it even more complicated.
- EDIT: (I now discovered %5B is "[" and %5D is "]"
Tried with
$node=menu_get_object();
AND
return $node->field_geographic_hierarchy_tid[]=['tid']; (no list)
return $node->field_geographic_hierarchy_tid[]=; (non list)
return $node->field_geographic_hierarchy_tid[]; (screen in white)
return $node->field_geographic_hierarchy_tid%5B%5D=['tid'];
return $node->field_geographic_hierarchy_tid%5B%5D=;
return $node->field_geographic_hierarchy_tid%5B%5D;
Just in case also tried them as fixed values (without the colon).
To be honest I don't even know what that PHP or Fixed Value option is used for:
- to strip part of the URL so the authentic tid becomes "free" to use?
- or do they make some interpretation of the whole URL and then calculate and return the value of tid?
Tnx
I started with Drupal in 2007 and then my life got stuck...