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?

  • 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 Jul 14 '13 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. – danielson317 Jul 14 '13 at 18:42
  • found a kludge....still gonna dig around to see where exactly you could reset the plugin options. – Jimajamma Jul 14 '13 at 19:05
up vote 3 down vote accepted

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)

  • 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. – danielson317 Jul 15 '13 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 Jul 15 '13 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 :). – danielson317 Jul 15 '13 at 22:11
$handler->display->display_options['cache']['type'] = 'none';
  • 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 – danielson317 Jul 14 '13 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 – Anne Bonham Jul 14 '13 at 19:20

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.