function FieldConfigDeleteForm::submitForm

Same name in other branches
  1. 9 core/modules/field_ui/src/Form/FieldConfigDeleteForm.php \Drupal\field_ui\Form\FieldConfigDeleteForm::submitForm()
  2. 8.9.x core/modules/field_ui/src/Form/FieldConfigDeleteForm.php \Drupal\field_ui\Form\FieldConfigDeleteForm::submitForm()
  3. 11.x core/modules/field_ui/src/Form/FieldConfigDeleteForm.php \Drupal\field_ui\Form\FieldConfigDeleteForm::submitForm()

Overrides EntityDeleteFormTrait::submitForm

File

core/modules/field_ui/src/Form/FieldConfigDeleteForm.php, line 103

Class

FieldConfigDeleteForm
Provides a form for removing a field from a bundle.

Namespace

Drupal\field_ui\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
    $field_storage = $this->entity
        ->getFieldStorageDefinition();
    $target_entity_type_id = $this->entity
        ->getTargetEntityTypeId();
    $target_bundle = $this->entity
        ->getTargetBundle();
    $target_entity_definition = $this->entityTypeManager
        ->getDefinition($target_entity_type_id);
    $target_entity_bundle_entity_type_id = $target_entity_definition->getBundleEntityType();
    if (empty($target_entity_bundle_entity_type_id)) {
        $source_label = $this->t('entity type');
    }
    else {
        $target_entity_bundle_entity_type_definition = $this->entityTypeManager
            ->getDefinition($target_entity_bundle_entity_type_id);
        $source_label = strtolower($target_entity_bundle_entity_type_definition->getLabel());
    }
    $bundles = $this->entityTypeBundleInfo
        ->getBundleInfo($target_entity_type_id);
    $bundle_label = $bundles[$target_bundle]['label'];
    if ($field_storage && !$field_storage->isLocked()) {
        $this->entity
            ->delete();
        $this->messenger()
            ->addStatus($this->t('The field %field has been deleted from the %type %source_label.', [
            '%field' => $this->entity
                ->label(),
            '%type' => $bundle_label,
            '%source_label' => $source_label,
        ]));
    }
    else {
        $this->messenger()
            ->addError($this->t('There was a problem removing the %field from the %type %source_label.', [
            '%field' => $this->entity
                ->label(),
            '%type' => $bundle_label,
            '%source_label' => $source_label,
        ]));
    }
    $form_state->setRedirectUrl($this->getCancelUrl());
    // Fields are purged on cron. However field module prevents disabling
    // modules when field types they provided are used in a field until it is
    // fully purged. In the case that a field has minimal or no content, a
    // single call to field_purge_batch() will remove it from the system. Call
    // this with a low batch limit to avoid administrators having to wait for
    // cron runs when removing fields that meet this criteria.
    field_purge_batch(10);
}

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