Drupal Views2 Handlers
Views2 is an awesome progression from the original views interface. I've got a feeling the new UI will not please everyone, but I really like the fact it gives a good overview of the current configuration of a view without having to explore lots of collapsible fieldsets.
Views2 is still Release Candidate status, but there's already good support for the new API in other contrib modules such as the excellent Flag module (the progression of views_bookmark). I was forced today to start getting my head around the new Views2 API because I am trying to do something with the BuddyList2 module that requires views - but BuddyList does not yet have Views integration. Here's what I found out...
There's two places to start understanding the Views API. First port of call is the built in Advanced Help which you get when you install the module, and is also available online. Start with the (very brief) description of the API. Once you start to get your head around how the API interacts with the Views system you can start to explore the Views2 API documentation.
My problem here was that I needed to show a list of nodes that had been created by friends of the currently logged in user. To do this I need to make the Views system aware of the Buddylist table by implementing hook_views_data. This is a function that just returns an array of information about the BuddyList database tables. I only need to return some basics here because the only information I'm interested in is that which will help me decide if a node has been authored by one of my 'buddies'.
There's a whole page of documentation on how to implement hook_views_data() but I found looking at some examples examples was essential before I could get my head around what it was doing. Basically you need to return an array. The first element is an array that defines your table info, then you include elements for each field you want to make available. Here's the code that makes the BuddyList relationships available to Views. It's very basic at the moment as it doesn't account for different types of relationship - but let's take a look at how it works...
<?php
function buddylist_api_views_data() {
$data = array();
$data['buddylist_relations']['table']['group'] = t('Buddylist');
$data['buddylist_relations']['requester_id'] = array(
'title' => t('Your Buddy'),
'help' => t('A link if your buddy did create the node'),
'filter' => array(
'handler' => 'views_handler_filter_user_current',
),
);
$data['buddylist_relations']['table']['join']['node'] = array(
'left_field' => 'uid',
'field' => 'requestee_id',
);
return $data;
}
?>As you can see we're creating an array called $data and returning this. In this array we put information about our database tables. In order to find the nodes created by the people I befriended I need to join the node table to the buddylist_relations table based on the requestee_id. The requestee_id contains the uid (user id) of people on the receiving end of a friend request. You can see this join happening in the second half of the code.
The first half of the code exposes another of the Buddylist database columns as a filter. It uses the requester_id which is the uid (user id) of the person initiating the friend request. This is passed to one of the pre-existing handlers - so we don't have to write any more code! The views_handler_filter_user_current handler exists within the views user.module handlers. This takes the database field and will filter out the records that match the current logged in user.
I think the next step is to considering the effect of different user relationships defined in the BuddyList module, and how a custom handler would be built to accommodate this.
Comments
Thanks. Very useful. Hope there will be more about Views2.
- reply
Sam (not verified)Gotta say that Views is so baffling it's amazing. I'm a big fan of Drupal but I really feel that it's gotten to the point where useability -- from the user and the site maintainer -- is a real issue.
Perhaps you, as you get your head around Views2, can provide for the average person some walk throughs.... It is so NOT intuitive.
thanks
geoff
- reply
Guest (not verified)I wanna develop my own handlers, filters/fields for views using custom modules.
But am stuck with the handler as it does not perform well. Or I'm missing something.
Do you have any idea or links?
If so, please forward them.
- reply
Guest (not verified)Can you give me more information on what you mean by "does not perform well"?
Are you talking about performance, i.e. have you already created your own handler and it's slowing things down?
Or have you tried creating a handler and it's not doing what you expect?
To link your custom modules with views2 might be as simple as defining the relationship between the tables and using the handlers already available from the other modules. For example, I linked up the buddy list module tables with views, but used the handlers provided in the user module views as shipped with Views2.
- reply
DarrenThe problem is that I don't really understand how to write your own views2 handlers.
For example, in database table I have an url to a picture and I would like Views to display it properly (not like a link to it, but like a picture that is.
I tried writing something like that:
( hook_views_data )
...
$data['tablename']['piclink'] = array(
'title' => t('Picture'),
'help' => t('An URL to a picture'),
'filter' => array(
'handler' => 'myown_views_handler_filter_image',
),
);
...
Handler itself:
function myown_views_handler_filter_image( $value ){
return '';
}
But it didn't work =) It stopped displaying current field at all.
- reply
Gorb Alexey (not verified)First - you probably want to add a field, not a filter?
The handler code is object-oriented, and a bit more complicated than that, you can't just define your own function - you need to extend one of the built in ones.
You will probably find one of the default handlers works for you in this case. I would try using "views_handler_field".
Something like:
<?php
$data['tablename']['piclink'] = array(
'title' => t('Picture'),
'help' => t('An URL to a picture'),
'field' => array(
'handler' => 'views_handler_field',
),
);
?>
If you are using a bespoke handler, don't forget to declare it:
<?php
function example_views_handlers() {
return array(
'info' => array(
'path' => drupal_get_path('module', 'example'),
),
'handlers' => array(
// filter handlers
'example_handler_filter_field_exists' => array(
'parent' => 'views_handler_filter',
),
),
);
}
?>
- reply
Darrenhi fellow,
your writings are quite good, need to know the step by step usage of views. Me stuck right at creating view, need to register a magazine that has month, year, title , image etc fields, i wanna show year on the block, when user click on any year all magazine come up with their monthly editions.
any idea help is highly appreciated.
- reply
Nauman Ikram (not verified)Views and CCK will work well to achieve what you are trying to do.
First you create your Content type called Magazine. You add CCK fields for the extra data you need. As you mentioned above, a Date field with month and year, and an image field.
Then you can create your view that will define the block. Create a new view, and add a "block" display. Within the block display set your filters to only show nodes of type "magazine". Add fields to your block - as you described above you will want to add the Date field and change the formatting so that it just shows the Year. You can then change the row style options for the view to group by year.
This will start to get closer to what you described above. I'm not clear from your explaination exactly what it is you are trying to achieve. You may need to create some template overrides to get the fields to display how you like, you can find out how to do this by going into your view and clicking on "Template Information".
- reply
Darrenhi fellow,
Upto now am able to create block that contains the year like 2008,2009 when i clicked on any one it showed up with their respective results, here page view gives the selection of fields that I wanna show when some one click on years, the selected fields have an option to link the fetched rows with their nodes, finally my node come up successfully. A comple circle completed in three steps i.e. creating block then page and then page link to their respective nodes.
What I need or having an issue, the final page i.e. node must have of different style (cosmetic issue), my page come up with field caption and its value ... upto last form field.
This thing i want to change and restrict some of the fields not to display, how can i make a node with different style or as per my need.
Got poing in my mind is to make a view of node, passing node id and that view take it as a parameter and display the singl result.
How do i pass querystring variable to another view? Or is there other way to do that ?
You may visit http://panchamsachet.com/index.php
on the left side there is block named with "Pancham Mag. 2009" click on it, it will take you the next page, when you click on any one of them final node come up with Big and thumbnail image with rest of the fields, I want to customize this page, some of the fields will be removed and some alignment is needed in this regard.
- reply
Nauman Ikram (not verified)Hi
iam trying to build a query with views like this
select group_concat(distinct title separator ' ,') as service, down_id, sd.state_id, reason, enddate_planned from {state_downtimes} sd,{service_downtimes} srd,{node} n where sd.down_id = srd.downtime_id and srd.service_id=n.nid and concat(sd.state_id,'-',srd.service_id) in (select concat(state_id,'-',service_id) as state_service_id from {group_downtimes_view} where service_id != 0 and group_id=$_SESSION[Group_id]) and srd.resolved=0 and sd.startdate_planned <= $current_time group by srd.down_id order by down_id desc;
but iam very confused where to use filters,arguments and fields.
is it possible to build?
please help me.
- reply
nancy (not verified)Why have you called your article "views handlers" if the main point is not the handlers but exporting your data to the views which is a trivial task!
- reply
Guest (not verified)Please remember this article was written a long time ago, when there was no (well, almost no) documentation on the Views 2 API. I was going to discuss handlers further but published anyway just in case it was useful. It's true that now there is a lot more documentation and examples, and exposing data to views is a trivial task.
- reply
Darrencool!
- reply
Crystal (not verified)