You are here

public function ModuleBuilderComponentFormBase::copyFormValuesToEntity in Module Builder 7.2

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

includes/module_builder.form.inc, line 661

Class

ModuleBuilderComponentFormBase
Backport of ComponentFormBase from 8.x-3.x version.

Code

public function copyFormValuesToEntity(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());
  }

  if ($this->entity->isNew()) {
    $data = [];
  }
  else {
    // Add to the existing entity data array.
    $data = $entity->get('data');
  }
  */
  $data = [];
  foreach ($values['data'] as $key => $value) {

    // TODO kill on D8 too!
    // $form_element = $form['data'][$key];
    $value_address = [
      'data',
      $key,
    ];
    $value = $this
      ->getFormElementValue($value_address, $value, $form_state);
    if (empty($value)) {
      unset($data[$key]);
    }
    else {
      $data[$key] = $value;
    }
  }

  //$entity->set('data', $data);
  return $data;
}