Here's something interesting to keep in mind the next time you're getting fancy with your nodes during the hook_nodeapi presave step: Don't forget your teaser.
I was having trouble with a Drupal 6 website I've been working on, where it seemed almost as if the body was duplicating part of itself (or sometimes all of itself) on the edit - it looked fine when you were viewing the node, but editing it caused things to duplicate within the body field. I tried the usual suspects for fixing - I searched for nodeapi calls inside of the custom modules we'd written to find the culprit, and then even just started turning off modules, and the theme, one by one, hoping to find the culprit that way.
Eventually I just tried using sql to modify the teaser in node_revisions, and when this modification showed up in the edit form, I started doing a search in the code for 'teaser .' - and I found it in node.pages.inc's node_body_field:
<?php
function node_body_field(&$node, $label, $word_count) {
// Check if we need to restore the teaser at the beginning of the body.
$include = !isset($node->teaser) || ($node->teaser == substr($node->body, 0, strlen($node->teaser)));
...
$form['body'] = array(
'#type' => 'textarea',
'#title' => check_plain($label),
'#default_value' => $include ? $node->body : ($node->teaser . $node->body),
'#rows' => 20,
'#required' => ($word_count > 0),
);
?>The problem is that the edit form for nodes compares the teaser with the start of the body data. If they don't match, then it prepends the teaser to the body data - which is normally used for when the person writing the node splits the teaser/body into two parts.
However, I was changing the body in hook_nodeapi('presave'), which turns out to be after the node teaser was created. The fix was as simple as adding $node->teaser = node_teaser($node->body); to the end of the presave code.
- Tags: code, development, Drupal, Drupal 6








Comments
Daniel Hunt writes:
Hi, I'm just moving from Wordpress to Drupal, any ideas on what would be a good place to learn the ropes... got the basic system up, but need help on customisation..
Matt Tucker writes:
There really are countless ways of jumping into Drupal, and it really depends on what your skill set is. If you are a developer, get really comfortable with api.drupal.org - it really is a great resource for any developer. If you are less technical, there are many books which can get you up and running at a rapid pace. A good starting point is Building powerful and robust websites with Drupal 6. But if that sounds too simplistic, Pro Drupal Development V2 is your next best bet.
Another great resource is the community itself. Whether it is through blogs such ours here at pingVision, or others. There is also a few training videos out there, one by Lullabot and another set by Lynda, or for free at http://drupal.org/handbook/customization...
Finally, the most obvious resource is drupal.org itself. There are hundreds of pages of documentation available, and obvious place to start is http://drupal.org/getting-started.
Hopefully this is information which will direct you in the right direction. Welcome to Drupal!
Laura writes:
I would also encourage you to participate in Drupal Meet-ups. There are local groups all over the world -- perhaps near you? If you're in the Boulder area, you could come to the monthly Drupal meet-ups hosted right here at pingVision. Everyone from experts to the curious beginner are welcome! (Our next meet-up is Wednesday evening next week.)
David writes:
John, thank you for sharing this. This snippet and back information is exactly the in-the-trenches type stuff we end up running into. I'm sure this will save me some hours of would-have-been frustration in the future.
Thanks for sharing!