The Embed Views Display


If you don’t know about embedding views you might want to check out this post first http://views-help.doc.logrus.com/help/views/embed.

Update – This is now a module that you can download on d.o.

Embed Views – Project page

Have you ever created a view and the only thing you needed it for was embedding it somewhere on your site? On some projects I have had recently I certainly had and embedding regular view displays will work. But, you either have to choose from a block, page, feed, or use the default display from the views you want to embed. What if you did not want to have a callback or a block associated with a view and only wanted to embed it somewhere else on the site? What could you do? Create an embed views display of course!

The whole purpose of this views display is to only create a default style views display that does not have any additional features or “cruft”.



What I ended up doing since I wanted it to act exactly like the views default display was replicate the display and let it know it is not the default and give it another name and it worked!

If you would like to have your own just follow the steps below.

Requirements

  • Drupal 6.x
  • Views 2

First, create the module that will contain the information necessary for integrating w/ views2. Lets call it my_views.
Your file layout should look like.

  • my_views
    • my_views.module
    • my_views.info
    • my_views_plugin_display_embed.inc

The module only has to implement one function, hook_views_plugins().


array(
'embed' => array(
'title' => t('Embed'),
'help' => t('Creates a display specifically for embed.'),
'handler' => 'my_views_plugin_display_embed',
'theme' => 'views_view',
'use ajax' => TRUE,
'use pager' => TRUE,
'use more' => TRUE,
'accept attachments' => TRUE,
'help topic' => 'display-pane',
),
),
);
}
?>

The my_views_plugin_display_embed.inc file should look something like.

execute_display('default', $args);
* @endcode
*
* For more complex usages, a view can be partially built:
* @code
* $view->set_arguments($args);
* $view->build('default'); // Build the query
* $view->execute(); // Run the query
* $output = $view->render(); // Render the view
* @endcode
*
* If short circuited at any point, look in $view->build_info for
* information about the query. After execute, look in $view->result
* for the array of objects returned from db_query.
*
* You can also do:
* @code
* $view->set_arguments($args);
* $output = $view->render('default'); // Render the view
* @endcode
*
* This illustrates that render is smart enough to call build and execute
* if these items have not already been accomplished.
*
* Note that execute also must accomplish other tasks, such
* as setting page titles, breadcrumbs, and generating exposed filter
* data if necessary.
*/
function execute() {
return $this->view->render($this->display->id);
}
}
?>

And don’t forget the info file.

; $Id:$
name = My Views
description = Defines embed view display.
core = 6.x