[Drupal] How to print a view in a node in Drupal 6 website?

| | 4 min read

There are many situations where you need to print a view within a node in your Drupal website. A good example would be a portfolio page showcasing the Drupal websites you have done using a view slideshow that showcases screenshots of your work. It is not directly possible to print a view within node just like there is no direct method to print a block within a node. However as Drupal is a very flexible CMS and there are methods to print just about anything and that includes printing a view within a node.

Anyone who is familiar with Drupal knows that Views are an extremely powerful tool that can accomplish a variety of tasks. Views is simply a smart query builder used to create tables, lists and slideshows of data in your website. It has become so useful to the building of a Drupal website that it can lay claim to the title of one of the most downloaded Drupal modules and there is even talk of moving it to the Drupal core.

Theming a node.

Assigning a view to a node comes under theming. So you should have a little info on theming first. Drupal uses the PHP templating system which means that a Drupal theme consists of template files. Template files end with the extension *.tpl.php. To keep it short and simple there is a *.tpl.php file for a page (page.tpl.php) and *.tpl.php file for a node (node.tpl.php). The node.tpl.php is used to theme the content appearing in the node. For eg. the content of the pages and stories and page.tpl.php is used to theme the content in the pages which usually means most of the content around the nodes which includes blocks.

Another factor that you have to keep in mind while theming is that all the processing for getting the block, the code for getting the block should not be present anywhere in the node.tpl.php. The only thing that should be present in the template.php is the code to print the variables that contain the block. This is the Drupal way to write code. It is not a mere philosophy, its a technically sound philosophy. It makes sure that there is a clear distinction between the logic and the theming. This enables the themer to concentrate on the aesthetic aspects of Drupal while enabling the programmer to concentrate on the code

That said, there is no immediate penalty if you do otherwise. You could easily write the logic for getting the block in the node.tpl.php file and it will work just as fine. But what happens if you want to change the theme? You have to rewrite the code into the new theme and if somebody else has to do that it makes their job harder than it should be.

So where do you actually write the code?

Every Drupal theme has a template.php file or it ought to have one. Thats where you place all the logic for your theme. The template.php file contains many functions aptly named preprocessing functions. Since we are concerned with the preprocessing for a node we will have to place the code within the mytheme_preprocess_node() function. This function takes care of all the preprocessing before a node is loaded

Printing the view within node.tpl.php

Before setting out to print the view you have to create the view. In Drupal 6 you have to download the Views module and enable it to do that. Next create the block view that your wish to print. Following is the code for getting the view to be written in the template.php file.

function mytheme_preprocess_node(&$vars) {
  //Printing the images view  block inside the node
  $images_view = views_get_view('my_block');
  $images_view_block = $images_view->execute_display('your_view_block',array($node->nid));
  $vars['block_variable'] = $images_view_block['content'];
}

As you can see the views_get_view() is the function that is used to get the view you wish to print in the node. It takes in the machine name of the view as the arguments. If we need to pass any arguments to the view, like say passing the node id to the view we can pass it in the execute display function as an array of arguments. This is useful if you want to load only the images related to that node when that node is loaded. 'your_view_block' is the machine name of the block of the view. Finally the view is passed into the $vars array as 'block_variable'.

With the preprocessing now over all we need to do is print the view in the node.tpl.php. Within the node.tpl.php file inside a suitable wrapper div write.

print $block_variable;

If everything is in order, you will have the view within your node.