BundleConfigImportValidate.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Drupal\Core\Entity\Event;
  3. use Drupal\Core\Config\ConfigImporterEvent;
  4. use Drupal\Core\Config\ConfigImportValidateEventSubscriberBase;
  5. use Drupal\Core\Config\ConfigManagerInterface;
  6. use Drupal\Core\Config\Entity\ConfigEntityStorage;
  7. use Drupal\Core\Entity\EntityManagerInterface;
  8. /**
  9. * Entity config importer validation event subscriber.
  10. */
  11. class BundleConfigImportValidate extends ConfigImportValidateEventSubscriberBase {
  12. /**
  13. * The config manager.
  14. *
  15. * @var \Drupal\Core\Config\ConfigManagerInterface
  16. */
  17. protected $configManager;
  18. /**
  19. * The entity manager.
  20. *
  21. * @var \Drupal\Core\Entity\EntityManagerInterface
  22. */
  23. protected $entityManager;
  24. /**
  25. * Constructs the event subscriber.
  26. *
  27. * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
  28. * The config manager
  29. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
  30. * The entity manager.
  31. */
  32. public function __construct(ConfigManagerInterface $config_manager, EntityManagerInterface $entity_manager) {
  33. $this->configManager = $config_manager;
  34. $this->entityManager = $entity_manager;
  35. }
  36. /**
  37. * Ensures bundles that will be deleted are not in use.
  38. *
  39. * @param \Drupal\Core\Config\ConfigImporterEvent $event
  40. * The config import event.
  41. */
  42. public function onConfigImporterValidate(ConfigImporterEvent $event) {
  43. foreach ($event->getChangelist('delete') as $config_name) {
  44. // Get the config entity type ID. This also ensure we are dealing with a
  45. // configuration entity.
  46. if ($entity_type_id = $this->configManager->getEntityTypeIdByName($config_name)) {
  47. $entity_type = $this->entityManager->getDefinition($entity_type_id);
  48. // Does this entity type define a bundle of another entity type.
  49. if ($bundle_of = $entity_type->getBundleOf()) {
  50. // Work out if there are entities with this bundle.
  51. $bundle_of_entity_type = $this->entityManager->getDefinition($bundle_of);
  52. $bundle_id = ConfigEntityStorage::getIDFromConfigName($config_name, $entity_type->getConfigPrefix());
  53. $entity_query = $this->entityManager->getStorage($bundle_of)->getQuery();
  54. $entity_ids = $entity_query->condition($bundle_of_entity_type->getKey('bundle'), $bundle_id)
  55. ->accessCheck(FALSE)
  56. ->range(0, 1)
  57. ->execute();
  58. if (!empty($entity_ids)) {
  59. $entity = $this->entityManager->getStorage($entity_type_id)->load($bundle_id);
  60. $event->getConfigImporter()->logError($this->t('Entities exist of type %entity_type and %bundle_label %bundle. These entities need to be deleted before importing.', ['%entity_type' => $bundle_of_entity_type->getLabel(), '%bundle_label' => $bundle_of_entity_type->getBundleLabel(), '%bundle' => $entity->label()]));
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }