function FieldConfigEditForm::entityFormEntityBuild

Entity form builder for the field config edit form.

Parameters

string $entity_type_id: The entity type identifier.

\Drupal\field\FieldConfigInterface $entity: The entity updated with the submitted values.

array $form: The complete form array.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

See also

static::form

static::copyFormValuesToEntity

File

core/modules/field_ui/src/Form/FieldConfigEditForm.php, line 272

Class

FieldConfigEditForm
Provides a form for the field settings form.

Namespace

Drupal\field_ui\Form

Code

public function entityFormEntityBuild(string $entity_type_id, FieldConfigInterface $entity, array &$form, FormStateInterface &$form_state) : void {
  $previous_field_storage = $form_state->getFormObject()
    ->getEntity()
    ->getFieldStorageDefinition();
  assert($previous_field_storage instanceof FieldStorageConfigInterface);
  // Act on all sub-types of the entity_reference field type.
  $item_class = EntityReferenceItem::class;
  $class = $this->fieldTypePluginManager
    ->getPluginClass($entity->getFieldStorageDefinition()
    ->getType());
  if ($class !== $item_class && !is_subclass_of($class, $item_class)) {
    return;
  }
  // Update handler settings when target_type is changed.
  if ($entity->getFieldStorageDefinition()
    ->getSetting('target_type') !== $previous_field_storage->getSetting('target_type')) {
    // @see \Drupal\options\Hook\OptionsHooks::fieldStorageConfigUpdate()
    $entity->setSetting('handler_settings', []);
    // @see \Drupal\field\Hook\FieldHooks::fieldConfigPresave()
    $this->moduleHandler
      ->invoke('field', 'field_config_create', [
      $entity,
    ]);
    // Store updated settings in form state so that the form state can be
    // copied directly to the entity.
    $form_state->setValue('settings', $entity->getSettings());
    // Unset user input for the settings because they are not valid after the
    // target type has changed.
    $user_input = $form_state->getUserInput();
    unset($user_input['settings']);
    $form_state->setUserInput($user_input);
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.