X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Ffield_ui%2Fsrc%2FForm%2FFieldConfigDeleteForm.php;fp=web%2Fcore%2Fmodules%2Ffield_ui%2Fsrc%2FForm%2FFieldConfigDeleteForm.php;h=67782311bc44ec5cd2cef4fc5a4fae048a6c6c1e;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/field_ui/src/Form/FieldConfigDeleteForm.php b/web/core/modules/field_ui/src/Form/FieldConfigDeleteForm.php new file mode 100644 index 000000000..67782311b --- /dev/null +++ b/web/core/modules/field_ui/src/Form/FieldConfigDeleteForm.php @@ -0,0 +1,116 @@ +entityManager = $entity_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.manager') + ); + } + + /** + * {@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; + } + + /** + * {@inheritdoc} + */ + public function getCancelUrl() { + return FieldUI::getOverviewRouteInfo($this->entity->getTargetEntityTypeId(), $this->entity->getTargetBundle()); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $field_storage = $this->entity->getFieldStorageDefinition(); + $bundles = $this->entityManager->getBundleInfo($this->entity->getTargetEntityTypeId()); + $bundle_label = $bundles[$this->entity->getTargetBundle()]['label']; + + if ($field_storage && !$field_storage->isLocked()) { + $this->entity->delete(); + drupal_set_message($this->t('The field %field has been deleted from the %type content type.', ['%field' => $this->entity->label(), '%type' => $bundle_label])); + } + else { + 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'); + } + + $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); + } + +}