If you are investigating the use of the Views module, and would possibly like to embed content from a view into regular node content, there are a couple contributed modules that can help you out:
- The Insert View module acts as a filter in Drupal that can be enabled for various input methods.
- The Viewfield module adds on a field type for your CCK nodes, and allows you to select the desired view.
A more advanced option is to embed a View by hand with PHP. This can be done in the node edit page (if you are using an input method with the PHP evaluator filter enabled) or via PHPTemplate in your theme's .tpl.php files.
First, we would use
views_get_view($view_name) to create a view object, then we'd use
views_build_view($type, &$view, $args = array(), $use_pager = false, $limit = 0, $page = 0) to display it. Note: both of these functions are provided in views.module, please see that file for additional usage documentation.
Basically, all that
views_build_view requires is a type value (we'll be using 'embed') and a view object. The optional variables include an array of views arguments, an option to output the view in pages, how many results to return (or how many results to return per page), and which of the returned pages to start on.
As a simple example, I can edit a node and embed a view into it with the following code:
//load the view by name
$view = views_get_view('sample_view');
//output the view
print views_build_view('embed', $view);
As a more complex example, let's assume that I want to embed a view on all nodes of a specific type but have different results returned based on the node's title. One way to do this would be to place the following code in your node-type.tpl.php file:
//load the view by name
$view = views_get_view('faq_topics');
//output the top three items in the view with the node title as an argument
print views_build_view('embed', $view, array($title), false, 3);
You can also build a view in different ways:
- 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.
- Produce output as a block, sent through theme.
- 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.
- 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.
- Return info array as above, except instead of result, items: An array of objects containing the results of the query.
- Returns an array, summarizing the queries, but does not run them.