Sanitized text input for EntityMetadataWrapper set() 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(&amp;$form, &amp;$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-&gt;type));

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

  // Save node.
  $node_wrapper-&gt;save();
}


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-&gt;field_quip-&gt;value();

// Output of $original_value:
// Array (
//   'value' =&gt; '&lt;p&gt;The original value&lt;/p&gt;',
//   'format' =&gt; 'general',
//   'safe_value' =&gt; '&lt;p&gt;The original safe value&lt;/p&gt;',
// )


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.
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 &lt;p&gt;Foo bar&lt;/p&gt; will be displayed literally in Views (and other areas) as &amp;lt;p&amp;gt;Foo bar&amp;lt;/p&amp;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"  =&gt; $form_state['values']['field_quip']['value'],
  // Change to match the text format of your field.
  "format" =&gt; "general"
);
$node_wrapper-&gt;field_quip-&gt;set($quip);