Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Entity / Event / BundleConfigImportValidate.php
1 <?php
2
3 namespace Drupal\Core\Entity\Event;
4
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;
10
11 /**
12  * Entity config importer validation event subscriber.
13  */
14 class BundleConfigImportValidate extends ConfigImportValidateEventSubscriberBase {
15
16   /**
17    * The config manager.
18    *
19    * @var \Drupal\Core\Config\ConfigManagerInterface
20    */
21   protected $configManager;
22
23   /**
24    * The entity manager.
25    *
26    * @var \Drupal\Core\Entity\EntityManagerInterface
27    */
28   protected $entityManager;
29
30   /**
31    * Constructs the event subscriber.
32    *
33    * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
34    *   The config manager
35    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
36    *   The entity manager.
37    */
38   public function __construct(ConfigManagerInterface $config_manager, EntityManagerInterface $entity_manager) {
39     $this->configManager = $config_manager;
40     $this->entityManager = $entity_manager;
41   }
42
43   /**
44    * Ensures bundles that will be deleted are not in use.
45    *
46    * @param \Drupal\Core\Config\ConfigImporterEvent $event
47    *   The config import event.
48    */
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)
62             ->accessCheck(FALSE)
63             ->range(0, 1)
64             ->execute();
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()]));
68           }
69         }
70       }
71     }
72   }
73
74 }