t('Insert view filter'));
case 'description':
return t('Embed views into nodes using [view:name=display=args] tags.');
case 'prepare':
return $text;
case 'process':
return _insert_view_substitute_tags($text);
case 'no cache':
return TRUE;
}
}
/**
* Implementation of hook_filter_tips().
*/
function insert_view_filter_tips($delta, $format, $long = FALSE) {
if ($long) {
return '
' . t(
'
- Insert view filter allows to embed views using tags. The tag syntax is relatively simple: [view:name=display=args]
- For example [view:tracker=page=1] says, embed a view named "tracker", use the "page" display, and supply the argument "1".
- The display and args parameters can be omitted. If the display is left empty, the view\'s default display is used.
- Multiple arguments are separated with slash. The args format is the same as used in the URL (or view preview screen).
Valid examples:
- [view:my_view]
- [view:my_view=my_display]
- [view:my_view=my_display=arg1/arg2/arg3]
- [view:my_view==arg1/arg2/arg3]
') . '
';
}
else {
return t('You may use [view:name=display=args] tags to display views.', 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:([^=\]]+)=?([^=\]]+)?=?([^\]]*)?\]/i", $text, $match)) {
foreach ($match[0] as $key => $value) {
$view_name = $match[1][$key];
$display_id = ($match[2][$key] && !is_numeric($match[2][$key])) ? $match[2][$key] : 'default';
$args = $match[3][$key];
$view_output = insert_view($view_name, $display_id, $args);
$search[] = $value;
$replace[] = !empty($view_output) ? $view_output : '';
}
return str_replace($search, $replace, $text);
}
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;
}