I am creating nodes programmatically. I Have written following code,

$e = entity_create('node', array('type' => 'some_node'));
// Specify the author.
$e->uid = 1;
// Create a Entity Wrapper of that new Entity.
$entity = entity_metadata_wrapper('node', $e);

// Specify the title.
$entity->title = 'Some Title';
$entity->body = 'abc';

On giving value to body i am getting following error. Commenting body it works fine

EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. in EntityMetadataWrapper->set()

What is causing this error and how can I fix it?


Body is a Field API field and not a native PHP variable, therefore you cannot assign values to it like that. Instead, you would need to pass an array with the following keys:

  'value' => 'This is the body',
  'summary' => 'This is the summary',
  'format' => 'full_html',

If you wish to set the value for a specific language, you call prior to set:

$entity->language(LANGUAGE_NONE); // or 'en' or whatever

More on wrappers: https://drupal.org/node/1021556

  • Sorry its not working i am getting following error after using your code Fatal error: Cannot use object of type EntityStructureWrapper as array in
    – Ali Nouman
    Feb 25 '14 at 11:05
  • Right, it's a wrapper, my bad. Updated.
    – Wtower
    Feb 25 '14 at 11:19

Try using below code

$entity->body->set(array('value' => "abc"));
  • Thanks any idea how can i define input format so that it could accept html tags?
    – Ali Nouman
    Feb 25 '14 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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