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.
$node_wrapper->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->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.