I'd like the ability to change a View's filter value prior to rendering, specifically add a taxonomy filter to certain pages. The reason I need this, is to have one Views block, and the ability to filter based off the URI, without adding a contextual taxonomy id to the URI. I haven't been able to find a way to handle this in any hook, but I think that's where I should be looking. I've been using the MODULE_views_pre_view hook without any luck.

Right now, the only way I've gotten around this is to duplicate the Block over and over again, and it's starting to get messy.

I've found several articles, but they're all for Drupal 7.


1 Answer 1


I've figured out the problem after lots of guessing. You'll need to use a contextual filter and use the hook_views_pre_view hook in a custom module to alter the $args variable.

function MODULENAME_views_pre_view($view, $display_id, &$args) {
    if ($view->id() === 'view_machine_name') {
        $args[0] = 'your value';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.