0

I want to prevent an attached view from running depending on some coding parameters.

I've tried using hook_views_pre_view() to prevent it from running, but I can't figure out what options to set on the view to accomplish that. Tweaking attached or rendered doesn't seem to do anything.

function ding_eresource_views_pre_view(&$view, &$display_id, &$args) {
  if ($view->name == 'eresource_list' && $display_id == 'attachment_1') {
    // Prevent rendering if parent view has some option set.
    if (!empty($view->old_view[1]->barr)) {
      // This does not work. How can it be done?
      $view->attached = FALSE;
      $view->rendered = TRUE;
    }
  }
}

How can it be done?

1
  • In my case, the view display I wanted to conditionally prevent from showing was a block display, so I was able to use hook_block_list_alter() to pick it out and remove it at that point. That way views didn't touch it at all.
    – m4olivei
    May 13 '14 at 18:53
3

$view->executed = TRUE; worked for me but I'm not sure it is a correct way to stop Views from rendering.

1
  • Hmm, that does work, after a fashion. Regrettably, it shows the View's empty text instead, but I worked around this by removing the empty text for the attached view.
    – mikl
    Jun 19 '12 at 14:40
4

Wanted to do similar but just $view->executed = TRUE; made the query in the view still run. Looking at view::build(), I understood you need the following code, to prevent anything running further in the view (displays and query).

$view->executed = TRUE;
$view->built = TRUE;

To make it clearer, the code you should use is the following one.

function ding_eresource_views_pre_view(&$view, &$display_id, &$args) {
  if ($view->name == 'eresource_list' && $display_id == 'attachment_1') {
    // Prevent rendering if parent view has some option set.
    if (!empty($view->old_view[1]->barr)) {
      // This does not work. How can it be done?
      $view->executed = TRUE;
      $view->built = TRUE;
    }
  }
}
0

Options for hook_views_pre_render:

$view->result = array();

That destroys the array, preventing any data from rendering.

Or, the plain PHP option:

die();

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.