I have a page view that display one node. Then there is an attached view which displays other nodes. Now I want to modify the query of the attached view to not display the node of the first view. The problem is that this:

function recticel_views_pre_execute($view) {
  if ($view->name == 'references' && $view->current_display == 'attachment_1') {
   // var_dump($view->current_display);
   $aview = views_get_view('references');
   $aview->execute('page_1');
   var_dump($aview->result); exit; 
  }
}

generates infinite recursion. If I manually set the current_display it won't recurse but I won't get a result either. This code works fine when called from somewhere else than a view hook.

Is there any way to render a view display from the same view in a views hook?

Comments

jax’s picture

If I set 'accept attachments' to false it works:

function modulename_views_query_alter(&$view, &$query) {
  if ($view->name == 'references' && $view->current_display == 'attachment_1') {
    $aview = views_get_view('references');
    $aview->set_display('page_1');
    $aview->display_handler->definition['accept attachments'] = false;
    $aview->execute('page_1');
    $query->where[0]['clauses'][] = 'node.nid <> ' . $aview->result[0]->nid;
  }
}

In other hooks I don't get any results back. Is this the way this should be done?

jax’s picture

You need to pass the arguments back if you're using that:

function modulename_views_query_alter(&$view, &$query) {
  if (in_array($view->name, array('references', 'news')) && in_array($view->current_display, array('attachment_1', 'attachment_2'))) {
    $aview = views_get_view($view->name);
    $display = reset(array_filter($view->display[$view->current_display]->display_options['displays']));
    $aview->set_display($display);
    $aview->display_handler->definition['accept attachments'] = false;
    $aview->args = $view->args;
    $aview->execute($display);
    if (count($aview->result)) {
      $query->where[0]['clauses'][] = 'node.nid <> ' . $aview->result[0]->nid;
    }
  }
}
merlinofchaos’s picture

Status: Active » Closed (won't fix)

My assumption here is that you have attachment_1 set to attach to page_1 which of course will cause infinite recursion, as you see attachment_1 running, try to run page_1 which then attaches attachment_1 and then you see attachment_1 running, ad infinitum.

So, uh, other than saying "Don't do that" I don't really have any good ideas.