3 namespace Drupal\field_ui\Form;
5 use Drupal\Core\Config\Entity\ConfigEntityInterface;
6 use Drupal\Core\Entity\EntityDeleteForm;
7 use Drupal\Core\Entity\EntityManagerInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Render\Element;
10 use Drupal\field_ui\FieldUI;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Provides a form for removing a field from a bundle.
18 class FieldConfigDeleteForm extends EntityDeleteForm {
23 * @var \Drupal\Core\Entity\EntityManagerInterface
25 protected $entityManager;
28 * Constructs a new FieldConfigDeleteForm object.
30 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
33 public function __construct(EntityManagerInterface $entity_manager) {
34 $this->entityManager = $entity_manager;
40 public static function create(ContainerInterface $container) {
42 $container->get('entity.manager')
49 public function buildForm(array $form, FormStateInterface $form_state) {
50 $form = parent::buildForm($form, $form_state);
52 // If we are adding the field storage as a dependency to delete, then that
53 // will list the field as a dependency. That is confusing, so remove it.
54 // Also remove the entity type and the whole entity deletions details
55 // element if nothing else is in there.
56 if (isset($form['entity_deletes']['field_config']['#items']) && isset($form['entity_deletes']['field_config']['#items'][$this->entity->id()])) {
57 unset($form['entity_deletes']['field_config']['#items'][$this->entity->id()]);
58 if (empty($form['entity_deletes']['field_config']['#items'])) {
59 unset($form['entity_deletes']['field_config']);
60 if (!Element::children($form['entity_deletes'])) {
61 $form['entity_deletes']['#access'] = FALSE;
71 protected function getConfigNamesToDelete(ConfigEntityInterface $entity) {
72 /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
73 $field_storage = $entity->getFieldStorageDefinition();
74 $config_names = [$entity->getConfigDependencyName()];
76 // If there is only one bundle left for this field storage, it will be
77 // deleted too, notify the user about dependencies.
78 if (count($field_storage->getBundles()) <= 1) {
79 $config_names[] = $field_storage->getConfigDependencyName();
87 public function getCancelUrl() {
88 return FieldUI::getOverviewRouteInfo($this->entity->getTargetEntityTypeId(), $this->entity->getTargetBundle());
94 public function submitForm(array &$form, FormStateInterface $form_state) {
95 $field_storage = $this->entity->getFieldStorageDefinition();
96 $bundles = $this->entityManager->getBundleInfo($this->entity->getTargetEntityTypeId());
97 $bundle_label = $bundles[$this->entity->getTargetBundle()]['label'];
99 if ($field_storage && !$field_storage->isLocked()) {
100 $this->entity->delete();
101 $this->messenger()->addStatus($this->t('The field %field has been deleted from the %type content type.', ['%field' => $this->entity->label(), '%type' => $bundle_label]));
104 $this->messenger()->addError($this->t('There was a problem removing the %field from the %type content type.', ['%field' => $this->entity->label(), '%type' => $bundle_label]));
107 $form_state->setRedirectUrl($this->getCancelUrl());
109 // Fields are purged on cron. However field module prevents disabling modules
110 // when field types they provided are used in a field until it is fully
111 // purged. In the case that a field has minimal or no content, a single call
112 // to field_purge_batch() will remove it from the system. Call this with a
113 // low batch limit to avoid administrators having to wait for cron runs when
114 // removing fields that meet this criteria.
115 field_purge_batch(10);