Developing drupal module with customizable views
I was assigned a task to add more reports to casetracker module. This module is powerful. Anyway, it is not mature yet. There are only a few default reports. They are not enough at least for me and my colleage. In particular, the default reports just offer me to view list of all projects, my projects, all cases, my cases and advanced search. Well, what we want is all open cases, all resolved cases and a block to show my open cases.
There are so many approaches to implement these reports. However, I chose views module. Views module is very powerful. It lets us create a page or a block without coding. We just specify fields, filters, and sorting criteria and views will do the rest. Unfortunately, there is no magic. The module must support views by adding hooks to views module.
This is my first experience in hooking views module. In addition, it is also an extension of casetracker, says sub-module. I named my module as casetracker_views so I have to hook at function casetracker_views_views_tables(). This function will return an array of table information. In this case, I described 2 tables; casetracker_case and casetracker_project. That means I can create new pages and new blocks according to these 2 tables later.
I used a few tricks to display username according to assign_to field by adding a custom handler to look for username by given uid as follows.
function casetracker_views_views_handler_field_username($fieldinfo, $fielddata, $value, $data) {
$user = user_load(array(’uid’ => $value));
return theme(’username’, $user);
}
And added a handler to filter only cases assigned to current user.
function casetracker_views_views_handler_filter_usercurrent($op, $filter, $filterinfo, &$query) {
global $user;
$query->add_where("casetracker_case.assign_to $filter[operator] ’***CURRENT_USER***’");
}
For filtering case status, I added a filter handler to look for an array of status. Fortunately, casetracker already provided a generic function to generate that array shortly.
function casetracker_views_views_handler_filter_case_status() {
return casetracker_options_select(’casetracker_case_status’,’case_status_id’,’case_status_name’);
}
So now I can create 2 pages for showing all open cases and all resolved cases and a block for showing my open cases in just a few clicks.
Technorati Tags: English, Programming, PHP, Software, Drupal, Tips and Tricks, Casetracker, Views
- sugree's blog
- 606 reads
Post new comment