source: sipes/modules_contrib/nodereference_autocreate/nodereference_autocreate.module @ a8b1f3f

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

se actualizo el modulo

  • Propiedad mode establecida a 100755
File size: 5.5 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().
27 */
28function nodereference_autocreate_widget(&$form, &$form_state, $field, $items, $delta = 0) {
29  switch ($field['widget']['type']) {
30    case 'nodereference_autocreate':
31      $element = array(
32        '#type' => 'text_textfield',
33        '#default_value' => isset($items[$delta]) ? $items[$delta] : NULL,
34        '#value_callback' => 'nodereference_autocreate_value',
35        '#process' => array('nodereference_autocreate_process'),
36      );
37      break;
38  }
39  return $element;
40}
41
42/**
43 * Value for a nodereference auto-create element.
44 *
45 * Substitute in the node title for the node ID.
46 */
47function nodereference_autocreate_value($element, $edit = FALSE) {
48  $field_key = $element['#columns'][0];
49  if ($nid = $element['#default_value'][$field_key]) {
50    $value = db_result(db_query(db_rewrite_sql('SELECT n.title FROM {node} n WHERE n.nid = %d'), $nid));
51    return array($field_key => $value);
52  }
53  return array($field_key => NULL);
54}
55
56/**
57 * Process an individual element.
58 *
59 * @see nodereference_autocomplete_process()
60 */
61function nodereference_autocreate_process($element, $edit, $form_state, $form) {
62  $field_key = $element['#columns'][0];
63
64  $element[$field_key] = array(
65    '#type' => 'text_textfield',
66    '#default_value' => isset($element['#value']) ? $element['#value'] : '',
67    // The following values were set by the content module and need to be
68    // passed down to the nested element.
69    '#title' => $element['#title'],
70    '#required' => $element['#required'],
71    '#description' => $element['#description'],
72    '#field_name' => $element['#field_name'],
73    '#type_name' => $element['#type_name'],
74    '#delta' => $element['#delta'],
75    '#columns' => $element['#columns'],
76  );
77  if (empty($element[$field_key]['#element_validate'])) {
78    $element[$field_key]['#element_validate'] = array();
79  }
80  array_unshift($element[$field_key]['#element_validate'], 'nodereference_autocreate_validate');
81
82  return $element;
83}
84
85/**
86 * Validate an individual element.
87 *
88 * Create a node with the specified value on first save. Update title of
89 * referenced node on each additional save.
90 */
91function nodereference_autocreate_validate($element, &$form_state) {
92  $field_name = $element['#field_name'];
93  $type_name = $element['#type_name'];
94  $field = content_fields($field_name, $type_name);
95  $field_key  = $element['#columns'][0];
96  $delta = $element['#delta'];
97  $value = $element['#value'][$field_key];
98  $nid = NULL;
99
100  if ($value) {
101    // Load the current value stored in the database.
102    $self = node_load($form_state['values']['nid']);
103    $current_nid = $self->$field_name;
104    $current_nid = $current_nid[$delta]['nid'];
105
106    if ($current_nid) {
107      // Referenced node exists, update its title if changed.
108      $referenced_node = node_load($current_nid);
109      if ($referenced_node->title != $value) {
110        $referenced_node->title = $value;
111        node_save($referenced_node);
112      }
113
114      $nid = $referenced_node->nid;
115    }
116    else {
117      // Create a new node with the specified title.
118      $node = new stdClass();
119      $node->type = nodereference_autocreate_type_get($field['referenceable_types']);
120      $node->title = $value;
121      node_save($node);
122
123      if (!$node->nid) {
124        form_error($element[$field_key], t('%name: failed to create sub node.', array('%name' => t($field['widget']['label']))));
125      }
126      else {
127        $nid = $node->nid;
128      }
129    }
130  }
131  form_set_value($element, $nid, $form_state);
132}
133
134/**
135 * Implementation of hook_form_FORM_ID_alter(): content_field_edit_form.
136 */
137function nodereference_autocreate_form_content_field_edit_form_alter(&$form, $form_state) {
138  if ($form['#field']['widget']['type'] == 'nodereference_autocreate') {
139    $form['field']['referenceable_types']['#description'] .= t('Only one type can be referenced when using the ' .
140      'auto-create node widget. The node type referenced will be used when creating a node.');
141
142    $form['#validate'][] = 'nodereference_autocreate_content_field_edit_form_validate';
143  }
144}
145
146/**
147 * Validate nodereference setting form.
148 *
149 * If widget type is nodereference_autocreate then check for multiple selected
150 * referenceable types.
151 */
152function nodereference_autocreate_content_field_edit_form_validate($form, &$form_state) {
153  if ($form_state['values']['widget_type'] == 'nodereference_autocreate') {
154    $count = 0;
155    if ($form_state['values']['referenceable_types']) {
156      foreach ($form_state['values']['referenceable_types'] as $type => $enabled) {
157        if ($enabled) {
158          $count++;
159        }
160      }
161
162      if ($count > 1) {
163        form_set_error('referenceable_types', t('Only one content type can be referenced for widgets of type <em>Auto-create node</em>.'));
164      }
165    }
166  }
167}
168
169/**
170 * Get the referenced type.
171 *
172 * @param $referenceable_types
173 *   Referenceable types from field form.
174 * @return
175 *   Referenced type.
176 */
177function nodereference_autocreate_type_get(array $referenceable_types) {
178  foreach ($referenceable_types as $type => $enabled) {
179    if ($enabled) {
180      return $type;
181    }
182  }
183  return NULL;
184}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.