Drupal 8:

I am altering a view using hook_views_data_alter to display a date range filter.
I was able to create the form to look the way I wanted with hook_form_alter.

It looks like this:

Start Date: (textfield, the value of which is a filter)
End Date: (textfield, the value of which is a filter)

Now, this works and it filters successfully. What I'm having a hard time with, though, is a pair of radio buttons I'm generating in hook_form_alter.

I have two radio buttons. One says 'Date Range', and the other says 'Show All'. I want, when the user has selected 'Show All', for the date range filter to be ignored when the user clicks 'Submit'.

I know there's removeHandler(), but where can I call this? I tried calling it in hook_views_pre_build(), but it doesn't appear to do anything. Any help would be greatly appreciated!

Comments

Lukas von Blarer’s picture

I am facing a similar issue. I am able to remove a filter, but am unable to acces other filter data in hook_views_pre_build(). I created a question on Drupal Answers: http://drupal.stackexchange.com/questions/171331/remove-filter-from-a-vi...

osopolar’s picture

I use the following code to remove a filter which is set for anonymous users but should not be set for authenticated users:

function custom_module_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
  // Remove filter on field_private_content != TRUE for authenticated users.
  if ($view->id() == 'your_view_id' && \Drupal::currentUser()->isAuthenticated()) {
    // Remove private filter on all displays.
    $view->removeHandler($view->current_display, 'filter', 'private_filter');
  }
}
Dinesh18’s picture

How can we add the filter criteria in Views ?