FieldConfigDeleteForm.php 4.04 KB
Newer Older
1 2 3 4
<?php

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;
12 13

/**
14
 * Provides a form for removing a field from a bundle.
15 16
 *
 * @internal
17
 */
18
class FieldConfigDeleteForm extends EntityDeleteForm {
19

20 21 22
  /**
   * The entity manager.
   *
23
   * @var \Drupal\Core\Entity\EntityManagerInterface
24 25 26 27
   */
  protected $entityManager;

  /**
28
   * Constructs a new FieldConfigDeleteForm object.
29
   *
30
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
31 32
   *   The entity manager.
   */
33
  public function __construct(EntityManagerInterface $entity_manager) {
34 35 36 37 38 39
    $this->entityManager = $entity_manager;
  }

  /**
   * {@inheritdoc}
   */
40
  public static function create(ContainerInterface $container) {
41
    return new static(
42
      $container->get('entity.manager')
43 44 45
    );
  }

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);

    // If we are adding the field storage as a dependency to delete, then that
    // will list the field as a dependency. That is confusing, so remove it.
    // Also remove the entity type and the whole entity deletions details
    // element if nothing else is in there.
    if (isset($form['entity_deletes']['field_config']['#items']) && isset($form['entity_deletes']['field_config']['#items'][$this->entity->id()])) {
      unset($form['entity_deletes']['field_config']['#items'][$this->entity->id()]);
      if (empty($form['entity_deletes']['field_config']['#items'])) {
        unset($form['entity_deletes']['field_config']);
        if (!Element::children($form['entity_deletes'])) {
          $form['entity_deletes']['#access'] = FALSE;
        }
      }
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  protected function getConfigNamesToDelete(ConfigEntityInterface $entity) {
    /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
    $field_storage = $entity->getFieldStorageDefinition();
    $config_names = [$entity->getConfigDependencyName()];

    // If there is only one bundle left for this field storage, it will be
    // deleted too, notify the user about dependencies.
    if (count($field_storage->getBundles()) <= 1) {
      $config_names[] = $field_storage->getConfigDependencyName();
    }
    return $config_names;
  }

84 85 86
  /**
   * {@inheritdoc}
   */
87
  public function getCancelUrl() {
88
    return FieldUI::getOverviewRouteInfo($this->entity->getTargetEntityTypeId(), $this->entity->getTargetBundle());
89 90 91 92 93
  }

  /**
   * {@inheritdoc}
   */
94
  public function submitForm(array &$form, FormStateInterface $form_state) {
95
    $field_storage = $this->entity->getFieldStorageDefinition();
96 97
    $bundles = $this->entityManager->getBundleInfo($this->entity->getTargetEntityTypeId());
    $bundle_label = $bundles[$this->entity->getTargetBundle()]['label'];
98

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]));
102 103
    }
    else {
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]));
105 106
    }

107
    $form_state->setRedirectUrl($this->getCancelUrl());
108 109 110 111 112 113

    // 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
114
    // removing fields that meet this criteria.
115 116 117 118
    field_purge_batch(10);
  }

}