| 1 |
<?php |
<?php |
| 2 |
// $Id: auto_nodetitle.module,v 1.18 2007/04/07 15:25:04 fago Exp $ |
// $Id: auto_nodetitle.module,v 1.19 2007/10/04 17:08:45 fago Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 6 |
* Allows hiding of the node title field and automatic title creation |
* Allows hiding of the node title field and automatic title creation |
| 7 |
*/ |
*/ |
| 8 |
|
|
| 9 |
define('AUTO_NODETITLE_DISABLED', 0); |
define('AUTO_NODETITLE_DISABLED', 0); |
| 10 |
define('AUTO_NODETITLE_ENABLED', 1); |
define('AUTO_NODETITLE_ENABLED', 1); |
| 11 |
define('AUTO_NODETITLE_OPTIONAL', 2); |
define('AUTO_NODETITLE_OPTIONAL', 2); |
| 20 |
/** |
/** |
| 21 |
* Implementation of hook_form_alter(). |
* Implementation of hook_form_alter(). |
| 22 |
*/ |
*/ |
| 23 |
function auto_nodetitle_form_alter($form_id, &$form) { |
function auto_nodetitle_form_alter(&$form, $form_state, $form_id) { |
| 24 |
|
|
| 25 |
if (isset($form['#node_type']) && 'node_type_form' == $form_id) { |
if (isset($form['#node_type']) && 'node_type_form' == $form_id) { |
| 26 |
auto_nodetitle_node_settings_form($form); |
auto_nodetitle_node_settings_form($form); |
| 27 |
} |
} |
| 28 |
else if (isset($form['#node']) && isset($form['#post']) && $form['#node']->type .'_node_form' == $form_id) { |
else if (isset($form['#node']) && isset($form['#method']) && $form['#node']->type .'_node_form' == $form_id) { |
| 29 |
//this is a node form |
//this is a node form |
| 30 |
if (auto_nodetitle_get_setting($form['#node']->type) == AUTO_NODETITLE_ENABLED) { |
if (auto_nodetitle_get_setting($form['#node']->type) == AUTO_NODETITLE_ENABLED) { |
| 31 |
// we will autogenerate the title later, just hide the title field in the meanwhile |
// we will autogenerate the title later, just hide the title field in the meanwhile |
| 32 |
$form['title']['#value'] = 'ant'; |
$form['title']['#value'] = 'ant'; |
| 33 |
$form['title']['#type'] = 'value'; |
$form['title']['#type'] = 'value'; |
| 34 |
$form['title']['#required'] = FALSE; |
$form['title']['#required'] = FALSE; |
| 35 |
|
$form['#submit'][] = 'auto_nodetitle_node_form_submit'; |
| 36 |
} |
} |
| 37 |
else if (auto_nodetitle_get_setting($form['#node']->type) == AUTO_NODETITLE_OPTIONAL) { |
else if (auto_nodetitle_get_setting($form['#node']->type) == AUTO_NODETITLE_OPTIONAL) { |
| 38 |
// we will make the title optional |
// we will make the title optional |
| 39 |
$form['title']['#required'] = FALSE; |
$form['title']['#required'] = FALSE; |
| 40 |
|
$form['#submit'][] = 'auto_nodetitle_node_form_submit'; |
| 41 |
} |
} |
| 42 |
} |
} |
| 43 |
} |
} |
| 44 |
|
|
| 45 |
/** |
/* |
| 46 |
* Implementation of hook_nodeapi(). |
* Gets the node object, modifies the title, and updates the node in the form_state |
| 47 |
*/ |
*/ |
| 48 |
function auto_nodetitle_nodeapi(&$node, $op, $form = NULL, $a4 = NULL) { |
function auto_nodetitle_node_form_submit($form, &$form_state) { |
| 49 |
if ($op == 'validate') { |
$setting = auto_nodetitle_get_setting($form_state['values']['type']); |
| 50 |
if ((auto_nodetitle_get_setting($node->type) == AUTO_NODETITLE_ENABLED) || ((auto_nodetitle_get_setting($node->type) == AUTO_NODETITLE_OPTIONAL) && empty($node->title))) { |
if ($setting == AUTO_NODETITLE_ENABLED || ($setting == AUTO_NODETITLE_OPTIONAL && empty($form_state['values']['title']))) { |
| 51 |
/* |
$node = node_submit($form_state['values']); |
| 52 |
* Sets the node title. |
auto_nodetitle_set_title($node); |
| 53 |
* We need to do this on validation because of two points: |
$form_state['values'] = (array)$node; |
|
* It's early engouh to have node previews working and |
|
|
* it's late enough as CCK has already gone through the 'process form values' step |
|
|
* |
|
|
* As changes to $node are not persistent during validation, we use form_set_value()! |
|
|
*/ |
|
|
auto_nodetitle_set_title($node); |
|
|
form_set_value($form['title'], $node->title); |
|
|
} |
|
| 54 |
} |
} |
| 55 |
} |
} |
| 56 |
|
|
| 70 |
else { |
else { |
| 71 |
$node->title = t('@type', array('@type' => $types[$node->type]->name)); |
$node->title = t('@type', array('@type' => $types[$node->type]->name)); |
| 72 |
} |
} |
|
|
|
|
// warn, if the generated title is empty |
|
|
if (!trim($node->title) && !variable_get('ant_emptyok_'. $node->type, 0)) { |
|
|
$message = t('Autogenerated title field is blank.'); |
|
|
if (user_access('administer nodes')) { |
|
|
$message .= ' '. t('Perhaps you need to change the <a href="@url">configuration settings</a> for this content type.', array('@url' => 'admin/content/types/'. $node->type)); |
|
|
} |
|
|
} |
|
| 73 |
} |
} |
| 74 |
|
|
| 75 |
/** |
/** |
| 87 |
if (variable_get('ant_php_'. $node->type, 0) || module_exists('token')) { |
if (variable_get('ant_php_'. $node->type, 0) || module_exists('token')) { |
| 88 |
$output = preg_replace('/[\t\n\r\0\x0B]/', '', strip_tags($output)); |
$output = preg_replace('/[\t\n\r\0\x0B]/', '', strip_tags($output)); |
| 89 |
} |
} |
|
|
|
| 90 |
return $output; |
return $output; |
| 91 |
} |
} |
| 92 |
|
|
| 93 |
/** |
/** |
| 94 |
* Helper function for hook_form_alter() renders the settings per node-type. |
* Helper function for hook_form_alter() renders the settings per node-type. |
|
* @TODO: a re-evaluate PHP pattern on edit? option. |
|
| 95 |
*/ |
*/ |
| 96 |
function auto_nodetitle_node_settings_form(&$form) { |
function auto_nodetitle_node_settings_form(&$form) { |
| 97 |
$form['auto_nodetitle'] = array( |
$form['auto_nodetitle'] = array( |
| 112 |
); |
); |
| 113 |
|
|
| 114 |
if (module_exists('token') || user_access('use PHP for title patterns')) { |
if (module_exists('token') || user_access('use PHP for title patterns')) { |
| 115 |
|
|
| 116 |
$description = t('Leave blank for using the per default generated title. Otherwise this string will be used as title.'); |
$description = t('Leave blank for using the per default generated title. Otherwise this string will be used as title.'); |
| 117 |
if (module_exists('token')) { |
if (module_exists('token')) { |
| 118 |
$description .= ' '. t('Use the syntax [token] if you want to insert a replacement pattern.'); |
$description .= ' '. t('Use the syntax [token] if you want to insert a replacement pattern.'); |
| 124 |
'#default_value' => variable_get('ant_pattern_'. $form['#node_type']->type, ''), |
'#default_value' => variable_get('ant_pattern_'. $form['#node_type']->type, ''), |
| 125 |
); |
); |
| 126 |
} |
} |
| 127 |
|
|
| 128 |
if (module_exists('token')) { |
if (module_exists('token')) { |
| 129 |
$form['auto_nodetitle']['token_help'] = array( |
$form['auto_nodetitle']['token_help'] = array( |
| 130 |
'#title' => t('Replacement patterns'), |
'#title' => t('Replacement patterns'), |
| 155 |
} |
} |
| 156 |
|
|
| 157 |
/** |
/** |
| 158 |
* Gets the auto node title setting associated with the given content type. |
* Gets the auto node title setting associated with the given content type. |
| 159 |
*/ |
*/ |
| 160 |
function auto_nodetitle_get_setting($type) { |
function auto_nodetitle_get_setting($type) { |
| 161 |
return variable_get('ant_'. $type, AUTO_NODETITLE_DISABLED); |
return variable_get('ant_'. $type, AUTO_NODETITLE_DISABLED); |
| 162 |
} |
} |