User login

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.

Thanks. Very useful. Hope

Thanks. Very useful. Hope there will be more about Views2.

Gotta say that Views is so

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

Developing own handlers, filters/fields

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.

What do you mean "does not perform well"?

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.

The problem is that I don't

The 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 '<img src="'.$value.'">';
}

But it didn't work =) It stopped displaying current field at all.

First - you probably want to

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',
      ),
    ),
  );
}
?>

Post new 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> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.