2

I'm attempting to embed a view but need to disable the cache in this instance while leaving it enabled in other instances. I'm attmepting to do this using code but nothing I've tried seems to work. Here is my code so far:

  // Get the view and attach the handlers with the build call
  $view_object = views_get_view($view_name);
  $view_object->set_arguments($view_args);
  $view_object->build($view_display);

  // Manually editing the cache here does nothing
  //$view_object->display_handler->options['cache']['type'] = 'none';

  // Override option breaks the view object and the get plugin does nothing.
  $view_object->display_handler->override_option('cache', 'none');

  // Prints tell me that the previous two lines have not actualy changed the cache method.
  dsm($view_object->display_handler, 'display handler');
  dsm($view_object->display_handler->get_plugin('cache'), 'get plugin cache');

  // Returns the cached copy of the view despite changing the option.
  $view_object->execute($view_display);

Any views experts out there know how to override this cache option?

3
  • I tried just about every variant of the above without success here, so, is punting an option? if so, you could create a clone of the display in the view and override the cache options in there. then call that display here. eg, have a display with caching turned on and another with it turned off.
    – Jimajamma
    Commented Jul 14, 2013 at 15:23
  • Thanks Jimajammy I think your right. Unfortunately this view has 15 displays so it's going to make it a nightmare to maintain. Maybe if I bypass execute and run all the deeper commands manually. Commented Jul 14, 2013 at 18:42
  • found a kludge....still gonna dig around to see where exactly you could reset the plugin options.
    – Jimajamma
    Commented Jul 14, 2013 at 19:05

2 Answers 2

3

ADDITION:

This works:

$view=views_get_view('to_c_or_not_to_c'); // or whatever your name of your view is
$view->display['cached']->display_options['cache']=array('type' => 'none'); // 'cached' is the display name of the view that is normally cached but here we are going to set it to none
return $view->preview('cached'); // returns the rendered html of that display (if you need arguments, they can be passed here as an array in a second argument

ORIGINAL:

May the great drupal gods strike me down for this answer, but....

$view_object->live_preview=TRUE;

will cause drupal to ignore any cache()ing that may be set and a very quick look doesn't seem to show much other side effects (other than some query capturing)

3
  • Well the first suggetion doesn't seam to work for me. but the live preview does. I'll play with both of them some more but this is the clear solution. Thanks. Commented Jul 15, 2013 at 20:35
  • just make sure the display name of your View is correct and not the string 'cached' in the second and third lines above.
    – Jimajamma
    Commented Jul 15, 2013 at 21:44
  • Fair qualifier. Unfortunately I actually did do that. After of course a copy paste test didn't work out of the box :). Commented Jul 15, 2013 at 22:11
0
$handler->display->display_options['cache']['type'] = 'none';
2
  • 1
    Not sure I understand. Where are you starting from? what is $handler revering too here? I tried $view_object->display->display_options. but that didn't seem to do anything Commented Jul 14, 2013 at 1:48
  • I think this will clarify, though this example is creating the view not embedding it: packtpub.com/article/creating-views-3-programmatically Commented Jul 14, 2013 at 19:20

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.