diff --git a/auto_nodetitle.install b/auto_nodetitle.install index 12e9b7b..179f2b9 100755 --- a/auto_nodetitle.install +++ b/auto_nodetitle.install @@ -15,6 +15,7 @@ function auto_nodetitle_uninstall() { variable_del('ant_'. $type); variable_del('ant_pattern_'. $type); variable_del('ant_php_'. $type); + variable_del('ant_has_nid_'. $type); } } diff --git a/auto_nodetitle.module b/auto_nodetitle.module index c35a271..bc8690b 100644 --- a/auto_nodetitle.module +++ b/auto_nodetitle.module @@ -61,8 +61,26 @@ function auto_nodetitle_node_form_submit($form, &$form_state) { * Implementation of hook_nodeapi(). */ function auto_nodetitle_nodeapi(&$node, $op) { - if ($op == 'presave' && auto_nodetitle_is_needed($node)) { - auto_nodetitle_set_title($node); + switch ($op) { + case 'presave': + // Only use the presave op when node nid is not set which is the same as + // doing it everytime except initial node creation. + if ($node->nid && auto_nodetitle_is_needed($node)) { + auto_nodetitle_set_title($node); + } + break; + case 'insert': + // Insert only occurs on initial creation of the node. The node nid will + // not be set at this point. + if (auto_nodetitle_is_needed($node)) { + auto_nodetitle_set_title($node); + // If the title pattern has been marked as containing a reference to + // node nid then we will perform an additional node save during the insert. + if (variable_get('ant_has_nid_'. $node->type, 0)) { + node_save($node); + } + } + break; } } @@ -213,6 +231,12 @@ function auto_nodetitle_node_settings_form(&$form) { '#value' => variable_get('ant_php_'. $form['#node_type']->type, ''), ); } + $form['auto_nodetitle']['ant_has_nid'] = array( + '#type' => 'checkbox', + '#title' => t('The pattern contains tokens or PHP reliant on the node nid.'), + '#description' => t('If the pattern contains the node nid or other things that may be reliant on the node nid then you need to check this option. Checking this option will however cause a performance hit when initially saving a node so only check if necessary or you experience issues with the title not generating properly on initial creation of a node.'), + '#default_value' => variable_get('ant_has_nid_'. $form['#node_type']->type, ''), + ); } /** @@ -242,16 +266,20 @@ function auto_nodetitle_node_type($op, $info) { variable_del('ant_'. $info->type); variable_del('ant_pattern_'. $info->type); variable_del('ant_php_'. $info->type); + variable_del('ant_has_nid_'. $info->type); break; case 'update': if (!empty($info->old_type) && $info->old_type != $info->type) { variable_set('ant_'. $info->type, auto_nodetitle_get_setting($info->old_type)); variable_set('ant_pattern_'. $info->type, variable_get('ant_pattern_'. $info->old_type, '')); variable_set('ant_php_'. $info->type, variable_get('ant_php_'. $info->old_type, '')); + variable_set('ant_has_nid_'. $info->type, variable_get('ant_has_nid_'. $info->old_type, '')); variable_del('ant_'. $info->old_type); variable_del('ant_pattern_'. $info->old_type); variable_del('ant_php_'. $info->old_type); + variable_del('ant_has_nid_'. $info->old_type); } break; } } +