Drupal 6 Views embeding and passing filters

Submitted by programmer on Tue, 01/13/2009 - 10:10.
::
  • Load a view:

    <?php
    $view
    = views_get_view('view_name');
    ?>
  • Initalize it and add arguments. Fiddle with options as needed:

    <?php
    $view
    ->set_display('default');
    $view->set_arguments(array('first', 'second'));
    $view->is_cacheable = FALSE;
    $view->display['default']->handler->options['items_per_page'] = 3;
    $view->display['default']->handler->options['title'] = $title;
    $view->display['default']->handler->options['display_options']['use_pager'] = FALSE;
    ?>
  • Or add a filter. Works for fields, sorts, and arguments as well.

    <?php
    $display_id
    = 'default';
    $view->set_display($display_id);
    $id = $view->add_item($display_id, 'filter', 'og_uid', 'is_admin');
    $item = $view->get_item($display_id, 'filter', $id);
    $item['value'] = array(0, 1);
    $view->set_item($display_id, 'filter', $name, $item);
    $view->is_cacheable = FALSE;
    ?>
  • Get an array of the view result objects:

    <?php
    $view
    ->execute();
    foreach (
    $view->result as $result) {
      foreach (
    $view->field as $id => $field) {
          if (!empty(
    $view->field[$id]['handler'])) {
           
    $view->field[$id]['handler']->pre_render($view->result);
           
    // Do something with this unrendered result object
         
    }
        }
    }
    ?>
  • Another way to get array of results:

    <?php
    $view
    ->render();
    foreach (
    $view->result as $result) {
     
    // Do something with this rendered result object
    }
    ?>
  • Use a custom display plugin

    <?php
    $display_id
    = $view->add_display('my plugin');
    $result = $view->render($display_id);
    ?>
  • Another method

    <?php
    $view
    = views_get_view('my_view');
    $view->execute_display($display_id, $args);
    ?>
Source: http://groups.drupal.org/node/10129

Post your Answer

  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options