Through searching, I've found plenty of examples using the EntityMetadataWrapper set() function to update entity fields, and have implemented it successfully in my module. However, I haven't been able to find much in the way of documentation / best practices on the different array options you can pass to set(). I want to make sure the way I'm updating data won't cause unforeseen issues later on.

I am doing a straightforward update of text field values in a form submit callback:

function hook_FORM_ID_submit(&$form, &$form_state) {
  // Get the node.
  $node = node_load($form_state['nid']);

  // Create entity metadata wrapper to get and set values easily.
  $node_wrapper = entity_metadata_wrapper('node', $node, array('bundle', $node->type));

  // Set fields
  $node_wrapper->field_quip->set(array("value" => $form_state['values']['field_quip']['value']));
  $node_wrapper->field_short_sentence_summary->set(array("value" => $form_state['values']['field_short_sentence_summary']['value']));

  // Save node.

I am passing set() an array with only one key: value. This seems to be working fine. However, I notice that when I call value() on that same field before I set a new value, it returns an array with three keys:

$original_value = $node_wrapper->field_quip->value();

// Output of $original_value:
// Array (
//   'value' => '<p>The original value</p>',
//   'format' => 'general',
//   'safe_value' => '<p>The original safe value</p>',
// )

The way I am current setting fields, I lose the sanitized value. Will this come back to bite me? Should I be running check_markup() on the value and passing format and safe_value to set(), rather than an array with just value? I might be splitting hairs here, but I want to make sure I do things right.

1 Answer 1


It turns out that if you just set the value, Drupal will not display your HTML without converting your tags to entities. So the value <p>Foo bar</p> will be displayed literally in Views (and other areas) as &lt;p&gt;Foo bar&lt;/p&gt;.

Rather than attempt to create a sanitized safe_value myself before calling set(), I found that if I passed a format value, Drupal will take care of the sanitization for me with the filter settings defined for that format.

So the correct syntax is:

$quip = array(
  "value"  => $form_state['values']['field_quip']['value'],
  // Change to match the text format of your field.
  "format" => "general"

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.