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.
16 class FieldConfigDeleteForm extends EntityDeleteForm {
21 * @var \Drupal\Core\Entity\EntityManagerInterface
23 protected $entityManager;
26 * Constructs a new FieldConfigDeleteForm object.
28 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
31 public function __construct(EntityManagerInterface $entity_manager) {
32 $this->entityManager = $entity_manager;
38 public static function create(ContainerInterface $container) {
40 $container->get('entity.manager')
47 public function buildForm(array $form, FormStateInterface $form_state) {
48 $form = parent::buildForm($form, $form_state);
50 // If we are adding the field storage as a dependency to delete, then that
51 // will list the field as a dependency. That is confusing, so remove it.
52 // Also remove the entity type and the whole entity deletions details
53 // element if nothing else is in there.
54 if (isset($form['entity_deletes']['field_config']['#items']) && isset($form['entity_deletes']['field_config']['#items'][$this->entity->id()])) {
55 unset($form['entity_deletes']['field_config']['#items'][$this->entity->id()]);
56 if (empty($form['entity_deletes']['field_config']['#items'])) {
57 unset($form['entity_deletes']['field_config']);
58 if (!Element::children($form['entity_deletes'])) {
59 $form['entity_deletes']['#access'] = FALSE;
69 protected function getConfigNamesToDelete(ConfigEntityInterface $entity) {
70 /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
71 $field_storage = $entity->getFieldStorageDefinition();
72 $config_names = [$entity->getConfigDependencyName()];
74 // If there is only one bundle left for this field storage, it will be
75 // deleted too, notify the user about dependencies.
76 if (count($field_storage->getBundles()) <= 1) {
77 $config_names[] = $field_storage->getConfigDependencyName();
85 public function getCancelUrl() {
86 return FieldUI::getOverviewRouteInfo($this->entity->getTargetEntityTypeId(), $this->entity->getTargetBundle());
92 public function submitForm(array &$form, FormStateInterface $form_state) {
93 $field_storage = $this->entity->getFieldStorageDefinition();
94 $bundles = $this->entityManager->getBundleInfo($this->entity->getTargetEntityTypeId());
95 $bundle_label = $bundles[$this->entity->getTargetBundle()]['label'];
97 if ($field_storage && !$field_storage->isLocked()) {
98 $this->entity->delete();
99 drupal_set_message($this->t('The field %field has been deleted from the %type content type.', ['%field' => $this->entity->label(), '%type' => $bundle_label]));
102 drupal_set_message($this->t('There was a problem removing the %field from the %type content type.', ['%field' => $this->entity->label(), '%type' => $bundle_label]), 'error');
105 $form_state->setRedirectUrl($this->getCancelUrl());
107 // Fields are purged on cron. However field module prevents disabling modules
108 // when field types they provided are used in a field until it is fully
109 // purged. In the case that a field has minimal or no content, a single call
110 // to field_purge_batch() will remove it from the system. Call this with a
111 // low batch limit to avoid administrators having to wait for cron runs when
112 // removing fields that meet this criteria.
113 field_purge_batch(10);