0

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);

2 Answers 2

0

The structure in your second code block looks correct to me.

To see what the problem is first create a node of type 'anuncio' using the UI in the backend of Drupal. Now use devel (or just print_r) to inspect the node you just made.

dmp(node_load(NEW_NODE_ID));

Just before you save your new node you can inspect it too:

dpm($node);

The new node you are creating needs to match the structure of this node - any differences here tells you what bit is wrong.

EDIT:

In your second code block you don't need these lines:

$node->nid = NULL;
$node->vid = NULL;
$node->created = REQUEST_TIME;
$node->changed = REQUEST_TIME;

These will all be automatically set so you don't need to set their values.

Also look at the example here.

You need:

$node = node_submit($node);

and your also missing:

node_object_prepare($node);

I've edited your code slightly, I think this should work:

$node = new stdClass();
$node->title = $values['titulo'];
$node->type = "anuncio";
node_object_prepare($node);

$node->language = $language_url;
$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->field_fecha_inicio[$node->language][]['value'] = $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;

$node = node_submit($node); // Prepare node for saving
node_save($node);

EDIT 2:

So, if the fields are not getting populated then there is something wrong with this line (let's just focus on 1 field at a time):

$node->field_fecha_inicio[$node->language][]['value'] = $values['fecha_inicio'];

Initially I would try hard coding the values to get it to work, so see if this line works:

$node->field_fecha_inicio[LANGUAGE_NONE][0]['value'] = 'my test value';

Does this line work?

17
  • The structure of the node is the next one: [1]: i.stack.imgur.com/vykqA.png
    – more770
    Mar 28, 2014 at 10:03
  • I've change what you have asked but it doesn't work. Maybe I've done something wrong from the begining. I've created a content type like the next one : [1]: i.stack.imgur.com/WKtVo.png . My object is to insert into that fields the data programmaticaly
    – more770
    Mar 28, 2014 at 10:26
  • I've updated my answer - does the code I've posted work?
    – Felix Eve
    Mar 28, 2014 at 10:48
  • I've insert your code but it doesn't work. I've two question: 1- When I have created the content type I have created the fields into a group, like in the picture that I have added in the second comment. Can this afect when insert the value into the fields? 2- I can only see in the database, for example, field_data_field_fecha_inicio and field_revision_field_fecha_inicio. Maybe I have to insert the value into one of this tables?
    – more770
    Mar 28, 2014 at 11:07
  • Grouping fields doesn't affect the structure of the node. You can see this by inspecting a node created though the admin interface. You don't need to manually insert into any DB tables that's what the API is for. When you call node_save it will do all of the inserts for you.
    – Felix Eve
    Mar 28, 2014 at 11:33
0

Hope this function will help you. I am creating a basic node of type page.

/**
 * Create a custom node.
 *
 * @param string $type
 *   Type of node (bundle).
 */
function _custom_work_create_a_node($type = 'page') {
  global $user;
  $node = entity_create('node', array(
     'type' => $type,
     'uid' => $user->uid,
    ));
  $wrapper = entity_metadata_wrapper('node', $node);
  $wrapper->title->set('Demo title');
  $wrapper->body->value->set('body value');
  $wrapper->save();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.