Drupal 8 - Ignore contextual filter

Korrigan Source

On a search view, linked to search api and facets, I want to add contextual filters on my content type. This content type have referenced entity fields, linked to taxonomy term (one field to taxonomy from destination vocabulary, the other to taxonomy from activity vocabulary)

So, I have created 2 contexual filter, one for each "taxonomy field".

view

filter

But, I appears only the first filter (destination) is applied. If my taxonomy term from URL is a destination, the view displays right results. But if it's an activity, it display all contents. So I supposed there is a problem with contextual filter validator : 'Action to take if filter value does not validate' should have something like 'Ignore filter' option, because with 'Display all results for the specified field', its shows everything, but don't execute next filter.

Anyone has a solution ?

Thanks a lot

drupaldrupal-viewsdrupal-8drupal-contextual-filters

Answers

answered 6 months ago Korrigan #1

Finally, I found a solution, just altering view in pre_build

/**
* Implements hook_views_pre_build().
*/

function my_module_views_pre_build(ViewExecutable $view)
{
    if ($view->id() == 'tour_search' && $view->current_display == 'tours_taxonomy') {

        $tid = reset($view->args);

        if (! $tid) {
            return;
        }

        /** @var Term $term */
        $term = Term::load($tid);

        if ($term->getVocabularyId() === 'activities') {
            unset($view->argument['field_tour_destination']);
            return;
        }

        if ($term->getVocabularyId() === 'destinations') {
            unset($view->argument['field_tour_activity']);
            return;
        }

        return;
    }
}

I moved validation logic in this hook, by unsetting filter in with a simple term vocabulary test.

Hope it will helps somebody !

comments powered by Disqus