3

I have some views of display page which I do not want to show to public if there would not be any result when accessed without any exposed filters set. I have a custom views access plugin and a custom access checking class which can handle the access granting.

My idea so far is that I would like to run the view a first time during access checking without exposed filters. In this way I could find out if the view would ever deliver any results. I tried it in the following way:

  $view = Views::getView($view_id);
  $view->setDisplay($display_id);
  $view->setArguments([$politician_id]);
  $view->execute();
  $result = $view->total_rows;

The problem is: as soon as an user comes to this page with query parameters in the url set by exposed filters these exposed filters are used for this view query as well. I did not find any way to tell the class in $view to execute without exposed filters. I even tried to set these exposed filters data in the following ways but even then the filter data in the query parameters is still used to filter the result set:

  $view = Views::getView($view_id);
  $view->setDisplay($display_id);
  $view->setArguments([$politician_id]);
  $view->initDisplay();
  $view->initHandlers();
  $view->setExposedInput([]);
  $view->exposed_data = [];
  $view->exposed_raw_input = [];
  $view->execute();
  $result = $view->total_rows;

I want to achieve that in this way because I want to use this logic for several views. It would be so smart to simply re-use the arguments and filters set in the views instead of hardcoding a new custom database query as the settings in the views might change. I hope someone can help me.

2
  • 2
    One workaround could be to have a master display without any exposed filters. – leymannx May 7 at 18:19
  • 1
    In setExposedInput() I would set at least a dummy value. If completely empty it's filled with the query parameters from the request. – 4k4 May 7 at 19:53
1

Thanks to 4k4 (see comments). The solution is to set some dummy exposed filter data:

  $view = Views::getView($view_id);
  $view->setDisplay($display_id);
  $view->setArguments([$politician_id]);
  $view->setExposedInput([
    'dummy' => ''
  ]);
  $view->execute();
  $result = $view->total_rows;

In this way it works.

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.