- Home
- Blogs
- Guy Schneerson's blog
- A custom/optional Drupal teaser in a view using the Views Custom Field module
A custom/optional Drupal teaser in a view using the Views Custom Field module
A client needed implementing a custom teaser/short description for a CCK field (not the body) in his Drupal 6 site. The requirements: if the short description/ teaser is available use it otherwise get the data from the CCK field. It is important to note that Display Suite Drupal module was not available as I think it provides a much better approach (will post about in the future).
I have used the Views Custom Field module , this adds a PHP field that allows us to add PHP code to a view.
Setting it up:
Install the views Custom Field Drupal Module
Add two fields to your Content Type in my case it was field_biography & field_teaser
Create your view and add the two fields make sure you check “Exclude from display” as we are going to use the PHP field to determine the output
Add the a customfield: PHP code and put in the following code:
<?php if ($data->node_data_field_teaser_field_teaser_value) { $txt = strip_tags($data->node_data_field_teaser_field_teaser_value); } else { $txt = strip_tags($data->node_data_field_biography_field_bio_value); } // check if empty text if ($txt == '') { // msg for empty text $txt = 'Content coming soon'; } else { // trim if to long if (strlen($txt) > 300) { $last_space = strrpos(substr($txt, 0, 300), ' '); $txt = substr($txt, 0, $last_space); $txt .= '...'; } } // output print check_plain($txt); ?>
The $data variable holds information about the form, note that it only stores the fields that you have added to the view.
The best way to figure out what fields are available is to put in the following debug line into the field
print_r($data);
The first stage is to figure out what field to use if we have a teaser use it otherwise use the biography cck field, we use the strip_tags function to remove HTML formatting, we are doing this for two reasons, the first is that our CCK field can hold links to other content and this will clutter our view, the second is that it looks like the views custom field holds non filtered text, and as the content dosent go through the Drupal filler system the string will need Sanitising.
if ($data->node_data_field_teaser_field_teaser_value) { $txt = strip_tags($data->node_data_field_teaser_field_teaser_value); } else { $txt = strip_tags($data->node_data_field_biography_field_bio_value); }
If our $txt veritable is empty i.e. both of the CCK fields are empty, we should set a 'Content coming soon' Message
// check if empty text if ($txt == '') { // msg for empty text $txt = 'Content coming soon'; }
The last step in preparing the text is to make sure it is not to long, if it is longer than our set limit (300) we will trim it without choping any words off, same as the views “Trim this field to a maximum length” & “Trim only on a word boundary” option
else { // trim if to long if (strlen($txt) > 300) { $last_space = strrpos(substr($txt, 0, 300), ' '); $txt = substr($txt, 0, $last_space); $txt .= '...'; } }
Finally output the text make sure you use the Drupal check_plain to sanitise the string
// output print check_plain($txt);
Contact Details
Blue-Bag Ltd
- info [at] blue-bag.com
- Telephone: 0843 2894522
- Blue-Bag HQ:
The Garage, Manor Farm
Chilcompton, Radstock
Somerset, BA3 4HP, United Kingdom - Telephone: (+44) 01761 411542
- Blue-Bag Brighton:
Unit 35 Level 6 North, New England House
New England Street, Brighton
BN1 4GH United Kingdom - Telephone: (+44) 07944 938204
- VAT GB 748125034
- UK Company Reg: 3932829