Drupal tutorial: Embedding a view

tagged with

At some stage almost everyone running a website based on Drupal will find that they want to add a view somewhere on the site where it can't easily be done. Most often, views can be added to blocks to accompany content types or pages and the block visibility rules can be used to tweak who gets to see the view and when. Views can easily be added to panel pages, and most have their own dedicated page anyway. Despite all this, you might still need to know how to add a view manually...

Luckily, Drupal provides a PHP function that can be used to embed a view anywhere on the website. Before we start adding code snippets all over the show, remember that some views require a context in which to operate. For example, a view may require a node ID to be passed as an argument and this could lead to unexpected behaviour if the view is embedded somewhere where that argument is unavailable. It's important to think out all the possibilites beforehand to avoid disappointment.

The great thing about using this PHP function is that you really don't have to know anything about PHP programming - it's more a case of cutting and pasting and matching a few variables to suit your view. Here is the function:

views_embed_view($name, $display_id = 'default')

In this case:

  • $name - The name of the view to embed.
  • $display_id - The display id to embed. If unsure, use 'default', as it will always be valid.

Used in context, and assuming there is a view called blogs with a display id of block_1, we can print out the view using the following PHP statement:

<?php print views_embed_view('blogs', 'block_1'); ?>

To embed any view somewhere on your own site, simply copy and paste the above statement into the page and change blogs to match the name of your view, and block_1 to match the name of a page or block in that view. If you're unsure of what the display id is for a particular view, then viewing the source code while browsing the view will give you the answer.

The class atrribute that houses the view will always denote the display id. In the above case, it was shown as:

view view-blogs view-id-blogs view-display-id-block_1 view-dom-id-2

As you can see, one of the classes tells us that the view-display-id is block_1. To prove this all works, I will embed the above statement directly in here for you to view the results. Here we go:

Other people also read

How to build a webpage for SEO and Internet marketing
How do you make a webpage that stands out from the crowd, drives and converts traffic and helps to...
Depending on what you need from a website, you might have a lot or very little work to do on the...
One of the most common requirements for today's website is the ability to sell something. Most...
Being able to accurately control the size and dimensions of images is vital for any modern website...
In addition to the enhanced Drupal API search features, the Drupal toolbar also received two new...


Et voila! Remember that you must have the input or text format set to allow PHP in order for this to work within a node. There's nothing stopping you from using this method anywhere on your website (bearing in mind the considerations mentioned at the start), although you can often avoid having to use PHP snippets by using what Drupal already provides - blocks, panels, etc.