Overriding Field Handlers in Views with your own custom handler

10 +
dalin's picture

Pretty cool stuff, but I'm

Pretty cool stuff, but I'm not quite sure how node/123/members is any nicer than og/users/123/faces. Marginally I suppose.  I might've chosen something like group/123/members.  Also would it have been easier to do this by overriding the output on the theme layer?

himerus's picture

Tons of possibilities to override

There were a couple of options here to override this same data.

Using template_preprocess_page, it could have been done in order to pull out the data, and then reformat it. However, the default OG functionality added the link, and made it so when preprocess got the data, it was already a link, and I would have had to regex out the numeric value, and then generate my custom link. (Not a horrible solution)

It could have also been done using the views custom fields module, which allows you to attach PHP code inside a views field. This would have allowed me to pull the data I wanted, but exclude from display, and then use my custom PHP code field to access those values, and do pretty much the same thing. (Not the cleanest option)

Regarding the linking to node/$nid/members, yes, I agree, it's not the best option, but on the project I'm on, those links will later be altered using hook_menu_alter() to use a more appropriate path alias for these pages that were "attached" to OG groups. So in my method, that link could be named to anything under the sun, or altered later.

For me this was mostly an attempt to better understand views handlers, and get a wrap on what would be the "most appropriate" "views way" of doing something like this using some of those obscure views API functions that have little to no documentation. There are tons of ways to have accomplished this same thing that are all valid enough, and would work fine. I think the important thing here is maybe not my silly use case with OG, but other things where you may want to completely overtake the field formatting for an item. Maybe something like imagecache or lightbox, etc. would have a more valid reason to be overwritten on a site.

Jake Strawn
Drupal Rockstar

schildi's picture

understanding field handlers

Jake,

when searching desperadly for a way to assign a handler to a field of a default-view I came across your blog.

My problem: When using a default view as a starting point I like to combine two fields into one (and setting a link to somewhere when a URL exists). In view-1 I had no problem defining fields and a handler which does the job. In views-2 it seems not to be possible to define this kind of handler. I tried to set a handler as follows

<code>

$handler->override_option('fields', array(

    'biblio_number' => array(

    ...

    'handler' => 'my_field_handler',

</code>

and defined / declared the handler as also described in this blog. But the handler is silently ignored.

1. Your approach was to modify the data structure. But when setting up the default view no extra data were defined (hook_views_data unused). This is only done once for a table.

2. As far as I understand the data structure is for defining structur an dependencies of database tables. This has nothing to do with displaying content  - done by views - in various ways depending only on current needs. So, my first question is: Why should I modify the data structure to install a field handler? There might exist 10 different handlers in 10 different views for assembling a specific output.

I would be nice if my gap in understanding views-2 could be filled a little bit.

 

Regards

moshe weitzman's picture

Simpler way?

Nice real world example ...

Is it not sufficient to use the Views built in features for 'Rewrite the output of this field' and 'Output this field as a link'? Those appear when you edit this Field in the Views UI. Seems to me like you can change the text and destination of the link with zero code.

 

himerus's picture

If I'm not mistaken

Unless I'm mistaken, the reason what I want to do (at least in this case) couldn't be done using the rewrite output and/or output this field as a link.

Reason being, rewriting the output of the field was the placeholder for this feature until it was figured out. However, you cant run format_plural() inside there. The link to the default page that only wraps the numeric value is also output there, and any text, (members in this case) is output afterwards and not included in the link as the desired effect needed to have.

I think also the output this field as a link ran into the same issues as the link was generated in the handler, and not really able to override it? I don't remember what the result was of playing with that, it might be possible (sort of) with a combination of the two. One of the biggest issues was that appending member(s) or post(s) after result counts I couldn't use format_plural unless I was using that custom views fields module that lets you throw in PHP, and I consider that an evil alternative. :)

Sorry for bashing one of the OG functions for the purpose of this example Moshe!!

Jake Strawn
Drupal Rockstar

crea's picture

There is OG queue for

There is OG queue for that: http://drupal.org/node/558134

himerus's picture

holy crap, that's exactly it!

I'll be damned... I swear I never even found that post when I came up with the handler override!!! That's too funny! Even searching on google with site:drupal.org views_data_alter didn't give me that result!

That sure would have saved me a crap load of time!! LOL

Jake Strawn
Drupal Rockstar

crea's picture

I mean, issue in the queue...

I mean, issue in the queue... :)

aufumy's picture

I'm glad that you didn't find

I'm glad that you didn't find it in the issue queue, because this is a great tutorial.

Thanks!

John Carbone's picture

After trying unsuccessfully

After trying unsuccessfully to override the workflow module for a while I finally figured out the problem. Your tutorial says to "create the file MYMODULE.views.inc in the root of your module's directory" I was following workflow's convention of placing it in the includes folder. After re-reading your article I found this:

/**
 * Implementation of hook_views_api().
 */

function workflow_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'workflow') .'/includes',
  );
}
Just a
stupid nuance that had be scratching my head. Also, hook_views_data_alter is cached and doesn't get run all the time. Hopefully those two tid bits help someone else out.

Post new comment

  • 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> <p> <h2> <h3> <h4> <h5> <h6> <img> <p>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.
  • Twitter-style @usersnames are linked to their Twitter account pages.

More information about formatting options