Drupal: Calling Views In Code
- October 11th, 2007
- Posted in Drupal . PHP . Technology
- Write comment
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); ?>
and
-
<?php views_build_view($type, $view, $view_args, $use_pager, $node_limit); ?>
Clearly, the views_get_view() function retrieves the view definition from the database, while the views_build_view() function renders it.
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:
-
<h2>Latest Blog Post:</h2>
-
<h2>Latest Forum Topic:</h2>
-
<h2>Latest Poll:</h2>
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:
- page - Produces output as a page, sent through the theme. The only real difference between this and block is that a page uses drupal_set_title to change the page title.
- block - Produces output as a block, sent through the theme
- embed - Use this if you want to embed a view onto another page, and don't want any block or page specific things to happen to it.
- result - Returns an array of information, including a database object that you can use db_fetch_object() on
- items - Returns an array like resul, however, it contains an array of objects found by the queries, so you don't have to fetch the objects yourself.
- queries - returns an array, summarizing the queries, but does not run them
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 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).