Advanced: Using views_build_view to control your own views

Below is the views_build_view function comment.

This builds the basic view.

@param $type
  'page' -- Produce output as a page, sent through theme. The only real difference between this and block is that a page uses drupal_set_title to change the page title.
    'block' -- Produce output as a block, sent through 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' -- return an $info array. The array contains:
      query: The actual query ran.
      countquery: The count query that would be run if limiting was required.
      summary: True if an argument was missing and a summary was generated.
      level: What level the missing argument was at.
      result: Database object you can use db_fetch_object on.
    'items' -- return info array as above, except instead of result,
      items: An array of objects containing the results of the query.
    'queries' -- returns an array, summarizing the queries, but does not
      run them.

@param $view
   The actual view object. Use views_get_view() if you only have the name or vid.

@param $args
   args taken from the URL. Not relevant for many views. Can be null.

@param $use_pager
   If set, use a pager. Set this to the pager id you want it to use if you plan on using multiple pagers on a page. To go with the default setting, set to $view->use_pager. Note that the pager element id will be decremented in order to have the IDs start at 0.

@param $limit
   Required if $use_pager is set; if $limit is set and $use_pager is not, this will be the maximum number of records returned. This is ignored if using a view set to return a random result. To go with the default setting set to $view->nodes_per_page or $view->nodes_per_block. If $use_pager is set and this field is not, you'll get a SQL error. Don't do that!

@param $page
    $use_pager is false, and $limit is !0, $page tells it what page to start on, in case for some reason a particular section of view is needed,

@param $offset
   If $use_pager == false, skip the first $offset results. Does not work with pager. without paging on.

@param $filters
   An array of exposed filter ops and values to use with the exposed filter system
   Array has the form:
     [0] => array('op' => 'foo', 'value' => 'bar'),
     [1] => array('value' => 'zoo'), // for a locked operator, e.g.*   If no array is passed in, views will look in the $_GET array for potential filters

My own embedded views

davea - January 18, 2007 - 03:18

I created the view and tested them to ensure I got the right results, including all fields.

The view in this example is named "myCustomView", created through the view interface.

Then, in my page, I inserted the following, including the php tags:

<?php
   
// show the custom view
   
$view = views_get_view('myCustomView');
    print
views_build_view('page', $view, array(), false, false);
?>

I don't know what all the arguments mean. Maybe this is enough to get you started.

Dave_From_Texas

example with filter parameter

victorkane - January 22, 2007 - 10:38

I have tried this and it works perfectly.

Of course, it brings back the entire set of the list specified in the view.

What I would really like to see an example of (something I haven't been able to manage yet) is the use of views_build_view which returns a subset of the list based on a filter.

For example, suppose I have created a view that has an exposed filter based on a cck added integer field, which is greater than. For example, a cck created node which 'company' with the added integer field 'number of employees'. Or a picture gallery node with number of hits, or votes.

How do I specify the equivalent of an interactive exposed filter value using the views_build_view() function?
Any help here would be spectacular, and would be a big push for views itself.

Victor Kane
http://awebfactory.com.ar

With the third argument you

chrisd - January 29, 2007 - 06:57

With the third argument you can pass parameters (an array of parameters). To pass 1 argument with the value 3:

<?php
$view
= views_get_view(10);
if (!
$view) { // Just in case your view with ID 10 does not exist anymore....
   
drupal_not_found();
    exit;
  }
$content = views_build_view('embed', $view, array(0 => 3), $view->use_pager, $view->nodes_per_block);
print
$content;
?>

So my guess is that to pass a filter value, you need to pass it as an argument to your view (not 100% sure about this).

Tech. articles with a WAMP+IIS+Drupal focus

thanks! I was looking for

gemini - February 6, 2007 - 18:19

thanks! I was looking for something similar, but instead of args I used filter:

$view->filter[0][value] = "3";
print views_build_view('embed', $view, $view->args, $view->use_pager, $view->nodes_per_block);

dynamic views filter in a block?

Mojah - April 4, 2007 - 22:35

Can you explain how you got the above code to work? Does it also work in a block?

I'm trying to display a context sensitive view by setting its filter according to which page (nid) is being viewed. I have the view embedded in my template page (page.tpl.php) and wish to set this views filter dynamically. Here's what I have (which does not work).

$myview = views_get_view('page_quotes');
$myview_args = array();
$myview->filter[3][value] = "frontpage";
print views_build_view('block', $myview, $myview_args, false, 4);
}

dynamic views filter

Mojah - April 8, 2007 - 22:03

Here's something useful that may save a day of searching...

This can be used to embed a view in a template page and pass variables to a view argument. One can use this to display a view in relation to the node id or whatever desired context. One may use this method to create a generic view and pass variables to the view argument in order to output content related to context.

A simple example where this can be used: To display related random quotes on different areas of a site. Lets say you have a cck (content) type called quotes. One of the of the cck fields is a list type text field where a user, when creating a quote node, selects from the list which area of the site the quote should appear on.

A view can be created with filters set for this content type and an argument can be added related to the cck field. The view can then be embedded into a template page and the view argument can be dynamically set. I used this instead of having to create 12 different views for different areas of the site. Taxonomy was not an option so cck was used instead. Here's an example...

<?php
// you may use an if else statement to set the variable
// to be passed to the view argument based on nid
if ($is_front) {
$viewarg = 'your_variable';
} elseif (
$node->nid = '20') {
$viewarg = 'your_variable2';
} else {
$viewarg = "your_default_variable";
}
// you may call the view using its id or name:
// views_get_view("your_view_name")
$myview = views_get_view(2);
//set the view argument $args[0] to your_variable before building the view
$myview_args = array(0 => $viewarg);
// instead of block you may use page or embed
// block and page will display the view according to the
// your block and page options set when creating the view
// 4 sets the number of nodes to display
// false sets the pager to false
print views_build_view('block', $myview, $myview_args, false, 4);
}
?>

Further reading....
Inserting views into a template page: http://drupal.org/node/48816
Defining and passing views: http://drupal.org/node/129532
How to insert multiple views in a node: http://drupal.org/node/85720
Context sensitive embedded views: http://drupal.org/node/124446

keywords: dynamically filter view, pass arguments to embedded view

Pass-in URL, otherwise problems!

JirkaRybka - July 14, 2007 - 21:50

If you have any Exposed filters on the embedded View, you need to pass-in a correct URL, otherwise the Submit button on the filters will break (i.e. point only just to the View, so after clicking the button you'll get it NOT embedded inside your original page).

The correct way is for example like this:

<?php
$view
= views_get_view('myCustomView');
$view->url = 'node/$arg';
print
views_build_view('page', $view, array(0 => 3), false, false);
?>

The value for $view->url is worth a little discussion. Basically it's just the URL to your page (for example 'node/###'), where the Submit should be directed again, to have the resulting view embedded exactly where the original was.
The tricky part comes into play, if the third argument to views_build_view() is used for filtering: In this case, Views are re-encoding its value into the URL, which is rather strange in this case, and currently (Views 5.x-1.6-beta5) results in these limitations:
--- You cannot pass url-alias
--- The arguments must be present in the URL
--- The arguments must be replaced by the Views' typical placeholder '$arg'

So this is why I didn't use 'my_url_alias' nor 'node/###', both these will get another '/###' appended by Views. The final url after submitting the filters in my example will be 'node/###&filters......', where ### is the number 3 taken from the array.

Drupal 6 Views missing views_build_view() at present.

checksum - June 18, 2008 - 23:22

This will most likely be resolved once the views module is completed.
Work around:

<?php
function views_build_view($view_name, $args = array()) {
 
$view = views_get_view($view_name);
 
$view->set_arguments($args);
 
$view->execute();
  return
$view->result;
}
?>

Hope this helps someone.

In Views 2 / Drupal 6 you

mlsamuelson - June 25, 2008 - 14:03

In Views 2 / Drupal 6 you can use views_embed_view() in place of views_build_view().

Doesn't take all the same arguments. It works like this:

<?php
print views_embed_view($viewname, $display_id = 'default');
?>

Of course you can supply different $display_id's, but default is a good option since it'll always be available, and not all views may implement a block or page display.

$viewname is just a string representing the name of the view.

mlsamuelson

 
 

Drupal is a registered trademark of Dries Buytaert.