67782311bc44ec5cd2cef4fc5a4fae048a6c6c1e
[yaffs-website] / web / core / modules / field_ui / src / Form / FieldConfigDeleteForm.php
1 <?php
2
3 namespace Drupal\field_ui\Form;
4
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 class FieldConfigDeleteForm extends EntityDeleteForm {
17
18   /**
19    * The entity manager.
20    *
21    * @var \Drupal\Core\Entity\EntityManagerInterface
22    */
23   protected $entityManager;
24
25   /**
26    * Constructs a new FieldConfigDeleteForm object.
27    *
28    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
29    *   The entity manager.
30    */
31   public function __construct(EntityManagerInterface $entity_manager) {
32     $this->entityManager = $entity_manager;
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public static function create(ContainerInterface $container) {
39     return new static(
40       $container->get('entity.manager')
41     );
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function buildForm(array $form, FormStateInterface $form_state) {
48     $form = parent::buildForm($form, $form_state);
49
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;
60         }
61       }
62     }
63     return $form;
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   protected function getConfigNamesToDelete(ConfigEntityInterface $entity) {
70     /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
71     $field_storage = $entity->getFieldStorageDefinition();
72     $config_names = [$entity->getConfigDependencyName()];
73
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();
78     }
79     return $config_names;
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function getCancelUrl() {
86     return FieldUI::getOverviewRouteInfo($this->entity->getTargetEntityTypeId(), $this->entity->getTargetBundle());
87   }
88
89   /**
90    * {@inheritdoc}
91    */
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'];
96
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]));
100     }
101     else {
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');
103     }
104
105     $form_state->setRedirectUrl($this->getCancelUrl());
106
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);
114   }
115
116 }