Populates an entity object with values from a form submission.
Currently, this accounts for drag-and-drop reordering of field values, and filtering of empty values.
\Drupal\Core\Entity\EntityInterface $entity: The entity being submitted. The actual field values will be read from $form_state['values'].
$form: The form structure where field elements are attached to. This might be a full form structure, or a sub-element of a larger form.
$form_state: An associative array containing the current state of the form.
array $options: An associative array of additional options. See field_invoke_method() for details.
function field_attach_extract_form_values(EntityInterface $entity, $form, &$form_state, array $options = array()) {
// Ensure we are working with a BC mode entity.
$entity = $entity
->getBCEntity();
// Extract field values from submitted values.
$form_display = $form_state['form_display'];
field_invoke_method('extractFormValues', _field_invoke_widget_target($form_display), $entity, $form, $form_state, $options);
// Let other modules act on submitting the entity.
// Avoid module_invoke_all() to let $form_state be taken by reference.
foreach (module_implements('field_attach_extract_form_values') as $module) {
$function = $module . 'field_attach_extract_form_values';
$function($entity, $form, $form_state);
}
}