Insert values into a node fields with entity wrapper

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); 
This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Insert values into a node fields with entity wrapper

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.