3 namespace Drupal\Core\Entity\Event;
5 use Drupal\Core\Config\ConfigImporterEvent;
6 use Drupal\Core\Config\ConfigImportValidateEventSubscriberBase;
7 use Drupal\Core\Config\ConfigManagerInterface;
8 use Drupal\Core\Config\Entity\ConfigEntityStorage;
9 use Drupal\Core\Entity\EntityManagerInterface;
12 * Entity config importer validation event subscriber.
14 class BundleConfigImportValidate extends ConfigImportValidateEventSubscriberBase {
19 * @var \Drupal\Core\Config\ConfigManagerInterface
21 protected $configManager;
26 * @var \Drupal\Core\Entity\EntityManagerInterface
28 protected $entityManager;
31 * Constructs the event subscriber.
33 * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
35 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
38 public function __construct(ConfigManagerInterface $config_manager, EntityManagerInterface $entity_manager) {
39 $this->configManager = $config_manager;
40 $this->entityManager = $entity_manager;
44 * Ensures bundles that will be deleted are not in use.
46 * @param \Drupal\Core\Config\ConfigImporterEvent $event
47 * The config import event.
49 public function onConfigImporterValidate(ConfigImporterEvent $event) {
50 foreach ($event->getChangelist('delete') as $config_name) {
51 // Get the config entity type ID. This also ensure we are dealing with a
52 // configuration entity.
53 if ($entity_type_id = $this->configManager->getEntityTypeIdByName($config_name)) {
54 $entity_type = $this->entityManager->getDefinition($entity_type_id);
55 // Does this entity type define a bundle of another entity type.
56 if ($bundle_of = $entity_type->getBundleOf()) {
57 // Work out if there are entities with this bundle.
58 $bundle_of_entity_type = $this->entityManager->getDefinition($bundle_of);
59 $bundle_id = ConfigEntityStorage::getIDFromConfigName($config_name, $entity_type->getConfigPrefix());
60 $entity_query = $this->entityManager->getStorage($bundle_of)->getQuery();
61 $entity_ids = $entity_query->condition($bundle_of_entity_type->getKey('bundle'), $bundle_id)
65 if (!empty($entity_ids)) {
66 $entity = $this->entityManager->getStorage($entity_type_id)->load($bundle_id);
67 $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()]));