This site is not supported for Internet Explorer 6. It might look funny or even not function properly if you are using this browser.
You upgrade Flash as soon as they get a new version. Why not get an updated browser?
Try Firefox, Chrome or if you love and breath Microsoft than IE7.

Changing views filters programmatically with Views 2 and Drupal 6

In trying to keep the amount of new views created on the site I'm working on I wanted to programmitcally change certain portions of a view to use the same output but different inputs. In background I'm using Drupal 6 with Views 2 API.

Specifically I wanted to change the number of items and node type filter. Though this could be expanded to changing the sort, arguments or fields returned as well.

Thanks to some great help from Nathan Rambeck I was able to get this working with the following code:

    $view_args = array(); //Array of arguments
    $display_id = 'block_1'; 
    $view = views_get_view('viewname'); //Name of your view
    $view->set_display($display_id); 
    $view->display_handler->set_option('items_per_page',5);  //Set the number of items to return
 
    $view->set_item($display_id,'filter','type',NULL);  // Get rid of any current node type filters.
 
    /*******************
    *  Below we construct the new filter array.  
    *  I got this be creating a test view and stealing this array from the export code.  
    *  It was located in the $handler->override_option('filters', array()) code of the export.
    *******************/
 
    $filteroptions = array(
        'operator' => 'in',
        'value' => array(
          'ctype_name' => 'ctype_name',  //Change each ctype_name to the machine name of your content type
          'ctype_name' => 'ctype_name',
         ),
      'group' => '0',
      'exposed' => FALSE,
      'expose' => array(
        'operator' => FALSE,
        'label' => '',
      ));
 
    $view->add_item($display_id,'filter','node','type',$filteroptions);  //Add the new filter back to the $view object.
 
    print $view->preview($display_id , $view_args);

Hopefully this will help anyone else trying to programmitcally change Views filters or work with the Drupal Views 2 API.

2 comments

 
shushu wrote 23 weeks 4 days ago

Thanks, I am trying to

Thanks,
I am trying to manipulate the filter by code as well.
Did you find anywhere proper documentation for add_item ?

kbasarab's picture
kbasarab wrote 23 weeks 3 days ago

Hey shushu. I think this

Hey shushu. I think this might help you out:

views_db_object::add_item

kB

Add your comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <p> <br /> <br> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • 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 <% ... %>.
  • Images can be added to this post.

More information about formatting options

Mollom CAPTCHA (play audio CAPTCHA)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated.

Friends

User login