| 1 |
<?php |
<?php |
| 2 |
// $Id: auto_nodetitle.module,v 1.4.2.19 2008/12/30 20:28:45 fago Exp $ |
// $Id: auto_nodetitle.module,v 1.4.2.20 2009/01/08 11:28:42 fago Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 47 |
* To let node previews work this is also done during node validation. |
* To let node previews work this is also done during node validation. |
| 48 |
*/ |
*/ |
| 49 |
function auto_nodetitle_nodeapi(&$node, $op, $form = NULL, $a4 = NULL) { |
function auto_nodetitle_nodeapi(&$node, $op, $form = NULL, $a4 = NULL) { |
| 50 |
if ($op == 'validate' && $form['#post']['op'] == t('Preview')) { |
if ($op == 'validate' && $form['#post']['op'] == t('Preview') && auto_nodetitle_is_needed($node)) { |
| 51 |
if (($setting = auto_nodetitle_get_setting($node->type)) && !($setting == AUTO_NODETITLE_OPTIONAL && !empty($node->title))) { |
// As changes to $node are not persistent during validation, we use form_set_value()! |
| 52 |
// As changes to $node are not persistent during validation, we use form_set_value()! |
auto_nodetitle_set_title($node); |
| 53 |
auto_nodetitle_set_title($node); |
form_set_value($form['title'], $node->title); |
|
form_set_value($form['title'], $node->title); |
|
|
} |
|
| 54 |
} |
} |
| 55 |
else if ($op == 'submit' && ($setting = auto_nodetitle_get_setting($node->type)) && !($setting == AUTO_NODETITLE_OPTIONAL && !empty($node->title))) { |
else if ($op == 'submit' && auto_nodetitle_is_needed($node)) { |
| 56 |
auto_nodetitle_set_title($node); |
auto_nodetitle_set_title($node); |
| 57 |
} |
} |
| 58 |
} |
} |
| 59 |
|
|
| 60 |
|
/** |
| 61 |
|
* Returns whether the auto nodetitle has to be set. |
| 62 |
|
*/ |
| 63 |
|
function auto_nodetitle_is_needed($node) { |
| 64 |
|
return empty($node->auto_nodetitle_applied) && ($setting = auto_nodetitle_get_setting($node->type)) && !($setting == AUTO_NODETITLE_OPTIONAL && !empty($node->title)); |
| 65 |
|
} |
| 66 |
|
|
| 67 |
/* |
/* |
| 68 |
* Sets the automatically generated nodetitle for the node |
* Sets the automatically generated nodetitle for the node |
| 69 |
*/ |
*/ |
| 80 |
else { |
else { |
| 81 |
$node->title = t('@type', array('@type' => $types[$node->type]->name)); |
$node->title = t('@type', array('@type' => $types[$node->type]->name)); |
| 82 |
} |
} |
| 83 |
|
// With that flag we ensure we don't apply the title two times to the same node. |
| 84 |
|
$node->auto_nodetitle_applied = TRUE; |
| 85 |
} |
} |
| 86 |
|
|
| 87 |
/** |
/** |
| 90 |
function auto_nodetitle_node_operations() { |
function auto_nodetitle_node_operations() { |
| 91 |
$operations = array( |
$operations = array( |
| 92 |
'nodetitle_update' => array( |
'nodetitle_update' => array( |
| 93 |
'label' => t('Update automatic Nodetitles'), |
'label' => t('Update automatic nodetitles'), |
| 94 |
'callback' => 'auto_nodetitle_operations_update', |
'callback' => 'auto_nodetitle_operations_update', |
| 95 |
), |
), |
| 96 |
); |
); |
| 103 |
function auto_nodetitle_operations_update($nodes) { |
function auto_nodetitle_operations_update($nodes) { |
| 104 |
foreach ($nodes as $nid) { |
foreach ($nodes as $nid) { |
| 105 |
$node = node_load($nid); |
$node = node_load($nid); |
| 106 |
$nodetitle_save = $node->title; |
if ($node && auto_nodetitle_is_needed($node)) { |
| 107 |
auto_nodetitle_set_title($node); |
$previous_title = $node->title; |
| 108 |
if ($node->title != $nodetitle_save) { |
auto_nodetitle_set_title($node); |
| 109 |
//only save if the title has actually changed |
// Only save if the title has actually changed. |
| 110 |
node_save($node); |
if ($node->title != $previous_title) { |
| 111 |
|
node_save($node); |
| 112 |
|
} |
| 113 |
} |
} |
| 114 |
} |
} |
| 115 |
} |
} |