You are here

protected function ComponentSectionForm::copyFormValuesToEntity in Module Builder 8.3

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

1 call to ComponentSectionForm::copyFormValuesToEntity()
ModuleNameForm::copyFormValuesToEntity in src/Form/ModuleNameForm.php
Copies top-level form values to entity properties
2 methods override ComponentSectionForm::copyFormValuesToEntity()
ModuleHooksForm::copyFormValuesToEntity in src/Form/ModuleHooksForm.php
Copies top-level form values to entity properties
ModuleNameForm::copyFormValuesToEntity in src/Form/ModuleNameForm.php
Copies top-level form values to entity properties

File

src/Form/ComponentSectionForm.php, line 950

Class

ComponentSectionForm
Generic form for entering a section of data for a component.

Namespace

Drupal\module_builder\Form

Code

protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
  $data = $form_state
    ->get('data');

  // EntityForm::submitForm() has already called $form_state->cleanValues().
  $data_values = $form_state
    ->getValue($data
    ->getName());
  $this
    ->cleanUpValues($data_values, $data);

  // We need to preserve any data for properties not on this form, but
  // completely clear properties that are on this form so we don't merge into
  // any existing data.
  // Note that $data is initially loaded from the component entity when the
  // form is built.
  $component_properties_to_use = $this
    ->getFormComponentProperties($data);
  foreach ($component_properties_to_use as $property_name) {
    $data
      ->removeItem($property_name);
  }
  try {
    $data
      ->set($data_values);
  } catch (InvalidInputException $e) {
    watchdog_exception('module_builder', $e);
    $this
      ->messenger()
      ->addError($this
      ->t("There was a problem with the form data. The component was not saved."));
    return;
  }

  // Set the name and ID, which use form elements outside of MTD.
  // This only applies to the 'name' component section form.
  if ($form_state
    ->getValue('id')) {
    $data->root_name = $form_state
      ->getValue('id');
    $data->readable_name = $form_state
      ->getValue('name');
  }

  // Let validation fill in required values with defaults, which we didn't
  // mark as required in the form.
  $data
    ->validate();
  $data_export = $data
    ->export();
  $entity
    ->set('data', $data_export);
}