Ignorar:
Fecha y hora:
30/05/2016 10:53:19 (hace 8 años)
Autor:
José Gregorio Puentes <jpuentes@…>
Branches:
stable, version-3.0
Children:
e69205c
Parents:
664c856
Mensaje:

se actualizo el modulo

Fichero:
1 editado

Leyenda

No modificado
Añadido
Eliminado
  • modules_contrib/nodereference_autocreate/nodereference_autocreate.module

    r177a560 r8f5c26c  
    2424
    2525/**
    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 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 /**
    10926 * Implementation of hook_widget().
    11027 */
     
    11835        '#process' => array('nodereference_autocreate_process'),
    11936      );
    120       $element['#type'] = $field['widget']['nodereference_field_type'];
    12137      break;
    12238  }
     
    13046 */
    13147function 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 
    14648  $field_key = $element['#columns'][0];
    14749  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);
    16152  }
    16253  return array($field_key => NULL);
     
    17061function nodereference_autocreate_process($element, $edit, $form_state, $form) {
    17162  $field_key = $element['#columns'][0];
     63
    17264  $element[$field_key] = array(
    173     '#type' => $element['#type'],
     65    '#type' => 'text_textfield',
    17466    '#default_value' => isset($element['#value']) ? $element['#value'] : '',
    17567    // The following values were set by the content module and need to be
     
    18274    '#delta' => $element['#delta'],
    18375    '#columns' => $element['#columns'],
    184     '#nid' => 'casaloca',
    18576  );
    18677  if (empty($element[$field_key]['#element_validate'])) {
     
    18879  }
    18980  array_unshift($element[$field_key]['#element_validate'], 'nodereference_autocreate_validate');
     81
    19082  return $element;
    19183}
     
    20597  $value = $element['#value'][$field_key];
    20698  $nid = NULL;
     99
    207100  if ($value) {
    208101    // 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']);
    215103    $current_nid = $self->$field_name;
    216104    $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
    239106    if ($current_nid) {
    240107      // Referenced node exists, update its title if changed.
    241108      $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;
    245111        node_save($referenced_node);
    246112      }
     113
    247114      $nid = $referenced_node->nid;
    248115    }
    249116    else {
    250117      // Create a new node with the specified title.
    251       global $user;
    252118      $node = new stdClass();
    253119      $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;
    257121      node_save($node);
     122
    258123      if (!$node->nid) {
    259124        form_error($element[$field_key], t('%name: failed to create sub node.', array('%name' => t($field['widget']['label']))));
     
    274139    $form['field']['referenceable_types']['#description'] .= t('Only one type can be referenced when using the ' .
    275140      'auto-create node widget. The node type referenced will be used when creating a node.');
     141
    276142    $form['#validate'][] = 'nodereference_autocreate_content_field_edit_form_validate';
    277143  }
     
    293159        }
    294160      }
     161
    295162      if ($count > 1) {
    296163        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.