EntityDeleteForm.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. use Drupal\Core\Config\Entity\ConfigEntityInterface;
  4. use Drupal\Core\Form\FormStateInterface;
  5. /**
  6. * Provides a generic base class for an entity deletion form.
  7. *
  8. * @ingroup entity_api
  9. *
  10. * @internal
  11. */
  12. class EntityDeleteForm extends EntityConfirmFormBase {
  13. use EntityDeleteFormTrait;
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public function buildForm(array $form, FormStateInterface $form_state) {
  18. $form = parent::buildForm($form, $form_state);
  19. $entity = $this->getEntity();
  20. // Only do dependency processing for configuration entities. Whilst it is
  21. // possible for a configuration entity to be dependent on a content entity,
  22. // these dependencies are soft and content delete permissions are often
  23. // given to more users. This method should not make assumptions that $entity
  24. // is a configuration entity in case we decide to remove the following
  25. // condition.
  26. if (!($entity instanceof ConfigEntityInterface)) {
  27. return $form;
  28. }
  29. $this->addDependencyListsToForm($form, $entity->getConfigDependencyKey(), $this->getConfigNamesToDelete($entity), $this->getConfigManager(), $this->entityManager);
  30. return $form;
  31. }
  32. /**
  33. * Gets the configuration manager.
  34. *
  35. * @return \Drupal\Core\Config\ConfigManager
  36. * The configuration manager.
  37. */
  38. protected function getConfigManager() {
  39. return \Drupal::service('config.manager');
  40. }
  41. /**
  42. * Returns config names to delete for the deletion confirmation form.
  43. *
  44. * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $entity
  45. * The entity being deleted.
  46. *
  47. * @return string[]
  48. * A list of configuration names that will be deleted by this form.
  49. */
  50. protected function getConfigNamesToDelete(ConfigEntityInterface $entity) {
  51. return [$entity->getConfigDependencyName()];
  52. }
  53. }