Supercharge Your Drupal Views with the Views Custom Field Module

3
May 2010

Supercharge Your Drupal Views with the Views Custom Field Module

comment icon4 comment(s) |

There have been several occasions when I've been working on a Drupal view and needed some conditional logic when re-writing the output of a field.  It would be nice if there were some kind of built-in IF or SWITCH statement, but there's not.  On my latest project, I decided to consult Google for some Drupal wisdom.

The result was the Drupal Views Custom Field module.  It basically adds three new field types that you can display in your view: Markup, PHP Code, and Rownumber.  While I'm sure that Markup and Rownumber can be really useful, I didn't investigate them at this time.  I mainly wanted the functionality provided by the PHP field type. 

The main problem I was trying to solve was that I needed to display a different link depending on the node type.  I was pulling in feed items from another site and mixing those with internal blog entries.  If the node was a feed item, I wanted to link to the external blog page.  If it was a blog entry, I wanted to link to it's node in the current site.  Even with the custom field module, I struggled a bit.  To begin with, I was trying to exclude all other fields from display and write the entire row of content into a single PHP field.  The downside of doing it that way is that you forfeit several build-in Drupal functions for path aliases, date formats, and others.

My next approach was to use a PHP field to calculate the link, and then use normal view fields to pull that content in and display it.  This worked better, but I still found some instances where I needed to calculate a path alias.  I eventually found that I could wrap a node ID with the "drupal_get_path_alias()" function and Drupal would handle the rest for me.  I suspect that there are several other drupal functions that I'll be able to incorporate when I use this technique in the future.

Maybe future releases of Drupal views will have a facility for conditional logic.  Until then, I'll be using the Views Custom Field module to achieve similar results.

Comments

October 31, 2010

Anonymous

Viws Custom Field - only show field if current user is author

Hi,

Wondered if you could help,

Would you be able to guide me on how I can add php code to only show a particular field if the current user is the author of that node.

thanks

January 13, 2011

henlfern

Maybe solved at this time...

I wonder if it isn't easier for you to acomplish what you're after with settings User Rights and adding the Content Access module for Drupal.

March 7, 2011

Imprimante

There seems to be some issues

There seems to be some issues with this module.

See here http://pixeljets.com/blog/be-careful-views-custom-field

May 15, 2012

frederickjh

Could you post the PHP you used?

Hi Matt Mueggenborg!

I am working on something similar in views. I have a front page slideshow that each picture in it should link to either a node or an external website. I am now trying to put together the PHP to have it deceide between the two fields in my slideshow-picture type.

I already used the Conditional Fields module to make the user choose between a node or link. Depending on what they choose they will be presented with either a field to enter a link or a nodereference.

Not sure if you did the same but I would be interested to see the PHP you used in views in the Custom Field Module and glean what I can from it.

Thanks in advance for your time and help!

Frederick

Search