Skip to main content
  • Home
  • Work
  • Photography
  • Contact
  • Client Login
New Rivers Digital
Home Blogs Eric Weik's blog

Accessing Views 2 in an External Script

In:
  • Drupal
  • Drupal Recipes
  • PHP
  • Views.module
26Jun2009

I just finished a quick project that had the following requirements:

  1. A PHP based application needs to output content from a Drupal installation that lives elsewhere on the same server. For the purposes of this discussion, lets pretend we will be adding a "Recent Blog Posts" item to an e-commerce site that pulls directly from Drupal.
  2. The Drupal content must be nicely formatted, and fairly easy for the site administrators to alter.

My solution used the views_embed_view() function for Views 2.

The Drupal site already had Views 2 installed, so the first step was to point and click my way through making a new "Recent Blog Posts" view (called "recent_blog_posts" in our example). The view settings were pretty standard -- filter by node:type and node:published, sort by node:sticky and node:post-date, and set the output style as an HTML list. Should the site administrators wish to change the number of items output, etc. at a later date, they can easily do so from the Views UI.

The next step was to update the external script to load this view for display in the external application. Thanks to Drupal's drupal_bootstrap() function, and the Views 2 views_embed_view() function, this can be accomplished with a scant four lines of code.

<?php
// Change to Drupal root directory
chdir('/path/to/drupal/');

// Optional error reporting during development (turn off for production sites)
// error_reporting(E_ALL);

// Bootstrap Drupal

require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// Output the view
print views_embed_view('recent_blog_posts');
?>
  • Eric Weik's blog

Comments

#1 A Few Updates

Eric Weik's picture

Submitted by Eric Weik on Tue, 07/07/2009 - 21:04.

In cases where your site uses clean urls, and your script exists in a directory outside of the Drupal directory, relative links in your view may not be formatted properly. Relative links passed through Drupal's url() function will include the path to the directory your script exists in (e.g. "node/99" will link to "/path/to/script/node/99") which likely will not work properly.

Here is an updated version of the above script to run a search and replace on such links:

<?php
// If Drupal is installed in a subdirectory under DOCUMENT_ROOT, specify it
// here in the form /path/to/drupal/
define('DRUPAL_SUBDIR','/content/');

// Change to Drupal root directory
chdir($_SERVER["DOCUMENT_ROOT"].DRUPAL_SUBDIR);

// Turn on error reporting during development (turn off for production sites)
// error_reporting(E_ALL);

// Bootstrap Drupal

require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// Views 2 output via views_embed_view()
$rawfeed = views_embed_view('recent_blog_posts');

// Replace relative links which may be broken by calls to url()
$search = "href=\"{$base_path}";
$replace = "href=\"{$base_root}".DRUPAL_SUBDIR;
print
str_ireplace($search,$replace,$rawfeed);

// To fetch nodes without views, see use something like:
// $results = db_query('select nid from node where type="story"');
// while ($result = db_fetch_object($results)) {
//   $node = node_load($result->nid);
//   echo "Debug (node): <pre>";print_r($node);echo "</pre>";
// }
?>
  • reply

#2 Clarification of views_embed_view() parameters in 6.x-2.7

Eric Weik's picture

Submitted by Eric Weik on Mon, 01/04/2010 - 22:40.

Although I do not specifically discuss this feature in my original post, views_embed_view() accepts additional parameters, one of which changed slightly between 6.x-2.7 and older versions.

Lets first look at the function definition and documentation for views_embed_view():

<?php
/**
* 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.
*
* Note that this function does NOT display the title of the view. If you want
* to do that, you will need to do what this function does manually, by
* loading the view, getting the preview and then getting $view->get_title().
*
* @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 my original example, we were only using the $name parameter, which pulled the default display of a view. However, if you have a view with multiple displays (e.g. "Default" for the default view, "Some Page View" for a page view, and "Some Block View" for a block), you can load the specific display by passing in $name and $display_id with a calls like:

<?php
// Fetch the default view for output:
$defaultfeed = views_embed_view('recent_blog_posts');

// Fetch "Some Page View" for output:
$pagefeed = views_embed_view('recent_blog_posts','page_1');

// Fetch "Some Block View" for output:
$blockfeed = views_embed_view('recent_blog_posts','block_1');
?>

In versions of views prior to 6.x-2.7, you could actually use the human readable name of the display instead of the identifier:

<?php
// *** Don't do this, it does not work in Views 6.x-2.7 or later!! ***
$rawfeed = views_embed_view('recent_blog_posts','Some Page View');
?>

However, this is not the correct usage, and after some changes to the display handler, this usage will result in an empty view result.

If you run into this problem, you should simply be able to update to use the proper display identifier ('page_1', 'page_2', 'block_1', block_2', etc.).

For more information, see the following issue: http://drupal.org/node/624144.

  • reply

Post new comment

Warning
I strongly encourage and welcome links and feedback. However, this site is moderated and comments with inappropriate links are rejected. Please do not post a one-line "Me too" or "Great post!" comment just so you can link to your site. Thank you for your understanding.
The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.
  • Use to create page breaks.

More information about formatting options

Blog Posts (RSS)

About

Hello! My name is Eric Weik. I am a computer scientist, photographer, musician, and occasional blogger. New Rivers Digital is my software consulting business. I am dedicated to using open source software and open data standards for Web development and applications integration. In particular, I am an ardent Drupal fan and specialize in Drupal module development, theming, and data architecture integration.

Contact Details

New Rivers Digital
PO Box 784

Lancaster, VA 22503

Voice+1-804-577-8526
Fax +1-804-462-3229
Contact Form

Content Tags

Abstract B&W Bookmarklet Celestial Clouds CSA Date Drupal Drupal Recipes Drupal Sites Dynamic Range Estuary Government Grasses Long Exposure Macro New Rivers Digital Photoblog Photoset RGB Sketchbook Snow Storm Sumi-e Sunset Surf Theming Trigger.module Views.module Webform.module Zen Zen Theming
more tags

Recent comments

  • 3.x Beta
    8 weeks 2 days ago
  • Some feedback from Webform 3.x
    8 weeks 3 days ago
  • rainbow art
    8 weeks 3 days ago
  • Sumi
    10 weeks 5 days ago
  • Thanks for this info! I lost
    11 weeks 4 days ago
  • Look into "Comment Notify"
    18 weeks 4 days ago

Popular content

Today's:

  • Implementing Flickr Slideshow Links By Theming Flickr.module
  • Sending Multiple Customized Confirmation Messages with Webform.Module
  • The Meggy Jr. RGB

All time:

  • Using Drupal Actions, Triggers, and Tokens to Send Notifications About Comments
  • Implementing Flickr Slideshow Links By Theming Flickr.module
  • Sending Multiple Customized Confirmation Messages with Webform.Module

Activity Stream

  • Thu, 09/09/2010 - 16:24

  • Flickr Eric posted Microcity v2.04.2 (IR sim) 4:24pm #
  • Wed, 09/08/2010 - 13:32

  • Twitter Eric tweeted "@troysomers Mid-September to mid-November is the pinnacle of the year IMHO -- I'm ready!" 1:32pm#
  • Tue, 09/07/2010 - 15:54

  • Twitter Eric tweeted "@troysomers I applaud this idea, even if I won't be able to follow it. Write about what you know and love!" 3:54pm#
  • Twitter Eric tweeted "Well hello again twitter. Long time no see. How about some #photography for the day? http://bit.ly/c6p0XZ" 8:07am#
  • Mon, 09/06/2010 - 21:55

  • Flickr Eric posted DSC04355-foxtails 9:55pm #
  • Flickr Eric posted #04361 - Foxtails 9:54pm #
  • Flickr Eric posted DSC04359-foxtails 9:54pm #
  • Thu, 09/02/2010 - 21:05

  • Del.icio.us Eric linked to The Magic Onions 9:05pm#
  • Sat, 08/28/2010 - 23:00

  • Del.icio.us Eric linked to Fanboy.com 11:00pm#
  • Sat, 08/28/2010 - 22:58

  • Del.icio.us Eric linked to Japanese Emoticons: A Stamp of Success ยป Fanboy.com 10:58pm#
more from my activity-stream


I am a member of the Drupal Association.
Eric At NRD on Drupal.org
Circumjacence (Eric Weik) on Twitter
Circumjacence on Delicious
Eric Weik on Linkedin
Circumjacence (Eric Weik) on Flickr
Circumjacence (Eric Weik) at StumbleUpon

Powered by Drupal & Genesis | Valid XHTML 1.0 Strict | Syndicate content RSS Feed

© 2010 New Rivers Digital | PO Box 784 | Lancaster, Virginia 22503 | +1-804-577-8526 | Contact Form