--- /home/yagosoft/tenb/auto_nodetitle/auto_nodetitle.module 2011-02-01 16:26:41.000000000 +0100 +++ auto_nodetitle.module 2011-02-01 16:52:16.000000000 +0100 @@ -9,6 +9,7 @@ define('AUTO_NODETITLE_DISABLED', 0); define('AUTO_NODETITLE_ENABLED', 1); define('AUTO_NODETITLE_OPTIONAL', 2); +define('AUTO_NODETITLE_ENABLED_PRESERVE', 3); /** * Implementation of hook_perm() @@ -27,17 +28,33 @@ function auto_nodetitle_form_alter(&$for } else if (isset($form['#node']) && isset($form['#method']) && $form['#node']->type .'_node_form' == $form_id) { //this is a node form - if (auto_nodetitle_get_setting($form['#node']->type) == AUTO_NODETITLE_ENABLED) { - // we will autogenerate the title later, just hide the title field in the meanwhile - $form['title']['#value'] = 'ant'; - $form['title']['#type'] = 'value'; - $form['title']['#required'] = FALSE; - $form['#submit'][] = 'auto_nodetitle_node_form_submit'; - } - else if (auto_nodetitle_get_setting($form['#node']->type) == AUTO_NODETITLE_OPTIONAL) { - // we will make the title optional - $form['title']['#required'] = FALSE; - $form['#submit'][] = 'auto_nodetitle_node_form_submit'; + $ant_mode = auto_nodetitle_get_setting($form['#node']->type); + $form['title']['#required'] = FALSE; + $form['#submit'][] = 'auto_nodetitle_node_form_submit'; + switch ($ant_mode) { + case AUTO_NODETITLE_ENABLED: + // we will autogenerate the title later, just disable the title field in the meanwhile + $form['title']['#value'] = 'ant'; + $form['title']['#type'] = 'value'; + $form['title']['#disabled'] = TRUE; + $form['title']['#description'] = t('Title for this node will be set automatically by ANT module'); + break; + case AUTO_NODETITLE_OPTIONAL: + // we will make the title optional + $form['title']['#description'] = t("If you don't enter a title it will be generated automatically by ANT module"); + break; + case AUTO_NODETITLE_ENABLED_PRESERVE: + if (empty($form['title']['#default_value'])) { + $form['title']['#value'] = 'ant'; + } + else { + $form['title']['#value'] = $form['title']['#default_value']; + } + $form['title']['#disabled'] = TRUE; + $form['title']['#description'] = t('Title for this node will be set automatically bij ANT module, however once created it will be preserved and not changed on update'); + break; + default: + break; } } } @@ -50,7 +67,8 @@ function auto_nodetitle_node_form_submit // Only do something, if the user clicked the preview button. if (isset($form_state['clicked_button']['#submit']) && in_array('node_form_build_preview', $form_state['clicked_button']['#submit'])) { $setting = auto_nodetitle_get_setting($form_state['values']['type']); - if ($setting == AUTO_NODETITLE_ENABLED || ($setting == AUTO_NODETITLE_OPTIONAL && empty($form_state['values']['title']))) { + if ($setting == AUTO_NODETITLE_ENABLED || ($setting == AUTO_NODETITLE_OPTIONAL && + empty($form_state['values']['title'])) || ($setting == AUTO_NODETITLE_ENABLED_PRESERVE)) { $node = node_submit($form_state['values']); auto_nodetitle_set_title($node); $form_state['values'] = (array)$node; @@ -64,14 +82,17 @@ function auto_nodetitle_node_form_submit function auto_nodetitle_nodeapi(&$node, $op) { if ($op == 'presave' && auto_nodetitle_is_needed($node)) { auto_nodetitle_set_title($node); - } + } } /** * Returns whether the auto nodetitle has to be set. */ function auto_nodetitle_is_needed($node) { - return empty($node->auto_nodetitle_applied) && ($setting = auto_nodetitle_get_setting($node->type)) && !($setting == AUTO_NODETITLE_OPTIONAL && !empty($node->title)); + return empty($node->auto_nodetitle_applied) && + ($setting = auto_nodetitle_get_setting($node->type)) && + !($setting == AUTO_NODETITLE_OPTIONAL && !empty($node->title)) && + !($setting == AUTO_NODETITLE_ENABLED_PRESERVE && !($node-> title == 'ant')); } /** @@ -163,6 +184,7 @@ function auto_nodetitle_node_settings_fo t('Disabled'), t('Automatically generate the title and hide the title field'), t('Automatically generate the title if the title field is left empty'), + t('Always automatically generate the title but preserve the first generated title'), ) );