Transforming teaser/body fields output from a view

Events happening in the community are now at Drupal community events on www.drupal.org.
Posted by descender on April 14, 2011 at 1:44pm

Hello everyone,

I'm looking to strip some HTML tags from the teaser/body fields output by a view. Specifically, I want to remove images, tables, etc., and retain only simple text markup (p, br, strong, em, etc.). Where and how can I add the code to perform this?

For CCK fields, I understand that this can be accomplished neatly with custom formatters. Does something similar exist for formatting node teasers/bodies?

Thanks in advance.

Categories: , ,

Comments

A few ways to do it

Posted by alexrayu on April 14, 2011 at 1:52pm

There are a few ways to do it:
1. In the view field settings, set it to strip html. It will strip all html.
2. Use Views Customfield field set to PHP, put it after the field you want to process, see the name of required field with dsm($data) or print_r($data), and write php to parse that field or use some drupal prewritten code to do it. Set the actual field to "exclude from display" and display it's customfield processed variant only.
3. Use a views template to intercept and preprocess the field.

Thanks. Views Customfield is

Posted by descender on April 14, 2011 at 2:09pm

Thanks. Views Customfield is exactly what I am looking for. I did fiddle around with Views templates but couldn't get the right preprocess hook to run.

I used this code for formatting the teaser field:

<?php
echo strip_tags($data->node_revisions_teaser, '<p><br><strong><em><b><u><i>');
?>

Can you please tell how to

Posted by Rameez on December 31, 2011 at 6:18pm

Can you please tell how to use this code? thanks,

Cool, glad it worked

Posted by alexrayu on April 14, 2011 at 2:19pm

Cool, glad it worked. I end up using customfield quite a lot for some tasks.

Theme development

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

AltStyle によって変換されたページ (->オリジナル) /