Trimming node titles

Events happening in the community are now at Drupal community events on www.drupal.org.
Chris Graham's picture

OK, so maybe I am missing something really basic, but this cropped up today when I was creating a directory index for publications on one of our sites.

I created a view using the default glossary view as a reference. When I first created the view and added my filters for Published and Node Type I noticed that the preview list had no sort order so I added Sort By: Node Title and previewed again... was surprised to see something along the lines of:

Advice Note 1: Charitable Status
9 things you need to know about policy analysis
A Guide to Good Employment

So, got a bit confused and then noticed that there was a hidden index linking to /publications-a-z/page_1/%20 - turns out that one of our editors had accidentally created a node with a space at the start of the title.

Now, I can tell the editors to make sure they don't do things like this, but it would be nice to handle it programmatically by running a trim() on $node->title... problem is that I have never hooked a core function before so I'm not entirely sure where to do that. I just need a pointer to which function to hook into in order to do the trim. I've looked at node_validate and node_save, but I can't see where the title is being validated as not empty (which would be a good place to start).

I also don't particularly want to have a complete copy of the core function code in my module, is there a way of forcing the hook to perform it's changes to the node and then call the core function?

I usually like to work these things out myself but I am a bit stumped and any help would be greatly appreciated :)

Comments

hook_nodeapi

alanburke's picture

I reckon hook_nodeapi, op presave
http://api.drupal.org/api/function/hook_nodeapi/6

Untested code..

<?php
mymodule_nodeapi
(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch(
$op){
    case
'presave' :
    
$node->title = trim($node->title);
     break;
  }

}
?>

Cheers

Chris Graham's picture

Thanks Alan, you're a gent... works like a charm.

Ireland

Group notifications

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