Hello,
I'm defining a new node type in a custom module. I'd like the node title be hidden on it's node add form and then populated based on values from the node type itself when it is saved. Is hook_node_presave the best place to do this? Also, is changing the access of the title field using hook_form_alter the best way, as this feels a little hackish to me, but I'm not sure of a better alternative.
Thanks!
function applicator_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'applicator_node_form') {
$form['title']['#access'] = 0;
$form['title']['#required'] = FALSE;
}
}
function applicator_node_presave($node) {
if ($node->type == 'applicator') {
$fname = field_get_items('node', $node, 'applicator_module_first_name');
$fname = $fname[0]['value'];
$surname = field_get_items('node', $node, 'applicator_module_last_name');
$surname = $surname[0]['value'];
$title = $fname . ' ' . $surname;
$node->title = $title;
}
}
Comments
Comment #1
richard.french commentedComment #2
cameron tod commentedThere's a contrib module which seems to solve a similar use case: https://drupal.org/project/auto_nodetitle
I used it on D6 many times with much success.
Comment #3
cameron tod commentedComment #4
richard.french commentedThanks. I've looked at the module, but it seems using this module you can only configure the node title through the node UI and ideally I'd rather have this set through code. I've looked and there don't seem to be any obvious functions I can use to do this. @cam8001 when you used this for D6 did you have it working through a module?
Thanks