try another color:
try another fontsize: 60% 70% 80% 90%
tom boone dot com
Excavating the grey area between pop culture and reality...

Getting around the Views Node: Body problem in Drupal

As reported by numerous users on Drupal.org, adding the Node: Body field to a view displays the entire node (i.e., title, author, body, CCK fields), not just the body field. Given that the display of Node: Body is controlled by node.tpl.php (or node-$type.tpl.php), this limits your ability to manipulate individual fields.

Add/Edit View:

One solution to this problem is disabling Drupal's default Body field for a content type. To do this, delete the "Body field label" value in the content type's Submission Form Settings. Then replace the Body field with a CCK text field. This new field will be available in Views UI and will display only the field's contents.

Edit Content Type:

Often, however, a site is already filled with content that uses the default body field. One workaround for this is to manually edit the site's database, copying the body field values into the new CCK field.

Unfortunately, after disabling the body field in some content types, the default field continues to display in the node edit form, thus requiring a more complex solution.

Using Contemplate to Display the Body Field
While Node: Body displays an entire node, there is a variable in Drupal that refers only to the content of the body field:

$node->content['body']['#value']

A solution suggested by BeatnikDude in a Drupal.org comment is to hijack the content type's teaser using the Content Templates module (aka Contemplate). In the Teaser template, check the box labeled "Affect teaser output" and enter this code into the template's text box:

<?php print $node->content['body']['#value'] ?>

Content Template:

Now when adding fields to a view in Drupal 6, select Node: Teaser.

Drupal 6:

In Drupal 5, select Node: Body and change the Handler to "Teaser."

Drupal 5:

The view will now display only the contents of the body field, allowing you to individually select only those fields you wish to display.

Manually Trimming the Body Field Display in Views
The biggest drawback to hijacking the Teaser template is that you no longer have a trimmed version of your node available. All calls to the teaser now display the untrimmed body field text. However, there is a workaround to create trimmed body text in Views.

Drupal 6:
In Views for Drupal 6 it's possible to create a template file for each individual field. The default field template used by Views (views-view-field.tpl.php) contains a single line of code:

<?php print $output; ?>

In a view titled 'test' containing a teaser field, you can create a separate template file (views-view-field--test--teaser.tpl.php, in this example) that controls the display of the Node: Teaser. To see what file name to use for a field, check the Theme: Information link in the Views UI.

You will first need to define a function for trimming the length of a string. This should be placed into your theme's template.php file or, if you want the function available to all themes, in a custom helper module. For this example, I'm using neat_trim() (via Justin-Cook.com):

<?php
function neat_trim($str, $n, $delim='&hellip;') {
 
$len = strlen($str);
  if (
$len &gt; $n) {
   
preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches);
    return
rtrim($matches[1]) . $delim;
  }
  else {
    return
$str;
  }
}
?>

In the field template file (views-view-field--test--teaser.tpl.php), use that function to trim the teaser's length:

<?php
$string
= neat_trim($output, 100);
print
$string;
?>

The field display will now be limited to 100 characters.

Drupal 5:
In Drupal 5, define a string trimming function in the same manner as in Drupal 6.

Unfortunately, in Drupal 5 you're limited to creating template files that cover an entire view, not individual fields, but you can still tweak a single field within this template.

To generate template code, use the Views Theme Wizard module (included with Views). This provides three code snippets: one to add to template.php, one to paste into a new view template file (views-list-test.tpl.php in this example -- the theme wizard will tell you what to name it), and one to create a stylesheet for your view.

In your new view template file look for:

<div class="view-field view-data-body">
  <?php print $body ?>
</div>

Then apply your trim function on $body:

<?php
$string
= neat_trim($body, 100);
?>

<div class="view-field view-data-body">
  <?php print $string ?>
</div>

1 comment so far...

1
Jay Mottern said...

I'm having an issue where no teaser is showing up at all, let alone the entire body: Drupal or Views seems to be ignoring my Post Settings' teaser length.

I was following your instructions to try to force a teaser length, so article submitters won't have to insert a break into each new story. I'm using Drupal 5.10 and in Views, I don't have "Node: Body" in my choices of available fields to add to my view. Can you provide or point me to some detailed instructions for how to add this field?

Add your comments...

  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

Syndicate content