Wednesday, January 30, 2013

Writing a custom Drupal 6 Views 2 filter - resources

This is not intended to be a full tutorial but resources and tips that I found helpful.

Drupal 6
Views 2

Helpful Tutorials:
http://www.hashbangcode.com/blog/creating-custom-views-filters-exposed-form-element-drupal-6-561.html

In summary the major steps are:

1. Within a custom module, define hook_views_api() to alert Drupal that the module is views enabled, api version 2, optional path index pointing to a directory that has additional files for the view.

2.  Use hook_views_handlers() to again point to the directory of files to include for the view, as well as the name of a custom handler to be used and the parent handler it will extend.  The custom name handler should be the same as the name of the .inc file which will contain a class, which again will have the same name.

3. Use hook_views_data() to define the name and group the filter will appear under in the Views UI. Again the name of the custom handler is included.

4. In the .inc file named after the handler, create a class (custom handler name) that extends the parent handler class.  This class will contain about two methods. The first value_form() will build and return the form to be used within the views UI. The second, query(), uses drupal methods to insert additional 'WHERE' or 'AND' clauses to the SQL query statement being built by the view.  (add_where())

One major roadblock I hit was attempting to reference a table other than the main one being used by the view. SQL was giving me errors about an 'unknown column'. The method add_table() allowed me to register other tables to be used within the query.

No comments:

Post a Comment