Tag Archives: Drupal Block View

Render block programmatically using Drupal 7

Render block programmatically using Drupal 7 - tutorialHi, folks:)

Render block programmatically - popular topic from google. Today I will tell you a little bit about Drupal Views and how to do that from code. Sometimes we need to render block in our template. For example you created block using Views. You want to place it in the template. In Drupal 7 there are couple of ways to solve this problem. I will show you the 4 methods that will help you render block programmatically. All was checked in Drupal 7.

How to render block programmatically?

Method 1

Let's assume, that you know your view name and block id. If you don't know you can learn how to check that here. In our case view name is sidebar and display id is article_ad. Simplest usage here:

print views_embed_view('sidebar', 'article_ad');

This code will return html markup for our block. Function views_embed_view can take few parameters.

  • name - view name
  • display_id - display id of view we want to render, by default it's 'default'
  • arguments - if you have any parameters you want to pass to block you want to render from code, like contextual filters, you can pass it there via arguments.

Note, that this function does not render block title. If you look to the code of this function you will find out that it's using views_get_view function. It provides more functionality, we will be talking about this approach later (method 4).

Method 2

In second method we will render block programmatically by module_invoke function. Let's look at the example below:

$block = module_invoke('views', 'block_view', 'sidebar-article_ad');
print render($block);

There are 3 parameters, but you can specify more. Just pass more parameters like in previous example to module_invoke function. First two parameters are constant for getting blocks. You have to specify 'views' as a module and 'block_view' as a hook. Next parameter is combination of name and display_id of our block. If you are not sure of the combination, or combination you have provided doesn't work, check it in your database.

In Drupal database there is table called block. In delta column you will find all blocks, that you can use. You can find your block executing query like this

SELECT * FROM block WHERE delta LIKE '%article_ad%'

Find your block in the query results and pass it as 3rd parameter to module_invoke.

Method 3

In 3rd method we will render block programmatically by block_load function. Take a look at example below.

As You can see, there's 'sidebar-article_ad' from previous method. Rest is just copy and paste code.

$block = block_load('views', 'sidebar-article_ad');
print render(_block_get_renderable_array(_block_render_blocks(array($block))));

OK, so here is the method I don't really like. It's hard to pass custom contextual filter value. By default it will take arguments from $_GET. If you know how to do that please write it in comments below:) Any solution will be appreciated:)

So there are two parameters, 'views' and combination of view name and display_id from previous examples.

Method 4

OK, so here is probably most advanced example if you want to render block programmatically. Here you can customize almost every View aspect.

$view = views_get_view('sidebar');
print $view->preview('article_ad');

We are using views_get_view function which returns view object. Take a look at the documentation here:

https://api.drupal.org/api/views/includes!view.inc/class/view/7

You will find there many useful methods, for customization of output, filters and basically everything:P

Getting data from block

OK, now we know how to get html code of your block. But hmmm, what if I need the raw data from my block after passing all contextual filters etc? I don't need html, I will not parse this code via regex or something... I'm not that stupid:P

Not a problem, just use views_get_view_result function. Example usage:

$block_data = views_get_view_result('sidebar', 'article_ad');
print_r($block_data);

It will return you all the data connected with your block. Function takes 2 parameters view name and display_id(block id). After that you can specify additional parameters that will be passed as arguments, like in previous examples.