BundleConfigImportValidate.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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\DependencyInjection\DeprecatedServicePropertyTrait;
  8. use Drupal\Core\Entity\EntityManagerInterface;
  9. use Drupal\Core\Entity\EntityTypeManagerInterface;
  10. /**
  11. * Entity config importer validation event subscriber.
  12. */
  13. class BundleConfigImportValidate extends ConfigImportValidateEventSubscriberBase {
  14. use DeprecatedServicePropertyTrait;
  15. /**
  16. * {@inheritdoc}
  17. */
  18. protected $deprecatedProperties = ['entityManager' => 'entity.manager'];
  19. /**
  20. * The config manager.
  21. *
  22. * @var \Drupal\Core\Config\ConfigManagerInterface
  23. */
  24. protected $configManager;
  25. /**
  26. * The entity type manager service.
  27. *
  28. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  29. */
  30. protected $entityTypeManager;
  31. /**
  32. * Constructs the event subscriber.
  33. *
  34. * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
  35. * The config manager.
  36. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  37. * The entity type manager service.
  38. */
  39. public function __construct(ConfigManagerInterface $config_manager, EntityTypeManagerInterface $entity_type_manager) {
  40. $this->configManager = $config_manager;
  41. if ($entity_type_manager instanceof EntityManagerInterface) {
  42. @trigger_error('Passing the entity.manager service to BundleConfigImportValidate::__construct() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Pass the new dependencies instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
  43. $this->entityTypeManager = \Drupal::entityTypeManager();
  44. }
  45. else {
  46. $this->entityTypeManager = $entity_type_manager;
  47. }
  48. }
  49. /**
  50. * Ensures bundles that will be deleted are not in use.
  51. *
  52. * @param \Drupal\Core\Config\ConfigImporterEvent $event
  53. * The config import event.
  54. */
  55. public function onConfigImporterValidate(ConfigImporterEvent $event) {
  56. foreach ($event->getChangelist('delete') as $config_name) {
  57. // Get the config entity type ID. This also ensure we are dealing with a
  58. // configuration entity.
  59. if ($entity_type_id = $this->configManager->getEntityTypeIdByName($config_name)) {
  60. $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
  61. // Does this entity type define a bundle of another entity type.
  62. if ($bundle_of = $entity_type->getBundleOf()) {
  63. // Work out if there are entities with this bundle.
  64. $bundle_of_entity_type = $this->entityTypeManager->getDefinition($bundle_of);
  65. $bundle_id = ConfigEntityStorage::getIDFromConfigName($config_name, $entity_type->getConfigPrefix());
  66. $entity_query = $this->entityTypeManager->getStorage($bundle_of)->getQuery();
  67. $entity_ids = $entity_query->condition($bundle_of_entity_type->getKey('bundle'), $bundle_id)
  68. ->accessCheck(FALSE)
  69. ->range(0, 1)
  70. ->execute();
  71. if (!empty($entity_ids)) {
  72. $entity = $this->entityTypeManager->getStorage($entity_type_id)->load($bundle_id);
  73. $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()]));
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }