W3cubDocs

/Drupal 8

protected function EntityForm::copyFormValuesToEntity

protected EntityForm::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.

File

core/lib/Drupal/Core/Entity/EntityForm.php, line 317

Class

EntityForm
Base class for entity forms.

Namespace

Drupal\Core\Entity

Code

protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
  $values = $form_state->getValues();

  if ($this->entity instanceof EntityWithPluginCollectionInterface) {
    // Do not manually update values represented by plugin collections.
    $values = array_diff_key($values, $this->entity->getPluginCollections());
  }

  // @todo: This relies on a method that only exists for config and content
  //   entities, in a different way. Consider moving this logic to a config
  //   entity specific implementation.
  foreach ($values as $key => $value) {
    $entity->set($key, $value);
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!EntityForm.php/function/EntityForm::copyFormValuesToEntity/8.1.x