You are here

protected function EntityDisplayFormBase::copyFormValuesToEntity in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field_ui/src/Form/EntityDisplayFormBase.php \Drupal\field_ui\Form\EntityDisplayFormBase::copyFormValuesToEntity()

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 EntityDisplayFormBase::copyFormValuesToEntity()
LayoutBuilderEntityViewDisplayForm::copyFormValuesToEntity in core/modules/layout_builder/src/Form/LayoutBuilderEntityViewDisplayForm.php
Copies top-level form values to entity properties.
1 method overrides EntityDisplayFormBase::copyFormValuesToEntity()
LayoutBuilderEntityViewDisplayForm::copyFormValuesToEntity in core/modules/layout_builder/src/Form/LayoutBuilderEntityViewDisplayForm.php
Copies top-level form values to entity properties.

File

core/modules/field_ui/src/Form/EntityDisplayFormBase.php, line 584

Class

EntityDisplayFormBase
Base class for EntityDisplay edit forms.

Namespace

Drupal\field_ui\Form

Code

protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
  $form_values = $form_state
    ->getValues();
  if ($this->entity instanceof EntityWithPluginCollectionInterface) {

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

  // Collect data for 'regular' fields.
  foreach ($form['#fields'] as $field_name) {
    $values = $form_values['fields'][$field_name];
    if ($values['region'] == 'hidden') {
      $entity
        ->removeComponent($field_name);
    }
    else {
      $options = $entity
        ->getComponent($field_name);

      // Update field settings only if the submit handler told us to.
      if ($form_state
        ->get('plugin_settings_update') === $field_name) {

        // Only store settings actually used by the selected plugin.
        $default_settings = $this->pluginManager
          ->getDefaultSettings($options['type']);
        $options['settings'] = isset($values['settings_edit_form']['settings']) ? array_intersect_key($values['settings_edit_form']['settings'], $default_settings) : [];
        $options['third_party_settings'] = isset($values['settings_edit_form']['third_party_settings']) ? $values['settings_edit_form']['third_party_settings'] : [];
        $form_state
          ->set('plugin_settings_update', NULL);
      }
      $options['type'] = $values['type'];
      $options['weight'] = $values['weight'];
      $options['region'] = $values['region'];

      // Only formatters have configurable label visibility.
      if (isset($values['label'])) {
        $options['label'] = $values['label'];
      }
      $entity
        ->setComponent($field_name, $options);
    }
  }

  // Collect data for 'extra' fields.
  foreach ($form['#extra'] as $name) {
    if ($form_values['fields'][$name]['region'] == 'hidden') {
      $entity
        ->removeComponent($name);
    }
    else {
      $entity
        ->setComponent($name, [
        'weight' => $form_values['fields'][$name]['weight'],
        'region' => $form_values['fields'][$name]['region'],
      ]);
    }
  }
}