ContentEntityForm::copyFormValuesToEntity

protected ContentEntityForm::copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state)

Copies top-level form values to entity properties

This should not change existing entity properties that are not being edited by this form.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity the current form should operate upon.

array $form: A nested array of form elements comprising the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides EntityForm::copyFormValuesToEntity

File

core/lib/Drupal/Core/Entity/ContentEntityForm.php, line 221

Class

ContentEntityForm
Entity form variant for content entity types.

Namespace

Drupal\Core\Entity

Code

protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
  // First, extract values from widgets.
  $extracted = $this->getFormDisplay($form_state)->extractFormValues($entity, $form, $form_state);

  // Then extract the values of fields that are not rendered through widgets,
  // by simply copying from top-level form values. This leaves the fields
  // that are not being edited within this form untouched.
  foreach ($form_state->getValues() as $name => $values) {
    if ($entity->hasField($name) && !isset($extracted[$name])) {
      $entity->set($name, $values);
    }
  }
}
doc_Drupal
2016-10-29 08:57:14
Comments
Leave a Comment

Please login to continue.