I’ve tried to use entity wrapper to create a new node and insert the values into the fields, but I get an error.
<?php $values_entity = array( 'type' => 'anuncio', 'uid' => $userid, 'status' => 1, 'comment' => 1, 'promote' => 0, ); $entity = entity_create('node', $values_entity); // The entity is now created, but we have not yet simplified use of it. // Now create an entity_metadata_wrapper around the new node entity // to make getting and setting values easier $ewrapper = entity_metadata_wrapper('node', $entity); // Using the wrapper, we do not have to worry about telling Drupal // what language we are using. The Entity API handles that for us. $ewrapper->title->set($values['titulo']); $ewrapper ->field_fecha_inicio->set($values['fecha_inicio']); $ewrapper ->field_duraci_n->set($values['duracion']); $ewrapper ->field_tipo_anuncio->set($values['tipo_anuncio']); // Now just save the wrapper and the entity // There is some suggestion that the 'true' argument is necessary to // the entity save method to circumvent a bug in Entity API. If there is // such a bug, it almost certainly will get fixed, so make sure to check. $ewrapper->save(); ?>
EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. en EntityMetadataWrapper->set() (línea 122 de /opt/lampp/htdocs/tentu/ataria/sites/all/modules/entity/includes/entity.wrapper.inc).
I’ve tried also to insert the values of the field this way:
<?php $node = new stdClass(); $node->nid = NULL; $node->vid = NULL; $node->type = "anuncio"; // node_object_prepare($node); // Sets some defaults. Invokes hook_prepare() and hook_node_prepare(). $node->language = $language_url;// Or e.g. 'en' if locale is enabled $node->title = $values['titulo']; $node->uid = $userid; $node->status = 1; //(1 or 0): published or not $node->created = REQUEST_TIME; $node->changed = REQUEST_TIME; $node->promote = 0; //(1 or 0): promoted to front page $node->comment = 1; // 0 = comments disabled, 1 = read only, 2 = read/write $node->sticky =0; $node->field_fecha_inicio[$node->language][]['value'] = $values['fecha_inicio']; //$node->field_posicion2[$node->language][]['und'] = $form_state['values']['fecha_inicio']; $node->field_duraci_n[$node->language][]['value']= $values['duracion']; $node->field_municipio[$node->language][0]['tid'] = $values['municipios']; $node->field_tem_tica[$node->language][0]['tid'] = $values['tematicas']; $node->field_importe[$node->language][0]['value'] = $values['importe']; $node->field_texto_anuncio[$node->language][0]['value'] = $values['texto']; $node->field_g_nero[$node->language][0]['value'] = $values['genero']; $node->field_ubicacion_anuncio[$node->language][0]['value'] = $ubicacion; $node->field_tipo_anuncio [$node->language][0]/*['value']*/= $tipo; // Entity reference field // 'node' is default, // Other possible values are "user" and "taxonomy_term" node_submit($node); // Prepare node for saving node_save($node); ?>
I can create the node with the last code, but I can’t save the values into the fields. I have seen in my database that the name of the fields isn’t for example, field_fecha_inicio, instead, I have field_data_field_fecha_inicio and field_revision_field_fecha_inicio.
How can I insert the values into the fields?
ANSWERD:
$node = new stdClass(); $node->title = $values['titulo']; $node->language = $values['idioma']; $node->uid = $userid; $node->status = 1; //(1 or 0): published or not $node->promote = 0; //(1 or 0): promoted to front page $node->comment = 1; // 0 = comments disabled, 1 = read only, 2 = read/write $node->sticky = 0; $node->type = "anuncio"; node_object_prepare($node); $node->field_texto_anuncio[LANGUAGE_NONE][] = $values['texto']; $node->field_fecha_inicio[LANGUAGE_NONE][]['value'] = $values['fecha_inicio']; $node->field_duraci_n[LANGUAGE_NONE][]['value'] = $values['duracion']; $node->field_municipio[$values['idioma']][]['tid'] = $values['municipios']; $node->field_tem_tica[$values['idioma']][]['tid'] = $values['tematicas']; $node->field_importe[LANGUAGE_NONE][]['value'] = $values['importe']; $node->field_g_nero[LANGUAGE_NONE][]['tid']= $values['genero']; $node->field_url_del_anuncio[LANGUAGE_NONE][]['value'] = $values['url']; $node->field_ubicacion_anuncio[LANGUAGE_NONE][]['tid'] = $ubicacion; $node->field_tipo_anuncio[LANGUAGE_NONE][]['tid']= $tipo; $node = node_submit($node); // Prepare node for saving node_save($node);