tom boone dot com
Excavating the grey area between pop culture and reality...

Drupal

Social Scheduling and Personalized Event Management (Video)

With DrupalCamp L.A. 2010 coming up this weekend, it seemed like as good a time as any to post the video of my presentation from last year. I'm nothing if not timely. My presentation was titled "Social Scheduling and Personalized Event Management" and focused on how I built ScheduAALL.

DrupalCamp LA word cloud

Taking my lead from Jason Eiseman, who created clouds for this year's CALI Conference and AALL Annual Meeting, I've created a DrupalCamp LA word cloud from all of the tweets posted with the camp ha

New features on ScheduAALL

I'm happy to say that, as of this morning, ScheduAALL has 80 user accounts. Assuming approximately 2,000 people attend the AALL Annual Meeting, that means 4% of attendees are already using ScheduAALL. Not bad, considering that the conference is still 4 months away.

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>

When Dreamweaver is a better option than CMS, it's probably time to call it a day

A few minutes ago, while trying to help a colleague create a web page in a proprietary Content Management System, I finally had to advise her to compose her complicated page of tabular data in Dreamweaver and then paste the resulting HTML code into the CMS text editor. And I feel very, very dirty.

So why would I make such a bizarre recommendation? Well, for starters, unless you add a border or background color to a table's cells, the CMS text editor doesn't outline the cells at all. So trying to insert your cursor into the row and column you want to edit is a bit like trying to pick a door on "Let's Make a Deal."

Truth be told, the way such complex pages should work in a CMS-driven site is that there should be a template in place so that all a user had to do is enter her data into a form containing only plain text fields. Upon hitting the "Submit" button, the data record she just entered should automatically be displayed, along with all other records of the same type, in a table based on the template we already created. The content provider never has to worry about formatting the table once it's been defined in the template.

Hmmm... that sounds a lot like CCK, Views and Themes in Drupal.

Is the proprietary CMS capable of doing the same thing? Probably. Does anybody have a clue how to do it? Nope. Do I already know how to do it in Drupal? You bet.

By the way, have I mentioned that tom boone dot com is powered with Drupal? Ah, home sweet home.

Syndicate content