2

Here's my simple scenario :

  • Execute view with exposed filter (min & max dates)
  • If there are no results, execute again with min date = 0 to get all results

Unfortunately, the query static cache will execute the same query twice.

$view           = views_get_view('my_view');
$view->set_display('default');

$date       = $view->get_item('default', 'filter', 'created');
$date['value']['min']   = date('Y-m-d', $date_from);
$date['value']['max']   = date('Y-m-d', $date_to);
$view->set_item('default', 'filter', 'created', $date);

$view->pre_execute();
$view->execute();
$results = $view->result;

$view->destroy();

// do logic here to determine if view needs to be queried a second time

$view           = views_get_view('my_view');
$view->set_display('default');

$date       = $view->get_item('default', 'filter', 'created');
$date['value']['min']   = date('Y-m-d', 0);
$date['value']['max']   = date('Y-m-d', $date_to);
$view->set_item('default', 'filter', 'created', $date);

$view->pre_execute();
$view->execute();
$results2 = $view->result;

$view->destroy();

// $results and $results2 are identical

If I comment the first part of the code, the second query will work correctly and $results2 contains a lot more results.

I've tried answers from this question [1], but no luck.

The only solution I found is to clear the entire static cache with drupal_static_reset();

How to elegantly solve this problem? I believe clearing only the static cache for the queries will solve this, but I fail to find the good $name argument.

[1] Clear views cache after node inserted in Drupal 7

0

2 Answers 2

1

The second argument to views_get_view is $reset:

If TRUE, reset this entry in the load cache.

So second time around you just need to pass that:

$view = views_get_view('my_view');
$view->set_display('default');

...

$view = views_get_view('my_view', TRUE);

That will reset the static cache and the data will be fresh.

1
  • It looked promising, but unfortunately it has no effect in my case
    – ddev1
    Commented Jun 26, 2019 at 13:44
0

Here's the correct static cache to clear in order to solve the issue :

drupal_static_reset('views_exposed_form_cache');

I guess since the exposed filters are not supposed to change during a request, the developpers did not anticipate this.

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.