Hi there,

I first tried posting this question about invoking a view using PHP to the forums, but had no luck. I hope you'll be able to help. I love the insert view module, and I'm looking forward to seeing it for D6!

Here's the question, as posted to the forums:

In Drupal 5, I used to use the great module Insert View quite a bit to put the contents of a view into the a node. Insert view is almost ready for Drupal 6, but I'm curious:

How would I do this on my own using PHP?

The Insert View docs mention that you can "invoke a view using PHP" but I'm having trouble finding directions on how to do this.

Say, for example, I've got a View that creates the following query:

SELECT node.title AS node_title,
   COUNT(node.nid) AS num_records
FROM node node 
WHERE node.type in ('news')
GROUP BY node_title
  ORDER BY node_title DESC

How would I wrap that up so that I could place a snippet of PHP within a node to invoke the view? (Note - I've got the PHP module activated and appropriate permissions set).

Thanks!

Doug

Comments

mlsamuelson’s picture

Status: Active » Fixed

In Views 2 / Drupal 6 you can insert a view using Views' views_embed_view() function.

If you check the function definition for views_embed_view() in views.module you'll find this very helpful bit of documentation:

/**
 * Embed a view using a PHP snippet.
 *
 * This function is meant to be called from PHP snippets, should one wish to
 * embed a view in a node or something. It's meant to provide the simplest
 * solution and doesn't really offer a lot of options, but breaking the function
 * apart is pretty easy, and this provides a worthwhile guide to doing so.
 *
 * @param $name
 *   The name of the view to embed.
 * @param $display_id
 *   The display id to embed. If unsure, use 'default', as it will always be
 *   valid. But things like 'page' or 'block' should work here.
 * @param ...
 *   Any additional parameters will be passed as arguments.
 */
function views_embed_view($name, $display_id = 'default') {

...

In Insert View we use a slightly different approach: get the view's object using the name of the view w/ views_get_view(), and then render the view's object with a $view->preview(). The benefit of this is that it doesn't bring along unnecessary block display related stuff.

mlsamuelson

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.