EntityDisplayFormBase::copyFormValuesToEntity

protected EntityDisplayFormBase::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/modules/field_ui/src/Form/EntityDisplayFormBase.php, line 541

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['type'] == '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'];
      // 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]['type'] == 'hidden') {
      $entity->removeComponent($name);
    }
    else {
      $entity->setComponent($name, array(
        'weight' => $form_values['fields'][$name]['weight'],
      ));
    }
  }
}
doc_Drupal
2016-10-29 09:05:23
Comments
Leave a Comment

Please login to continue.