Conjunto de cambios 8f5c26c en sipes para modules_contrib/nodereference_autocreate/nodereference_autocreate.module
- Fecha y hora:
- 30/05/2016 10:53:19 (hace 8 años)
- Branches:
- stable, version-3.0
- Children:
- e69205c
- Parents:
- 664c856
- Fichero:
-
- 1 editado
Leyenda
- No modificado
- Añadido
- Eliminado
-
modules_contrib/nodereference_autocreate/nodereference_autocreate.module
r177a560 r8f5c26c 24 24 25 25 /** 26 * Implementation of hook_widget_settings().27 */28 function 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 text37 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 26 * Implementation of hook_widget(). 110 27 */ … … 118 35 '#process' => array('nodereference_autocreate_process'), 119 36 ); 120 $element['#type'] = $field['widget']['nodereference_field_type'];121 37 break; 122 38 } … … 130 46 */ 131 47 function nodereference_autocreate_value($element, $edit = FALSE) { 132 //get field data133 $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 48 $field_key = $element['#columns'][0]; 147 49 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); 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); 161 52 } 162 53 return array($field_key => NULL); … … 170 61 function nodereference_autocreate_process($element, $edit, $form_state, $form) { 171 62 $field_key = $element['#columns'][0]; 63 172 64 $element[$field_key] = array( 173 '#type' => $element['#type'],65 '#type' => 'text_textfield', 174 66 '#default_value' => isset($element['#value']) ? $element['#value'] : '', 175 67 // The following values were set by the content module and need to be … … 182 74 '#delta' => $element['#delta'], 183 75 '#columns' => $element['#columns'], 184 '#nid' => 'casaloca',185 76 ); 186 77 if (empty($element[$field_key]['#element_validate'])) { … … 188 79 } 189 80 array_unshift($element[$field_key]['#element_validate'], 'nodereference_autocreate_validate'); 81 190 82 return $element; 191 83 } … … 205 97 $value = $element['#value'][$field_key]; 206 98 $nid = NULL; 99 207 100 if ($value) { 208 101 // 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); 102 $self = node_load($form_state['values']['nid']); 215 103 $current_nid = $self->$field_name; 216 104 $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 } 105 239 106 if ($current_nid) { 240 107 // Referenced node exists, update its title if changed. 241 108 $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; 109 if ($referenced_node->title != $value) { 110 $referenced_node->title = $value; 245 111 node_save($referenced_node); 246 112 } 113 247 114 $nid = $referenced_node->nid; 248 115 } 249 116 else { 250 117 // Create a new node with the specified title. 251 global $user;252 118 $node = new stdClass(); 253 119 $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}; 120 $node->title = $value; 257 121 node_save($node); 122 258 123 if (!$node->nid) { 259 124 form_error($element[$field_key], t('%name: failed to create sub node.', array('%name' => t($field['widget']['label'])))); … … 274 139 $form['field']['referenceable_types']['#description'] .= t('Only one type can be referenced when using the ' . 275 140 'auto-create node widget. The node type referenced will be used when creating a node.'); 141 276 142 $form['#validate'][] = 'nodereference_autocreate_content_field_edit_form_validate'; 277 143 } … … 293 159 } 294 160 } 161 295 162 if ($count > 1) { 296 163 form_set_error('referenceable_types', t('Only one content type can be referenced for widgets of type <em>Auto-create node</em>.'));
Nota: Vea TracChangeset
para ayuda en el uso del visor de conjuntos de cambios.