We think you're near Los Angeles

Currently in Los Angeles

Location: Los Angeles Current temperature: 55°F: Current condition: Light Rain See Extended Forecast

Drupal: How to create Views 2 filters for your new module

create views filters

Here I will show you how to create Views 2 filters to customize your view results.

I will show you how to write a view filter that joins several tables together to filter the results. I recently wrote a Taxonomy Access Control filter for each of it's permissions (view/update/delete/list/create). We will use that as an example, but in our case we'll only use the "view" permission.

Note: if the code is incomplete or hard to read, you can grab the whole TAC filter setup from here.

First, decide on the query that you need to run, write it and test it (in phpMyAdmin for example).

Here is the query that I need to use to filter nodes that have explicit TAC view permission:

SELECT tn.nid FROM term_node tn LEFT JOIN term_access ta ON tn.tid = ta.tid WHERE ta.rid IN (SELECT ur.rid FROM users_roles ur WHERE ur.uid = %d) AND ta.grant_view = 1

So we need our filter to do the following:

  • JOIN `term_node` to the base `node` table on `vid`.
  • JOIN `term_access` to `term_node` table on `tid`.
  • Add the above WHERE statement to the main views query.

Keep that in mind, and let's do the preliminary work.

First, we edit taxonomy_access.module, and add the following function:

function taxonomy_access_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'taxonomy_access') . '/includes',
  );
}

That will let the module know that we're using views 2 and to look for all the necessary files in the "includes" folder.

Create the "includes" folder.

Add taxonomy_access.views.inc to "includes" folder with the following contents:

function taxonomy_access_views_data() {
  $data = array();
  if(module_exists('taxonomy_access')) {
    $data['node']['taxonomy_access_view'] = array(
      'title' => t('TAC: grant view'),
      'help' => t('Current user has Taxonomy Access Control view permission.'),
      'filter' => array(
        'handler' => 'taxonomy_access_handler_filter_view',
      ),
    );
  }
  return $data;
}

function taxonomy_access_views_handlers() {
  return array(
    'info' => array(
      'path' => drupal_get_path('module', 'taxonomy_access') . '/includes',
    ),
    'handlers' => array(
      'taxonomy_access_handler_filter_view' => array(
        'parent' => 'views_handler_filter',
      ),
    ),
  );
}

The first part tells views about our filter. Provides some helpful explanation, title, and specifies which handler to use and which group to tag it along to ("Node" in our case). The second part is the handler, which basically tells views to look for "taxonomy_access_handler_filter_view.inc" in the "includes" directory.

Now let's create that file:

class taxonomy_access_handler_filter_view extends views_handler_filter {
  function admin_summary() { }
  function operator_form() { }
  function query() {
    $table = $this->ensure_my_table();
    $join = new views_join();
    $join->construct('term_node', $this->table_alias, 'vid', 'vid');
    $this->query->ensure_table('term_node', $this->relationship, $join);
    $join = new views_join();
    $join->construct('term_access', 'term_node', 'tid', 'tid');
    $this->query->ensure_table('term_access', $this->relationship, $join);
    $this->query->add_where($this->options['group'], "term_access.grant_view = 1 AND term_access.rid IN (SELECT rid FROM users_roles WHERE uid = ***CURRENT_USER***)");
  }
}

This is where the magic happens. This does percisely what we set out to do:

  • JOIN `term_node` to the base `node` table on `vid`.
  • JOIN `term_access` to `term_node` table on `tid`.
  • Add the above WHERE statement to the main views query.

Initially you will also want to clear the menu cache for the changes to take effect.

Note: if the code is incomplete or hard to read, you can grab the whole TAC filter setup from here.

Good luck!
Andrey Samode.

Advertisement

By

Open Source Examiner

Andrey is at the forefront of open-source development, having extensive expertise in PHP, Linux, Drupal and other cutting-edge technologies. He is...

Comments

  • Ranjith 1 year ago
    Report Abuse

    there are three exposed filters in my view. one is to enter term ids (not term name)seperated by ',' . I did it by arguments and altering exposed filter form, created a textfield and altered the #action. Second one is simple node type. Third one is for the fields to display i.e there are checkboxes for title, teaser, body. Now i have to show only those fields the user has selected. How can i do that??

  • Anonymous 10 months ago
    Report Abuse

    Do you still bear the noise because of your bad headphone?? Come on!!!
    Our monster beats by dr dre headphone will give you another feeling.
    Just click the follow link. http://www.monsterferraribeats.com/

    welcome to
    ┄┅┆ http://www.monsterferraribeats.com/ ┆┅┄

    ╭━━灬╮╭━━∞╮  .︵
    ┃⌒ ⌒┃┃⌒ ⌒┃  ( )
    ╰━━━〇〇━━━〇 ╱ ︶ ★ Music make a better world .! ★

  • nolan snider 7 months ago
    Report Abuse

    Thank you for this, I have been trying to understand joins for the longest time. the database driven blogs are really nice if you know what you are doing but for us beginners can really be difficult. black ray ban sunglasses, my wordpress site. it was a pain to rehost due to the databases, and everthing i hread is that drupal is worse.

  • Matthias 6 months ago
    Report Abuse

    Great Post! Helped me alot - thanks!

    Cheers,
    Matthias from Reifen Online and Webdesign München

  • Al Hardy llimsa 4 months ago
    Report Abuse

    llimsa Thanks for the help I needed it! Blogs like this are great :)

Add a new comment

Join the conversation! Log in here or create a new account if you've never registered before.

Got something to say?

Examiner.com is looking for writers, photographers, and videographers to join the fastest growing group of local insiders. If you are interested in growing your online rep apply to be an Examiner today!

Don't miss...