Programmatically Output a Views Block in Drupal 6

One of the best things about developing in Drupal 6 is the almost limitless ability to hook into core functions, contributed modules, and being able to tailor Drupal to meet your individual needs. For me, a lot of these awesome features involve the ability to programmatically utilize Drupal core modules, such as Views and CCK.

In one instance, it became necessary to create a new page template and to display a view block within it. Since the block contained arguments and there was no good way to utilize Drupal’s Blocks menu to place the block onto the particular pages I needed it, I opted to go for the programmatic route. This presented two challenges:

  1. How do I actually display the view?
  2. How do I pass arguments from the page to the view?

Through looking at Views documentation, the correct function to use in order to embed a view on a page is:

views_embed_view($name, $display_id);

$name is the name of the view and $display_id is the actual id of the block. These can be found by going to Site Building -> Blocks -> List. Mouse over "configure" on the block you wish to programmatically display, and look at the last argument. You will see something akin to "Viewname-block_1". "Viewname" is $name, while "block_1" is the $display_id.

Now, depending on whether or not you are using a URL Alias or a system generated URL to deliver your arguments to the block, you will have to grab your arguments in two different ways

System Generated URL

Simply utilize the arg function to grab the necessary arguments and pass them to Views. For example, let's take the URL http://www.example.com/office/containers. Use arg(0) to grab the first argument, which in this case would be "office". Use arg(1) to grab the second argument, and so on. In this instance, you would pass your two variables to the function as follows:

views_embed_view($name, $display_id, arg(0), arg(1));

URL Alias

Before passing arguments, you first need to extract the URL path from the current view. This is because even though the URL Alias may be in the URL, Drupal still loads the system URL. You can place the following function in your template.php and retrieve URL alias arguments as an array:

function returnAliasArgs() {
	$url = drupal_get_path_alias($_GET['q']);
	return explode("/", $url);
}

Assuming we assign returnAliasArgs to the $args variable, we would pass variables to the function as follows:

views_embed_view($name, $display_id, $args[0], $args[1]);

Now you can place a Views block anywhere in your code and pass any arguments to it that you deem necessary.

About the Author

Ben has been seriously working with websites since he was about 15, when he created a dynamic website to list results and user profiles for the Cross Country and Track and Field community in his county. Ever since, he has been enamored with web...

 
blog rss banner
 

Categories

View All

Testimonials

"Dear Mike - I wanted to take a moment to thank you and the Unleashed Team for such a wonderful experience with our new partnership.  Your team has been incredibly dedicated to making our...

Kelly Bedsole
Metro Offices

“Unleashed Technologies is a pleasure to work with. We needed an unusual, non-cookie-cutter web site on a very tight schedule, and Unleashed Technologies delivered.

Paul Jakubik
PureDiscovery Corporation