0

Is there a way to programmatically remove a contextual filter from a view? I have a view where, at times, I want to display it without the contextual filter active. This is the code I tried.

function MYMODULE_views_pre_view(&$view, &$display_id, &$args) {
  if ($view->name == 'some_view') {
    unset($view->args);
  }
}

I also tried the same code within hook_views_pre_execute().

How can I achieve this?

0

To remove a contextual filters just set the $args to empty array or unset the desired arguments unset($args[0]) to remove the first argument an so on: Here is an example to remove all contextual filters:

/**
 * Implements hook_views_pre_view().
 */
function MYMODULE_views_pre_view(&$view, &$display_id, &$args) {
  if ($view->name == 'some_view') {
    // Remove all filters.
    $args = [];
  }
  • Thanks. This removes the args being set, but doesn't actually remove the filter. Meaning if I go into the View and actually click Remove, I get a different (desired) result than just settings $args to an empty array. So I may need to just create separate views and display them conditionally in code, which is fine. I'll most likely accept this as the answer, though. – Micah May 29 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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