hook_form_alter() for views_exposed_form runs whether or not the exposed filters form is going to be delivered from cache. Since I'm doing some heavy calculations for each parameter (calculating the results if the parameter is added), it would be nice if during hook_form_alter I could know if the views_exposed_form form is going to be delivered from cache as to avoid the expensive calculations.

This is what I've come up with so far by looking at view.inc.

function views_depth_counts_form_alter(&$form, &$form_state, $form_id) {

  if($form_id == "views_exposed_form") {
    
    $view = $form_state['view'];
    // Taken from view.inc line 709
    if (!empty($view->live_preview)) {
      $cache = FALSE;
    }
    else {
      $cache = $view->display_handler->get_cache_plugin();
    }
    if ($cache && $cache->cache_get('results')) {
      vpr('Used cached results');
    }
    else {
      // Do some heavy calculations
    }

$cache->cache_get('results') returns NULL here even if it doesn't later in view.inc. I see that in views_plugin_cache::cache_get() calls views_plugin_cache::get_results_key() and I think the discrepancy is in what $this->view->build_info gives to $key_data on in views_plugin_cache.inc on line 252 which results in an incorrect key generation during hook_form_alter().

Ideally if a cached version of the exposed form was going to be used, the views_exposed_form wouldn't even be built. Perhaps that's an easier problem to fix but would require a more intimate knowledge of Views 2 internals than I have right now.

Any pointers from the Views gurus?

Comments

R.J. Steinert’s picture

Status: Needs work » Active
StatusFileSize
new911 bytes

Here's a patch that calls view::render_exposed_form() again during view::execute() when it is known that a cached version is not going to be used. The patch also sets a flag view::cache_rebuilding right before it calls view::render_exposed_form() so that you can check for it in hook_form_alter().

I'm not really proposing this patch as a fix, but it helps get the job done if you look for the cache_rebuilding flag in hook_form_alter().

function views_depth_counts_form_alter(&$form, &$form_state, $form_id) {

  if($form_id == "views_exposed_form" && $form_state['view']->cache_rebuilding) {
    // Do stuff
  }

}
R.J. Steinert’s picture

Status: Active » Needs work
dawehner’s picture

Status: Active » Needs work
+++ b/includes/view.incundefined
@@ -1,5 +1,5 @@
+// $Id: view.inc,v 1.167.2.7 2010/03/22 20:55:11 merlinofchaos Exp $

ups, this shouldn't be done here. Please checkout git and apply the patch there

+++ b/includes/view.incundefined
@@ -715,8 +715,14 @@ class view extends views_db_object {
+      dpm('Used cached results');

please remove this :) not everyone has devel installed.

+++ b/includes/view.incundefined
@@ -715,8 +715,14 @@ class view extends views_db_object {
+      ¶
+      ///////////////////////////////////////////////////////////////////////
+      $this->cache_rebuilding = TRUE;
+      $this->exposed_widgets = $this->render_exposed_form();

If such things are done, it really needed to be documented, explained and not wrapped with //////

R.J. Steinert’s picture

@Dereine Sorry if there was any confusion, the patch is really just an example of how I'm accomplishing what I need to get done and not necessarily what I think a solution should be. For example, it would be ideal if view::render_exposed_form() didn't have to run twice but the impression that I got from the documentation in view::render_exposed_form() is that there are other parts that depend on it being run as early as it is.

So if this is to be made a priority, it seems to me that we have to figure out how to allow view::render_exposed_form() to run later or figure out how we can know that we are going to used cached results earlier. But, then again maybe we do have to run view::render_exposed_form() twice when not using cached results and it is an acceptable amount of extra processing. I'm still getting familiar with Views internals. What do you think?

dawehner’s picture

It's actually possible to build the form array and render it later, but only with ctools.

kars-t’s picture

Status: Needs work » Fixed

Hi

I am closing this issue to clean up the issue queue. Feel free to reopen the issue if there is new information and the problem still resides. If not please make sure you close your issues that you don't need any more.

Maybe you can get support from the local user group. Please take a look at this list at groups.drupal.org.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.