You are here

insert_view.module in Insert View 5

File

insert_view.module
View source
<?php

function insert_view_filter($op, $delta = 0, $format = -1, $text = '') {

  // The "list" operation provides the module an opportunity to declare both how
  // many filters it defines and a human-readable name for each filter. Note that
  // the returned name should be passed through t() for translation.
  if ($op == 'list') {
    return array(
      0 => t('insert view filter'),
    );
  }

  // All operations besides "list" provide a $delta argument so we know which
  // filter they refer to. We'll switch on that argument now so that we can
  // discuss each filter in turn.
  switch ($op) {
    case 'description':
      return t('Inserts content lists into nodes using [view:myview] tags.');
    case 'prepare':
      return $text;
    case 'process':
      return _insert_view_substitute_tags($text);
    case 'no cache':
      return TRUE;
  }
}
function insert_view_filter_tips($delta, $format, $long = false) {
  if ($long) {
    return t('
    <p>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:</p>

    <pre>[view:my_view]</pre>

    <p>is replaced by the content listing corresponding to the named view (in
    this case, my_view).</p>

    <pre>[view:my_view=5]</pre>

    <p>limits the listing to 5 entries.</p>

    <pre>[view:my_view=10=1,2,3]</pre>

    <p>limits the listing to 10 entries, and passes a comma delimited list of
    arguments (in this case 1, 2, and 3) to the view.</p>

    <p>Here\'s an example you could use with the default view named "tracker"
    which takes a user ID as an argument:</p>

    <pre>[view:tracker=5=1]</pre>

    <p>In short this tag says, "Insert the view named tracker, limit the
    number of results to 5, and supply the argument/user ID 1."</p>

    <p>Sometimes you want to pass an argument without placing a limit on the number
    of results. You can do that by leaving the limits position empty, like so:</p>

    <pre>[view:my_view==1]</pre>

    <p>You can use a pager with your view by using the following syntax
    (note: you must set a limit which will serve as the number of nodes per
    page):</p>

    <pre>[view_pager:my_view=3]</pre>
    ');
  }
  else {
    return t('You may use <a href="@insert_view_help">[view:viewname] tags</a> to display listings of nodes.', array(
      "@insert_view_help" => url("filter/tips/{$format}", NULL, 'filter-insert_view'),
    ));
  }
}
function _insert_view_substitute_tags($text) {
  if (preg_match_all("/\\[view(_pager)?:([^=\\]]+)=?([^=\\]]+)?=?([^\\]]*)?\\]/i", $text, $match)) {
    foreach ($match[3] as $key => $value) {
      $match[1][$key] == '_pager' ? $pager = TRUE : ($pager = FALSE);
      $viewname = $match[2][$key];
      $limit = $match[3][$key];
      $view_args = $match[4][$key];
      $view = views_get_view($viewname);
      $replace = "";
      if ($view_args != NULL) {
        $view_args = explode(',', $view_args);
      }
      else {
        $view_args = array();
      }
      if ($view) {

        // set $view->url to current page so views with exposed filters submit back to the same page
        $view->url = drupal_get_path_alias($_GET['q']);
        if (is_numeric($limit)) {
          $replace = views_build_view('embed', $view, $view_args, $pager, $limit);
        }
        else {
          $replace = views_build_view('embed', $view, $view_args, FALSE, NULL);
        }
        $mtch[] = $match[0][$key];
        $repl[] = $replace;
      }
    }
    return str_replace($mtch, $repl, $text);
  }
  return $text;
}