4

I try to programmatically modify the body of a node using an entity wrapper.
In my Drupal installation I also use Locale module (language for node is 'en', for body it is 'und') and Revisioning module.

Now, I've created a simple code to do this task:

<?php
// in database: field_data_body->body_value = 'old_value'
$node = node_load(1);

$wrapper = entity_metadata_wrapper('node', $node);

print_r($wrapper->body->value->value());   // prints 'old_value'
$wrapper->body->value = 'new_value';
print_r($wrapper->body->value->value());   // still prints 'old_value'

$wrapper->save();
node_save($node);
?>

yet it seems not to make any changes to the node.

Although, if I've tried to change the title:

$wrapper->title = 'ABC';

everyting worked fine.

I guess it's the problem related to using localization, but I can't find sufficient documentation on how to achieve what I want.

Could someone tell me how can I put it to work?

4
  • I have also tried with this code, but I didn't get the Entity API module to save the node with the new body.
    – apaderno
    Jul 7, 2012 at 14:08
  • The upper method (the "normal" one) works fine if I switch Locale module off... Jul 7, 2012 at 17:15
  • Do you mean that $wrapper->body->value = 'new_value'; work if the Locale module is disabled? In that case, it sounds like a bug in the module. I didn't notice this, but in my test site the Locale module is enabled too.
    – apaderno
    Jul 7, 2012 at 17:38
  • i tried this to update the body of a node. i'm getting segmentation fault when i save the wrapper. any solution to this?
    – Bej
    Jan 11, 2017 at 12:23

1 Answer 1

8

Documentation is up to date now.

Examples from drupal.org :

 $wrapper->author = 0;
 $wrapper->url = 'dummy';
 $wrapper->author->mail = 'foo';
 $wrapper->author = NULL;
 $wrapper->author->set($GLOBALS['user']->uid);
 $wrapper->author->set($GLOBALS['user']);

 // Localization Here
 $wrapper->body->set(array('value' => "<b>The second body.</b>"));
 $wrapper->language('de');
 $wrapper->body->set(array('value' => "<b>Der zweite Text.</b>"));
 $wrapper->language(LANGUAGE_NONE);
 $wrapper->set($node);

 $wrapper->reference->name->set('foo');
 $wrapper->reference->set($wrapper);
 $wrapper->save();
 $wrapper->delete();
 $wrapper->type->set('article');
 $wrapper->author = $user->uid;
 $wrapper->{$property}[0] = '2009-10-05';
 $wrapper->field_image = array('fid' => $file->fid);
 $wrapper->$name->value();
 $wrapper->parent[] = $term_parent2;
 $wrapper->field_tags[1] = $term_parent;
 $wrapper->field_tags->set(array(2));
 $wrapper->field_tags = NULL;
 $wrapper->$field_name->set(NULL);
 $wrapper->field_text[0] = 'the text';
 $wrapper->field_text[0]->set(array('value' => "<b>The second body.</b>"));
 $wrapper->field_text2->summary = 'the summary';
 $wrapper->field_text2->value = 'the text';
 $wrapper->field_file[0] = array('fid' => $file->fid, 'display' => FALSE);
 $wrapper->field_file[0]->description = 'foo';
 $wrapper->field_file[0]->display = TRUE;
 $wrapper->field_file[1]->file = $file;
 $wrapper->field_file[] = array('description' => 'test');
 $wrapper->field_file = NULL;
 $wrapper->field_image = array('fid' => $file->fid);
 $wrapper->field_image->alt = 'foo';
 $wrapper->field_image->file = $file;
 $wrapper->field_image = array();

You can follow this link to see full documentation and examples about entity metadata wrappers and localization.

Documentation about entity metadata wrappers on Drupal.org

Your Answer

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

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