Blocks do not have paths so how can I pass a context to the block to get the correct results for a page?
2 Answers
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
.
-
I have two taxonomy vocabularies. Collections coll1, coll2 and coll3 Sizes small, medium and large My URL looks like this: collections/coll1/small I tried to use the method you describe but I get no results. Any idea? Commented Jan 26, 2012 at 15:14
-
I would need more information about your view. Why not add a question and describe which fields, filters, contextual filters you have set up and we'll figure it out.– AshlarCommented Jan 26, 2012 at 16:23
If you want to select according to a Content Type instead of taxonomy, you need a different setup. Try the following:
- Click on Contextual Filters
- Add Content: NID On the WHEN THE FILTER VALUE IS NOT AVAILABLE section, select Provide default value and choose Content ID from URL on the dropdown list.
- On the WHEN THE FILTER VALUE IS AVAILABLE OR A DEFAULT IS PROVIDED section, select Specify validation criteria, choose Content on the VALIDATOR select box, and choose which Content Types you authorize to be displayed.
That should do it.
-
Validation passes but it is not filtering the NID? Do I need to create a relationship to the contextual filter? Commented Aug 31, 2015 at 17:31