Drupal - How do I get a block to use a URL to configure a contextual filter?
A views page can read the url and use a path for the page to read a taxonomy term input for a contextual filter. A URL such as example.com/a/term/b
would be read by the view page with a path of a/%/b
. The value for the %
in the URL is passed in to the contextual filter to determine what content to include in the view.
Since a block doesn’t possess a path it is necessary to do a bit more configuration to achieve the result. To do so:
- Click on the contextual filter on the block view form in views.
- Under
When the filter value is NOT available
selectProvide default value
.
If the page in the path is a taxonomy term try this step 3 and 4:
- Choose
Taxonomy term ID from URL
from the Type drop-down box. - Check
Load default filter from term page
.
Or otherwise try this:
- Choose
Raw value from URL
- Identify which part of the URL is the term. eg. for
A/term/B
the term is in the second position.
Then:
- Under
When the filter value IS in the URL or a default is provided
check "Specify Validation criteria" - Select
Taxonomy term
and the specific vocabulary being used. - Under
Filter Value type
pick term name converted to term ID" - Checking
Transform dashes in URL to spaces in term name filter values
will ensure that any auto generated path alias' that have converted spaces to dashes are properly handled. Try your view with this checked and unchecked.
If your view is not appearing on the page troubleshooting steps should include verifying that the view works by testing input in the view preview. Make certain that any spaces are handled to match up with the setting in step 8. Also make certain that your view block is exposed in admin/structure/block
.