Automatic Nodetitles is Usually Overkill
I’m not a huge fan of the Automatic Nodetitles module. It’s got a lot of user interface overhead, and yet I’ve never given a non-programmer access to that administration page. On top of that, someone gets a headache from it on one of the forum sites I frequent every few weeks.
For those with the programming chops to avoid it, why keep the overhead of an entire project, when a custom module snippet will probably be more than enough?
Using the page content type and the custom custom module.
Default Title
/** * Implementation of hook_form_alter(). * Set a default page title. */ function custom_form_alter(&$form, &$form_state, $form_id) { if ($form_id == 'page_node_form') { $form['title']['#default_value'] = 'My Title or a Function Here'; } }
If I were using Spaces, I could snaz that up even further with a spaces_get_space() to pull information about the current space.
Automatic Nodetitle, No Forms
Okay, that didn’t really solve the puzzle of No Forms, Just Magic. This section does:
Step 1: Get Rid of the Title Form
/** * Implementation of hook_form_alter(). */ function custom_form_alter(&$form, &$form_state, $form_id) { if ($form_id == 'page_node_form') { unset($form['title']); } }
Step 2: Set the Title
/** * Implementation of hook_nodeapi(). */ function custom_nodeapi(&$node, $op, $a3, $a4) { switch ($op) { case 'presave': // Every user-entered value is available, such as OG Audience, CCK Field, and Taxonomy. if ($node->type == 'page') { $node->title = 'Some Awesome Title ' . date('Y-m-d'); } break; } }
Enjoy.
6 comments
May I asking what is the
May I asking what is the purpose of these custom code ?
The Purpose
Hello Adrian,
By creating a custom module with either of these code snippets, you can hard-wire aspects of the node title without needing to use something like the Automatic Nodetitles module.
Calling taxonomy values
Trying this code out. Thanks for the post. All is working well so far, except for taxonomy. You commented that Every user-entered value is available including taxonomy, but I seem to be having trouble bringing in any of the terms. I have tried foreach loops to break down the array and build a string and even tried targeting a specific term (i.e. $node->taxonomy[28]->name). I either get an all white screen on save or no data is passed into the title. Do you have a sample of code that uses taxonomy terms to build out the title? Thanks in advance.
Figured it out
Ahhhh, the power of stepping away for 5 minutes.
A print_r on the node object during save revealed that the taxonomy array is set up differently than in the actual node. Perhaps because I’m using tags.
Example:
[taxonomy] => Array
(
[tags] => Array
(
[3] => term 3
[2] => term 2
[1] => term 1
)
)
So….
$node->title = $node->taxonomy[‘tags’][3] . ‘, ’ . $node->taxonomy[‘tags’][2] . ‘, ’ . $node->taxonomy[‘tags’][1] ;
Worked great!
Thanks for this article. I’ve always dislike auto_nodetitle. This is my alternative from now on.
Why Automatic NodeTitles is a
Why Automatic NodeTitles is a bad thing? I’m using it and happy.
Great Module, Nothing Wrong with It
It’s a great module, and there’s nothing wrong with it.
But what is it for? This module primarily provides a nice Administrative GUI to define how node titles should be generated. In exchange for this power (which you will probably use once per content type at most) you have the overhead of an entire module, and yet another link in the administrative part of your website.
That is a fine trade-off, but if you have the development chops to feel comfortable in the code, the few lines I’ve demonstrated above achieve the same thing with much less overhead.