Index: README.txt =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/insert_view/README.txt,v retrieving revision 1.7 diff -u -p -r1.7 README.txt --- README.txt 26 Jun 2008 16:35:33 -0000 1.7 +++ README.txt 31 Mar 2009 04:11:15 -0000 @@ -24,9 +24,9 @@ of local override has been deprecated in version 2's custom display functionality. See "UPGRADING FROM A PREVIOUS VERSION" below for more information. -[view:my_view=my_display=1,2,3] +[view:my_view=my_display=arg1/arg2/arg3] uses the my_display view display, and passes a comma delimited list of arguments -(in this case 1, 2, and 3) to the view. +(in this case arg1, arg2, and arg3) to the view. Here's an example you could use with the default view named "tracker" which uses the page display and takes a user ID as an argument: @@ -63,7 +63,7 @@ magic. Once Insert View is installed, visit the the input formats page at /admin/settings/filters and click the "configure" link for the input format(s) -for which you wish to enable the Insert View Filter. Then simply check the +for which you wish to enable the Insert View Filter. Then simply check the checkbox for the filter. IMPORTANT PERFORMANCE NOTE: To dispaly views correctly, Insert View turns off Index: insert_view.info =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/insert_view/insert_view.info,v retrieving revision 1.3 diff -u -p -r1.3 insert_view.info --- insert_view.info 26 Jun 2008 16:35:33 -0000 1.3 +++ insert_view.info 31 Mar 2009 03:12:39 -0000 @@ -1,5 +1,6 @@ +; $Id$ name = Insert view description = Allows users to embed content lists generated by the Views module into node bodies. dependencies[] = views package = Views -core = 6.x \ No newline at end of file +core = 6.x Index: insert_view.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/insert_view/insert_view.install,v retrieving revision 1.2 diff -u -p -r1.2 insert_view.install --- insert_view.install 12 May 2008 15:39:43 -0000 1.2 +++ insert_view.install 31 Mar 2009 20:03:04 -0000 @@ -1,11 +1,15 @@ The Views module allows administrators to create dynamic lists of content - for display in pages or blocks. It is possible to insert those lists into - existing node bodies and blocks, but such inclusion requires that PHP - filtering be turned on. The Insert View module allows any user to insert - view listings using tag syntax, without the need for PHP execution - permissions. The Insert View tag syntax for embedding a view is relatively - simple:

- -
[view:my_view]
- -

is replaced by the content listing corresponding to the named view. In - this case it is my_view.

- -
[view:my_view=my_display]
- -

invokes the my_view view using the my_display view display ID. If the - display slot is left empty, the view\'s "default" display is used.

- -
[view:my_view=my_display=1,2,3]
- -

uses the my_display view display, and passes a comma delimited list of - arguments (in this case 1, 2, and 3) to the view.

- -

Here\'s an example you could use with the default view named "tracker" - which uses the page display and takes a user ID as an argument:

- -
[view:tracker=page=1]
- -

In short this tag says, "Insert the view named tracker, use the "page" - display, and supply the argument 1."

- -

Sometimes you want to pass an argument without specifying a display ID. - You can do that by leaving the display ID slot empty, like so:

- -
[view:my_view==1]
- -

How to find a display ID: On the edit page for the view - in question, you\'ll find a list of displays at the left side of the control - area. "Defaults" will be at the top of that list. Hover your mouse - pointer over the name of the display you want to use. A URL will appear in - the status bar of your browser. This is usually at the bottom of the - window, in the chrome. Everything after #views-tab- is the display ID. For - example in http://localhost/admin/build/views/edit/tracker?destination=node%2F51#views-tab-page - the display ID would be "page".

- - '); +

The Views module allows administrators to create dynamic lists of content for display in pages or blocks. It is possible to insert those lists into existing node bodies and blocks, but such inclusion requires that PHP filtering be turned on. The Insert View module allows any user to insert view listings using tag syntax, without the need for PHP execution permissions. The Insert View tag syntax for embedding a view is relatively simple:

+
[view:my_view]
+

is replaced by the content listing corresponding to the named view. In this case it is my_view.

+
[view:my_view=my_display]
+

invokes the my_view view using the my_display view display ID. If the display slot is left empty, the view\'s "default" display is used.

+
[view:my_view=my_display=arg1/arg2/arg3]
+

uses the my_display view display, and passes a slash delimited list of arguments (in this case arg1, arg2, and arg3) to the view.

+

Here\'s an example you could use with the default view named "tracker" which uses the page display and takes a user ID as an argument:

+
[view:tracker=page=1]
+

In short this tag says, "Insert the view named tracker, use the "page" display, and supply the argument 1."

+

Sometimes you want to pass an argument without specifying a display ID. You can do that by leaving the display ID slot empty, like so:

+
[view:my_view==1]
+

How to find a display ID: On the edit page for the view in question, you\'ll find a list of displays at the left side of the control area. "Defaults" will be at the top of that list. Hover your mouse pointer over the name of the display you want to use. A URL will appear in the status bar of your browser. This is usually at the bottom of the window, in the chrome. Everything after #views-tab- is the display ID. For example in http://localhost/admin/build/views/edit/tracker?destination=node%2F51#views-tab-page the display ID would be "page".

' + ); } else { return t('You may use [view:viewname] tags to display listings of nodes.', array("@insert_view_help" => url("filter/tips/$format", array('fragment' => 'filter-insert_view')))); } } +/** + * Helper function to replace the tag syntax with the actual view. + */ function _insert_view_substitute_tags($text) { if (preg_match_all("/\[view(_pager)?:([^=\]]+)=?([^=\]]+)?=?([^\]]*)?\]/i", $text, $match)) { + $mtch = $repl = array(); foreach ($match[3] as $key => $value) { // view_pager syntax is deprecated in the D6 version, but leaving in for // now so we don't break people's legacy tags $match[1][$key] == '_pager' ? $pager = TRUE : $pager = FALSE; // not used - - $viewname = $match[2][$key]; - $display = ($match[3][$key] && !is_numeric($match[3][$key])) ? $match[3][$key] : 'default'; - $view_args = $match[4][$key]; - $view = views_get_view($viewname); - $replace = ""; + $view_name = $match[2][$key]; + $display_id = ($match[3][$key] && !is_numeric($match[3][$key])) ? $match[3][$key] : 'default'; + $view_args = $match[4][$key]; - if ($view_args != NULL) { - $view_args = explode(',', $view_args); - } - else { - $view_args = array(); + if (strpos($view_args, '/') === FALSE) { + $view_args = str_replace(',', '/', $view_args); } - if ($view) { - // render our previously gotten view, providing it with our matched - // display (or default) and matched arguments - $replace = $view->preview($display, $view_args); + $replace = insert_view($view_name, $display_id, $view_args); + if ($replace) { $mtch[] = $match[0][$key]; $repl[] = $replace; } @@ -114,3 +88,48 @@ function _insert_view_substitute_tags($t } return $text; } + +/** + * 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. Other than embedding the view it checks + * the view access and also allows to use view arguments grabbed from the URL. + * + * @param $view_name + * The name of the view. + * @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 $args + * Additional arguments to send to the view as if they were part of the URL in + * the form of arg1/arg2/arg3. You may use %0, %1, ..., %N to grab arguments + * from the URL. + */ +function insert_view($view_name, $display_id = 'default', $args = '') { + if (empty($view_name)) { + return; + } + + $view = views_get_view($view_name); + if (empty($view)) { + return; + } + + if (!$view->access($display_id)) { + return; + } + + $url_args = arg(); + foreach ($url_args as $id => $arg) { + $args = str_replace("%$id", $arg, $args); + } + $args = preg_replace(',/?(%\d),', '', $args); + $args = $args ? explode('/', $args) : array(); + + $view->set_display($display_id); + $view->set_arguments($args); + $output = $view->preview($display_id, $args); + $view->destroy(); + return $output; +}