source: sipes/modules_contrib/nodereference_autocreate/nodereference_autocreate.module @ 664c856

stableversion-3.0
Last change on this file since 664c856 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: 11.2 KB
Línea 
1<?php
2
3/**
4 * @file
5 * Defines a nodereference widget for auto-creating nodes. The widget
6 * auto-creates a node and then updates its title on change.
7 */
8
9/**
10 * Implementation of hook_widget_info().
11 */
12function nodereference_autocreate_widget_info() {
13  return array(
14    'nodereference_autocreate' => array(
15      'label' => t('Auto-create node'),
16      'field types' => array('nodereference'),
17      'multiple values' => CONTENT_HANDLE_CORE,
18      'callbacks' => array(
19        'default value' => CONTENT_CALLBACK_DEFAULT,
20      ),
21    ),
22  );
23}
24
25/**
26* Implementation of hook_widget_settings().
27*/
28function nodereference_autocreate_widget_settings($op, $widget) {
29  switch ($op) {
30    case 'form':
31      $types = content_types();
32      $options_fields = array();
33      foreach($types as $content_name => $content_type) {
34        $fields = $content_type['fields'];
35        foreach ($fields as $name => $field) {
36          //only allow field type text
37          if ($field['type'] == 'text') {
38            $options_fields[$field['field_name']] = strip_tags(t('Field %field in content type %content_type', array('%field' => check_plain(filter_xss($field['widget']['label'])), '%content_type' => check_plain(filter_xss($content_type['name'])))));
39          }
40        }
41        $fields = $content_type['extra'];
42        $exclude_fields = array(
43          'revision_information',
44          'author',
45          'options',
46          'comment_settings',
47          'menu',
48          'path',
49        );
50        foreach ($fields as $name => $field) {
51          if (!in_array($name, $exclude_fields)) {
52            $options_fields[$content_type['type']. '_' . $name] = strip_tags(t('Field %field in content type %content_type', array('%field' => check_plain(filter_xss($field['label'])), '%content_type' => check_plain(filter_xss($content_type['name'])))));
53          }
54        }
55      }
56      $option = (isset($widget['nodereference_save_field_type']) && $options_fields[$widget['nodereference_save_field_type']]) ? $widget['nodereference_save_field_type'] : '';
57      $form = array();
58
59      $form['nodereference_save_field'] = array(
60        '#type' => 'fieldset',
61        '#title' => t('Auto-created node based in fields'),
62        '#description' => t('Settings auto-created node based in fields.'),
63      );
64      $form['nodereference_save_field']['nodereference_save_field_type'] = array(
65        '#type' => 'select',
66        '#title' => t('Save data in content field'),
67        '#default_value' => $option,
68        '#options' => $options_fields,
69        '#required' => TRUE,
70      );
71      $options = array(
72        'text_textfield' => 'textfield',
73        'text_textarea' => 'textarea',
74      );
75      $option = (isset($widget['nodereference_field_type']) && $options[$widget['nodereference_field_type']]) ? $widget['nodereference_field_type'] : '';
76      $form['nodereference_save_field']['nodereference_field_type'] = array(
77        '#type' => 'select',
78        '#title' => t('Save data in content field'),
79        '#default_value' => $option,
80        '#options' => $options,
81        '#required' => TRUE,
82      );
83      $form['nodereference_save_field']['nodereference_title_default'] = array(
84        '#title' => t('Title default'),
85        '#type' => 'textfield',
86        '#default_value' => isset($widget['nodereference_title_default']) ? $widget['nodereference_title_default'] : NULL,
87        '#maxlength' => 255,
88      );
89      $form['nodereference_save_field']['nodereference_title_delta'] = array(
90        '#type' => 'checkbox',
91        '#title' => t('Add delta in title'),
92        '#default_value' => isset($widget['nodereference_title_delta']) ? $widget['nodereference_title_delta'] : NULL,
93        '#return_value' => 1,
94      );
95      return $form;
96    break;
97    case 'save':
98      return array(
99        'nodereference_save_field_type',
100        'nodereference_field_type',
101        'nodereference_title_default',
102        'nodereference_title_delta',
103      );
104    break;
105  }
106}
107
108/**
109 * Implementation of hook_widget().
110 */
111function nodereference_autocreate_widget(&$form, &$form_state, $field, $items, $delta = 0) {
112  switch ($field['widget']['type']) {
113    case 'nodereference_autocreate':
114      $element = array(
115        '#type' => 'text_textfield',
116        '#default_value' => isset($items[$delta]) ? $items[$delta] : NULL,
117        '#value_callback' => 'nodereference_autocreate_value',
118        '#process' => array('nodereference_autocreate_process'),
119      );
120      $element['#type'] = $field['widget']['nodereference_field_type'];
121      break;
122  }
123  return $element;
124}
125
126/**
127 * Value for a nodereference auto-create element.
128 *
129 * Substitute in the node title for the node ID.
130 */
131function nodereference_autocreate_value($element, $edit = FALSE) {
132  //get field data
133  $field_name = $element['#field_name'];
134  $type_name = $element['#type_name'];
135  $field = content_fields($field_name, $type_name);
136  $content_type = nodereference_autocreate_type_get($field['referenceable_types']);
137  $type = content_types($content_type);
138  $nodereference_save_field_type = $field['widget']['nodereference_save_field_type'];
139  $pos = strpos($nodereference_save_field_type, $content_type);
140  $field = $nodereference_save_field_type;
141  if ($pos == 0) {
142    $field = str_replace($content_type . '_', '', $nodereference_save_field_type);
143    $field = str_replace('_field', '', $field);
144  }
145
146  $field_key = $element['#columns'][0];
147  if ($nid = $element['#default_value'][$field_key]) {
148    if ($element['#nid'][$field_key]) {
149$nid = $element['#nid'][$field_key];
150}
151    $current_node = node_load($nid);
152    $value = $current_node->{$field};
153    $current_value = $value;
154    if (is_array($value)) {
155      $current_value = $value[0]['value'];
156    }
157    else {
158      $current_value = $value;
159    }
160    return array($field_key => $current_value);
161  }
162  return array($field_key => NULL);
163}
164
165/**
166 * Process an individual element.
167 *
168 * @see nodereference_autocomplete_process()
169 */
170function nodereference_autocreate_process($element, $edit, $form_state, $form) {
171  $field_key = $element['#columns'][0];
172  $element[$field_key] = array(
173    '#type' => $element['#type'],
174    '#default_value' => isset($element['#value']) ? $element['#value'] : '',
175    // The following values were set by the content module and need to be
176    // passed down to the nested element.
177    '#title' => $element['#title'],
178    '#required' => $element['#required'],
179    '#description' => $element['#description'],
180    '#field_name' => $element['#field_name'],
181    '#type_name' => $element['#type_name'],
182    '#delta' => $element['#delta'],
183    '#columns' => $element['#columns'],
184    '#nid' => 'casaloca',
185  );
186  if (empty($element[$field_key]['#element_validate'])) {
187    $element[$field_key]['#element_validate'] = array();
188  }
189  array_unshift($element[$field_key]['#element_validate'], 'nodereference_autocreate_validate');
190  return $element;
191}
192
193/**
194 * Validate an individual element.
195 *
196 * Create a node with the specified value on first save. Update title of
197 * referenced node on each additional save.
198 */
199function nodereference_autocreate_validate($element, &$form_state) {
200  $field_name = $element['#field_name'];
201  $type_name = $element['#type_name'];
202  $field = content_fields($field_name, $type_name);
203  $field_key  = $element['#columns'][0];
204  $delta = $element['#delta'];
205  $value = $element['#value'][$field_key];
206  $nid = NULL;
207  if ($value) {
208    // Load the current value stored in the database.
209    $self_nid = $form_state['values']['nid'];
210    //get nid if lost(case editablefields)
211    if(!$self_nid && $form_state['node']->nid) {
212      $self_nid = $form_state['node']->nid;
213    }
214    $self = node_load($self_nid);
215    $current_nid = $self->$field_name;
216    $current_nid = $current_nid[$delta]['nid'];
217    $default_title = 'node';
218    if (trim($field['widget']['nodereference_title_default'])) {
219      $default_title = $field['widget']['nodereference_title_default'];
220    }
221    if ($field['widget']['nodereference_title_delta']) {
222      $count = $delta + 1;
223      $default_title .= $count;
224    }
225    $default_title = check_plain($default_title);
226    $nodereference_save_field_type = $field['widget']['nodereference_save_field_type'];
227    $content_type = nodereference_autocreate_type_get($field['referenceable_types']);
228    $pos = strpos($nodereference_save_field_type, $content_type);
229    $replace_field = $nodereference_save_field_type;
230    $extra_field = FALSE;
231    if ($pos == 0) {
232      $replace_field = str_replace($content_type . '_', '', $nodereference_save_field_type);
233      $replace_field = str_replace('_field', '', $replace_field);
234      $extra_node->$replace_field = $value;
235    }
236    else {
237      $extra_node->$replace_field[0]['value'] = $value;
238    }
239    if ($current_nid) {
240      // Referenced node exists, update its title if changed.
241      $referenced_node = node_load($current_nid);
242      if ($referenced_node->{$replace_field} != $extra_node->{$replace_field} || $referenced_node->title != $default_title) {
243        $referenced_node->title = $default_title;
244        $referenced_node->$replace_field = $extra_node->$replace_field;
245        node_save($referenced_node);
246      }
247      $nid = $referenced_node->nid;
248    }
249    else {
250      // Create a new node with the specified title.
251      global $user;
252      $node = new stdClass();
253      $node->type = nodereference_autocreate_type_get($field['referenceable_types']);
254      $node->title = $default_title;
255      $node->uid = $user->uid;
256      $node->{$replace_field} = $extra_node->{$replace_field};
257      node_save($node);
258      if (!$node->nid) {
259        form_error($element[$field_key], t('%name: failed to create sub node.', array('%name' => t($field['widget']['label']))));
260      }
261      else {
262        $nid = $node->nid;
263      }
264    }
265  }
266  form_set_value($element, $nid, $form_state);
267}
268
269/**
270 * Implementation of hook_form_FORM_ID_alter(): content_field_edit_form.
271 */
272function nodereference_autocreate_form_content_field_edit_form_alter(&$form, $form_state) {
273  if ($form['#field']['widget']['type'] == 'nodereference_autocreate') {
274    $form['field']['referenceable_types']['#description'] .= t('Only one type can be referenced when using the ' .
275      'auto-create node widget. The node type referenced will be used when creating a node.');
276    $form['#validate'][] = 'nodereference_autocreate_content_field_edit_form_validate';
277  }
278}
279
280/**
281 * Validate nodereference setting form.
282 *
283 * If widget type is nodereference_autocreate then check for multiple selected
284 * referenceable types.
285 */
286function nodereference_autocreate_content_field_edit_form_validate($form, &$form_state) {
287  if ($form_state['values']['widget_type'] == 'nodereference_autocreate') {
288    $count = 0;
289    if ($form_state['values']['referenceable_types']) {
290      foreach ($form_state['values']['referenceable_types'] as $type => $enabled) {
291        if ($enabled) {
292          $count++;
293        }
294      }
295      if ($count > 1) {
296        form_set_error('referenceable_types', t('Only one content type can be referenced for widgets of type <em>Auto-create node</em>.'));
297      }
298    }
299  }
300}
301
302/**
303 * Get the referenced type.
304 *
305 * @param $referenceable_types
306 *   Referenceable types from field form.
307 * @return
308 *   Referenced type.
309 */
310function nodereference_autocreate_type_get(array $referenceable_types) {
311  foreach ($referenceable_types as $type => $enabled) {
312    if ($enabled) {
313      return $type;
314    }
315  }
316  return NULL;
317}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.