Using Multiple Views in a Drupal Block

I was looking for a way to use the output of multiple views in a single block in a custom module. Combined with bootstrap classes and data attributes, this turned out to be very easy to do.

Assuming the views have already been created, the views_embed_view function can be used to grab the rendered output of the view.

The function requires the machine name of the view you want to output and optionally a display id (if the view contains more than one display).

When I hover over the list of views in the views admin screen, it pops up a little tooltip with the machine name.

The following implementation of hook_block_view grabs the output of two different views and combines them into the content of a custom block in the module.

/**
 * Implements hook_block_view().
 */

function ulls_news_events_block_view($delta='') {
  $block = array();

  switch($delta) {
    // Renders both News and Events views into a single block.
    case 'news_events_block' :

        $news = views_embed_view('ulls_news', 'block');
        $events = views_embed_view('events_calendar', 'block_2');

        $block['content'] = <<<BLOCK
<!-- Tabs -->
<ul class="nav nav-tabs nav-justified" role="tablist">
<li role="presentation" class="active"><a href="#events" role="tab" data-toggle="tab">Events</a></li>
<li role="presentation"><a href="#news"   role="tab" data-toggle="tab">News</a></li>
</ul>
<!-- Content -->
<div class="tab-content">
  <div role="tabpanel" class="tab-pane fade in active" id="events">$events</div>
  <div role="tabpanel" class="tab-pane fade" id="news">$news</div>
</div>
BLOCK;
        break;
  }

  return $block;
}

Here's a quick screen capture of the result: