Drupal is a fairly flexible system as far as CMS applications go, and is even more flexible as a development platform. The Views module gives developers ways to dynamically build queries of data, and display that data in many different ways. Sometimes, however, you want to display views in ways not supported through the admin interface. Fortunately, there are other ways to get the job done.
For example, I have a block in my sidebar where I want to display a list of the latest blog post, latest forum topic, and latest user poll. Each of these lists can be created as separate views. However, there doesn't seem to be any functionality, either in the Views module or another third-party module, that allows you to display multiple unrelated views in a single block. So, we turn to the code. The Views API provides a mechanism for building and displaying your views anywhere in your templates:
<?php views_get_view($name_of_view); ?>
<?php views_build_view($type, $view, $view_args, $use_pager, $node_limit); ?>
Back to my example - I simply created a new block, and set the input format to PHP. Then, I used the following code to populate the block with the views:
<?php
<h2>Latest Blog Post:</h2>
<?php echo views_build_view('embed', views_get_view('latest_blogpost'), null, false, 1); ?>
<h2>Latest Forum Topic:</h2>
<?php echo views_build_view('embed', views_get_view('latest_forumtopic'), null, false, 1); ?>
<h2>Latest Poll:</h2>
<?php echo views_build_view('embed', views_get_view('latest_userpoll'), null, false, 1); ?>
The above code shows the rendered view. However, there are times where you want or need to do some additional processing on the values returned by the view. Fortunately, the Views module is incredibly powerful, and has a ton of options for retrieving the views.
In the above example, the first argument to the views_build_view() function is 'embed'. There are several other options:
This information comes directly from the code, and hopefully, it will be of some assistance to people who are looking for this kind of functionality.
[tags]block, cms, development, drupal, php, templates, Views[/tags]
Note: I've found that when
Submitted by RTaylor (not verified) on Tue, 12/04/2007 - 18:43.Note:
I've found that when using the 'embed' option, it appears to use $view->page_type (That's the dropdown select under Page, View Type when editing the view) to determine what view type to use (teaser, full nodes, list, table, etc.), even if you have not checked "Provide Page View" in the views admin console ($view->page = FALSE).