drupal-7drupal-viewsdrupal-exposed-filterdrupal-contextual-filtersdrupal-views-relationship

How to display results tagged with just one term ID when there are multiple filters


I have a blog in Drupal 7 and used Views to display the fields. I have 2 sets of tags displayed on the sidebar:

1) SHAPES

Triangle

Square

Rectangle

2) COLOR

Red 

Blue 

Green

I created the 2 groups of tags via Structure > Taxonomy > Vocabulary > Add Term

For the View, I created one View with two exposed filters (similar to what was done in this tutorial)

VIEW:

Path: '/blog/tag'

Filter Criteria:
    1) 'Content: Shape (exposed)'

        Exposed form in Block: 'Yes'

        Filter identifier: 'shape_id'

    2) 'Content: Color (exposed)'

        Exposed form in Block: 'Yes'

        Filter identifier: 'color_id'

Exposed form style: BEF

The exposed filters are set to the appropriate block region in Structure > Blocks.

If I click 'triangle', only blog posts tagged with 'triangle' should be displayed. Then if I click 'red', only posts tagged with 'red' should be displayed.

It looks like the correct blog posts are being displayed when I click on the terms. The URL gets appended with IDs from both vocabularies, but I can't seem to get the right combos to display.

Desired URL when 'triangle' is clicked: '/blog/tag/?shape_id=1&color_id=All'

Desired URL when 'red' is clicked: '/blog/tag/?shape_id=All&color_id=1'

So, essentially the results should display items tagged with just one term ID.

However, how it is currently working is that if I click 'triangle', and then 'red', both 'triangle' and 'red' have the 'selected' class (are both bolded) and the url is: '/blog/tag/?shape_id=1&color_id=1'

The only way I was able to get the desired URLs is if I have the '-Any-' option displayed. However, the use case I have is to NOT have '-Any-' listed in the list of tags.

Is this even possible without '-Any-' listed in the list of terms? I have never used multiple exposed filters before, so any guidance would be great as I've researched this for almost a week now. I read in another post that contextual filters would allow me to create separate path aliases that are clean urls, but I have not been successfully able to do this.

Any help would be greatly appreciated. Thank you for your time.


Solution

  • I tried using the taxonomy term pages approach, but could not figure out how to get the fields displaying properly. The fields displayed were based on the teaser in my Blog content type. And using exposed filter option was not a solution since my use case was to only display results based on one tag. What worked for me:

    1) Add new view to existing Blog View

    2) FORMAT: 'Unformatted list' SHOW: 'Fields'

    3) FIELDS: [add fields that you need displayed]

    4) FILTER CRITERIA: 'Content: Published (Yes)'; 'Content: Type (= Blog)'

    5) PATH: '/blog/[YOUR VOCABULARY NAME]/%'

    6) CONTEXTUAL FILTERS: 'Content: Has Taxonomy Term ID'

    WHEN THE FILTER VALUE IS NOT IN THE URL: 'Display all results for the specified field'

    WHEN THE FILTER VALUE IS IN THE URL OR A DEFAULT IS PROVIDED: select 'Override title' and in the input field, enter: '[YOUR VOCABULARY NAME]/%1'

    select 'Specify validation criteria'

    Validator drop-down, select 'Taxonomy Term'

    Vocabularies: select appropriate Vocabulary name

    Filter value type drop-down: 'Term name converted to term ID'

    select 'Transform dashes in URL to spaces in term name filter values'

    Action to take if filter value does not validate: select appropriate one that works for your needs (in my case: 'Display contents of "No Results found"')

    If you need to display the term name in the heading on the results page, in the same view > HEADER > Add 'Global: Unfiltered Text' > then in text area, add something like "VIEWING RESULTS FOR [YOUR VOCABULARY NAME] / %1"

    Save the View. Since I had multiple filters, I cloned the above view and renamed everything. So, I ended up adding 3 additional views to my existing Blog View.

    I had previously set URL aliases for Taxonomy Term Paths, so I removed these

    Also, I had to go back to my Taxonomy Terms individually and delete the URL alias that was generated from the pattern

    To have the list of tags displaying on the right with the desired URL structure of /blog/[YOUR VOCABULARY NAME]/[YOUR TERM NAME], I created a separate new view:

    SHOW: 'Taxonomy Terms' of type '[YOUR VOCABULARY NAME]'

    Create a Block

    'Unformatted list' of 'Fields'

    FIELDS: 'Taxonomy: Term Name' > click this and unselect 'Link this field to its taxonomy term page'. Then expand "Rewrite Results" section and select 'Output this field as a link' > in the Link path: 'blog/[YOUR VOCABULARY NAME]/[name]' (the [name] at the end of the url structure should appear as you see it with the square brackets) > select 'Replace spaces with dashes' > Transform the case to 'lower case' > click Apply

    FILTER CRITERIA: 'Taxonomy vocabulary: Machine Name (= [YOUR VOCABULARY NAME])'

    Save the View

    Then in Structure > Blocks, place the block in the appropriate region

    Hope this helps someone!