Drupal Tutorial Video: Spotlight on the Views_Customfield ModuleDrupal Tutorial Video: Spotlight on the Views_Customfield Module

Categories:

This is one of my GEEKY-est videos yet! The Views CustomField module adds three new, optional fields in the FIELDS section of the VIEWS UI. The uses for these three fields are endless but I'm going to explain how I've used it and give you some sense of the power of this module.

VIEWS is awesome at building queries and showing grids and tables and lists and slideshows and feeds and Excel exports, etc. But sometimes you wish you could insert yourself right in the middle of the query results and do some coding and make some changes to the output or fire some other service or function. The Views CustomField module is exactly what you need.

HOW I USED IT

In my example I created a VIEW of Blog Post Titles then used Views CustomField to add the bold text "HAS IMAGE" to all Blog titles that contained an image-tag embedded in the body. I did this by parsing through the $data object which Views CustomField exposes as the result values of each query as expressed in a PHP object contained in the variable $data.

Here's the initial VIEW settings:

  • FILTER: (Published == YES) (Node Type == Blog Entry)
  • FIELDS: (TITLE as link to node)
  • Show 10 RESULTS
  • Pager FULL

I noted that some of my blog posts have images so I added a new field to my FIELDS section and I chose CustomField from the drop down. The Views CustomField module gave me three options MARKUP, PHP CODE, ROW NUMBERS. I chose PHP CODE.

Now my VIEW settigns looked like this:

 

  • FILTER: (Published == YES) (Node Type == Blog Entry)
  • FIELDS: (TITLE as link to node[EXCLUDED from view]) (CUSTOMFIELD PHP Code) (Node_Body[EXCLUDED from view])
  • Show 10 RESULTS
  • Pager FULL

I EXCLUDED from view the 2 fields because I will have the CUSTOMFIELD render those values with a PRINT command.
I added the Node_Body field NOT because I wanted to render the output but because I wanted to test the value to see if it contained "<img" anywhere in the Node_Body.
To perform this test on the body and do my conditional print I opened the CUSTOMFIELD field and entered the following PHP code into the VALUE text area.

 

<?php

$link = drupal_get_path_alias('node/'.$data->nid); //create a proper URL ALIAS for the NID

if (strpos($data->node_revisions_body, '<img')) { //testing the Node_body for "<img"

print '<a href="'.$link.'">'.$data->node_title.'</a> <strong>HAS IMAGE</strong>'; // Print the IMAGE notice

}

else print '<a href="'.$link.'">'.$data->node_title.'</a>'; // Just print as normal

?>

 

WHAT DOES THE CODE DO?

NOTE: The TITLE value that is contained in the $data object is NOT a link to its node even though that check box was selected on the TITLE field. Rather the $data object contains the RAW data that is returned from the database query. We need to create the links address ourselves from the NID. If you want to see ALL the values that $data hold you can turn on the DEVEL module and then add a dsm($data); within your CUSTOMFIELD.

This code establishes the URL Alias of the NID which Views brings in to scope without me even asking for in the FIELDS section. We pass that NID to the drupal_get_path_alias() function and get a return of the roper URL to use in our ANCHOR tag a few lines later.
After this I do a test to see if "<img" ever appears in the Node_Body. If it does then I PRINT the link with the HAS IMAGE appended to it.
If the body does not have "<img" anywhere within it then I print the link as normal and the PHP code ends.

PRETTY COOL HUH!?

In the video I also mentioned using Views Custom Field for another trick earlier this year. I'll explain that here as well as it is totally different use case.

I had a client that needed a rather tricky TABLE of data that would have FIVE columns. Four of those columns were straight forward CCK FIELDS from the Node-Type. But one of those columns was going to by ONE field OR ANOTHER field depending on which one had value. I didn't one TWO columns showing one value each for the field. I needed ONE column that "KNEW" which field to render in that column. 
By this time you're probably already a ahead of me. It was quite simple with Views Custom Field. I had the 4 other fields in the view just like normal. I also had the OTHER two field in the view BUT I EXCLUDED them from rendering. Instead I hadd the CUSTOMFIELD placed to render as the 4th column and in the VALUE section of the CUSTOMFIELD I places PHP to test for which of the TWO fields had values and render only that one field.

THUS the data in that 4th column was dynamically rendering based on the PHP code that executed in CUSTOMFIELD at the time of render by VIEWS!

I hope you enjoyed this one!

Brijesh's picture

Excellent tutorial. Can I use the custom PHP field to query the database to bring in content from other types? Views by itself only lets me display items of a certain type but I want to gather data from different types. So my question basically is can I query the database in the field?

tomhung's picture

don't forget to validate your output. filter_xss(), l(), or t() all the output!

Doug Vann's picture

 

Good call Tom! THNX! 

DWL's picture

How could you use this module with the PHP code option to create a link that when clicked would do some database I/O and redirect to another page? The database I/O would be based on some of the $data or $context? The $64K question is how to have a link in a view run a bunch of PHP code that's not hanging off a menu, a page, or some other visual URL'd thing. Too bad you can't drop an action in a module, and invoke that via URL. I wasn't able to find any reference samples on the use of the [phpcode] token when ticking the "Output as link"...

Doug Vann's picture

 DWL,

Use a hook_menu() to register the URL function to define the url. Then have that module do your logic and DB interaction.when you need to send the user to another url simply use drupal_goto() as documented in http://api.drupal.org/api/function/drupal_goto/6

If you needed to keep some data in scope for that custom module to use, then either pass it in as a query string OR write the data to a session variable and retrieve it later. Look at Drupal's api for some easy session functions.

Does that help?

 

 

tomhung's picture

You might want to look at "Flag" module as well as Rules based on the flag's value change.

Need a Custom Designed Theme?

If you need a custom designed theme for your business then our designers can work with you to create a unique concept that is consistent with your brand, clearly communicates your value proposition and helps you achieve your goals. >> Click here to contact us and let us know about your needs and we'll analyze and provide you with an estimate for designing and development your custom theme.