source: sipes/modules_contrib/auto_nodetitle/auto_nodetitle.module @ 1e95969

stableversion-3.0
Last change on this file since 1e95969 was 177a560, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se agrego el directorio de modulos contribuidos de drupal

  • Propiedad mode establecida a 100755
File size: 8.1 KB
Línea 
1<?php
2// $Id: auto_nodetitle.module,v 1.20.2.5 2009/04/21 08:52:10 fago Exp $
3
4/**
5 * @file
6 * Allows hiding of the node title field and automatic title creation
7 */
8
9define('AUTO_NODETITLE_DISABLED', 0);
10define('AUTO_NODETITLE_ENABLED', 1);
11define('AUTO_NODETITLE_OPTIONAL', 2);
12
13/**
14 * Implementation of hook_perm()
15 */
16function auto_nodetitle_perm() {
17  return array('use PHP for title patterns');
18}
19
20/**
21 * Implementation of hook_form_alter().
22 */
23function auto_nodetitle_form_alter(&$form, $form_state, $form_id) {
24
25  if (isset($form['#node_type']) && 'node_type_form' == $form_id) {
26    auto_nodetitle_node_settings_form($form);
27  }
28  else if (isset($form['#node']) && isset($form['#method']) && $form['#node']->type .'_node_form' == $form_id) {
29    //this is a node form
30    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
32      $form['title']['#value'] = 'ant';
33      $form['title']['#type'] = 'value';
34      $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) {
38      // we will make the title optional
39      $form['title']['#required'] = FALSE;
40      $form['#submit'][] = 'auto_nodetitle_node_form_submit';
41    }
42  }
43}
44
45/**
46 * Makes sure the node preview is shown right.
47 * It gets the node object, modifies the title, and updates the node in the form_state.
48 */
49function auto_nodetitle_node_form_submit($form, &$form_state) {
50  // Only do something, if the user clicked the preview button.
51  if (isset($form_state['clicked_button']['#submit']) && in_array('node_form_build_preview', $form_state['clicked_button']['#submit'])) {
52    $setting = auto_nodetitle_get_setting($form_state['values']['type']);
53    if ($setting == AUTO_NODETITLE_ENABLED || ($setting == AUTO_NODETITLE_OPTIONAL && empty($form_state['values']['title']))) {
54      $node = node_submit($form_state['values']);
55      auto_nodetitle_set_title($node);
56      $form_state['values'] = (array)$node;
57    }
58  }
59}
60
61/**
62 * Implementation of hook_nodeapi().
63 */
64function auto_nodetitle_nodeapi(&$node, $op) {
65  if ($op == 'presave' && auto_nodetitle_is_needed($node)) {
66    auto_nodetitle_set_title($node);
67  }
68}
69
70/**
71 * Returns whether the auto nodetitle has to be set.
72 */
73function auto_nodetitle_is_needed($node) {
74  return empty($node->auto_nodetitle_applied) && ($setting = auto_nodetitle_get_setting($node->type)) && !($setting == AUTO_NODETITLE_OPTIONAL && !empty($node->title));
75}
76
77/**
78 * Sets the automatically generated nodetitle for the node
79 */
80function auto_nodetitle_set_title(&$node) {
81  $types = node_get_types();
82  $pattern = variable_get('ant_pattern_'. $node->type, '');
83  if (trim($pattern)) {
84    $node->changed = time();
85    $node->title = _auto_nodetitle_patternprocessor($pattern, $node);
86  }
87  else if ($node->nid) {
88    $node->title = t('@type @node-id', array('@type' => $types[$node->type]->name, '@node-id' => $node->nid));
89  }
90  else {
91    $node->title = t('@type', array('@type' => $types[$node->type]->name));
92  }
93  // With that flag we ensure we don't apply the title two times to the same node.
94  $node->auto_nodetitle_applied = TRUE;
95}
96
97/**
98 * Implementation of hook_node_operations().
99 */
100function auto_nodetitle_node_operations() {
101  $operations = array(
102    'nodetitle_update' => array(
103      'label' => t('Update automatic nodetitles'),
104      'callback' => 'auto_nodetitle_operations_update',
105    ),
106  );
107  return $operations;
108}
109
110/**
111 * Callback function for updating node titles.
112 */
113function auto_nodetitle_operations_update($nodes) {
114  foreach ($nodes as $nid) {
115    $node = node_load($nid);
116    if ($node && auto_nodetitle_is_needed($node)) {
117      $previous_title = $node->title;
118      auto_nodetitle_set_title($node);
119      // Only save if the title has actually changed.
120      if ($node->title != $previous_title) {
121        node_save($node);
122      }
123    }
124  }
125}
126
127/**
128  * Helper function to generate the title according to the PHP code.
129  * Right now its only a wrapper, but if this is to be expanded, here is the place to be.
130  * @return a title string
131  */
132function _auto_nodetitle_patternprocessor($output, $node) {
133  if (module_exists('token')) {
134    $output = token_replace($output, 'node', $node);
135  }
136  if (variable_get('ant_php_'. $node->type, 0)) {
137    $output = auto_nodetitle_eval($output, $node);
138  }
139  if (variable_get('ant_php_'. $node->type, 0) || module_exists('token')) {
140    $output = preg_replace('/[\t\n\r\0\x0B]/', '', strip_tags($output));
141  }
142  return $output;
143}
144
145/**
146  * Helper function for hook_form_alter() renders the settings per node-type.
147  */
148function auto_nodetitle_node_settings_form(&$form) {
149  $form['auto_nodetitle'] = array(
150    '#type' => 'fieldset',
151    '#title' => t('Automatic title generation'),
152    '#weight' => 0,
153    '#collapsible' => TRUE,
154    '#collapsed' => TRUE,
155  );
156  $form['auto_nodetitle']['ant'] = array(
157    '#type' => 'radios',
158    '#default_value' => auto_nodetitle_get_setting($form['#node_type']->type),
159    '#options' => array(
160      t('Disabled'),
161      t('Automatically generate the title and hide the title field'),
162      t('Automatically generate the title if the title field is left empty'),
163    )
164  );
165
166  if (module_exists('token') || user_access('use PHP for title patterns')) {
167
168    $description = t('Leave blank for using the per default generated title. Otherwise this string will be used as title.');
169    if (module_exists('token')) {
170      $description .= ' '. t('Use the syntax [token] if you want to insert a replacement pattern.');
171    }
172    $form['auto_nodetitle']['ant_pattern'] = array(
173      '#type' => 'textarea',
174      '#title' => t('Pattern for the title'),
175      '#description' => $description,
176      '#default_value' => variable_get('ant_pattern_'. $form['#node_type']->type, ''),
177    );
178  }
179
180  if (module_exists('token')) {
181    $form['auto_nodetitle']['token_help'] = array(
182      '#title' => t('Replacement patterns'),
183      '#type' => 'fieldset',
184      '#collapsible' => TRUE,
185      '#collapsed' => TRUE,
186      '#description' => t('Prefer raw-text replacements for text to avoid problems with HTML entities!'),
187    );
188    $form['auto_nodetitle']['token_help']['help'] = array(
189      '#value' => theme('token_help', 'node'),
190    );
191  }
192  if (user_access('use PHP for title patterns')) {
193    $form['auto_nodetitle']['ant_php'] = array(
194      '#type' => 'checkbox',
195      '#title' => t('Evaluate PHP in pattern.'),
196      '#description' => t('Put PHP code above that returns your string, but make sure you surround code in &lt;?php and ?&gt;. Note that $node is available and can be used by your code.'),
197      '#default_value' => variable_get('ant_php_'. $form['#node_type']->type, ''),
198    );
199  }
200  else {
201    $form['auto_nodetitle']['auto_nodetitle_php'] = array(
202      '#type' => 'value',
203      '#value' => variable_get('ant_php_'. $form['#node_type']->type, ''),
204    );
205  }
206}
207
208/**
209 * Gets the auto node title setting associated with the given content type.
210 */
211function auto_nodetitle_get_setting($type) {
212  return variable_get('ant_'. $type, AUTO_NODETITLE_DISABLED);
213}
214
215/**
216 * Evaluates php code and passes $node to it
217 */
218function auto_nodetitle_eval($code, $node) {
219  ob_start();
220  print eval('?>'. $code);
221  $output = ob_get_contents();
222  ob_end_clean();
223  return $output;
224}
225
226/**
227 * Implementation of hook_node_type().
228 */
229function auto_nodetitle_node_type($op, $info) {
230  switch ($op) {
231    case 'delete':
232      variable_del('ant_'. $info->type);
233      variable_del('ant_pattern_'. $info->type);
234      variable_del('ant_php_'. $info->type);
235      break;
236    case 'update':
237      if (!empty($info->old_type) && $info->old_type != $info->type) {
238        variable_set('ant_'. $info->type, auto_nodetitle_get_setting($info->old_type));
239        variable_set('ant_pattern_'. $info->type, variable_get('ant_pattern_'. $info->old_type, ''));
240        variable_set('ant_php_'. $info->type, variable_get('ant_php_'. $info->old_type, ''));
241        variable_del('ant_'. $info->old_type);
242        variable_del('ant_pattern_'. $info->old_type);
243        variable_del('ant_php_'. $info->old_type);
244      }
245      break;
246  }
247}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.