3

I have two views that are block displays. I'm trying to embed these views in a block. When I include the block nothing is displayed. If i add "test" to $block['content'] it displays test in the block body.

Here is my code ($gid is an organic group id as an argument to the block):

  case 5: // Create Mugshot block
    $viewName = 'display_most_wanted';
    $display_id1 = 'block_1';
    $args = array($gid);
    $wanted = views_embed_view($viewName, $display_id1, $args);

    $display_id2 = 'block_2';
    $convicted = views_embed_view($viewName, $display_id2, $args);

    $block['subject'] = 'CWPA OG Mugshots';
    $block['content'] = $wanted . $convicted;         
    break;

  default: 
    // Throw an error
    break;
}

return $block;

2 Answers 2

2

Your problem is the arguments are not getting passed properly

function views_embed_view($name, $display_id = 'default') {
  $args = func_get_args();
  array_shift($args); // remove $name
  if (count($args)) {
    array_shift($args); // remove $display_id
  }

  $view = views_get_view($name);
  if (!$view || !$view->access($display_id)) {
    return;
  }

  return $view->preview($display_id, $args);
}

So you want $wanted = views_embed_view($viewName, $display_id1, $args[0], $args[1], $args[etc]);

Also from the Creator of Views

You either want:

views_embed_view('viewname', 'page_1', 1, 2, 3)

or

views_embed_view('viewname', 'page_1', '1+2+3')

depending on if you have a single argument expecting multiple nids or multiple arguments each expecting a single nid.

1
  • Thank you, worked as soon as I changed my statement to use $args[0] instead of $args.
    – Arosboro
    Jul 18, 2011 at 20:28
3

It seems like instead of rendering the views programmatically you could also leverage the Views "Attachment" display . The allows you to attach a view to another display, in other words render one view after another. The "block_1" display of the view you posted above would be the main view displayed as a block, and then you would change the "block_2" display to an attachment and configure the settings similar to the image below.

enter image description here

This would eliminate the need to create custom code to display both views in a block.

2
  • I've already tried this, it nests the attachment within the first block. I need them side by side in the code.
    – Arosboro
    Jul 18, 2011 at 20:27
  • 1
    Understood. I would assume you could display them side-by-side by overriding the view's template or writing some CSS. My design skills are non-existent, though, so take it with a huge grain of salt. Jul 23, 2011 at 14:36

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.