public function BundleConfigImportValidate::onConfigImporterValidate

public BundleConfigImportValidate::onConfigImporterValidate(ConfigImporterEvent $event)

Ensures bundles that will be deleted are not in use.

Parameters

\Drupal\Core\Config\ConfigImporterEvent $event: The config import event.

Overrides ConfigImportValidateEventSubscriberBase::onConfigImporterValidate

File

core/lib/Drupal/Core/Entity/Event/BundleConfigImportValidate.php, line 49

Class

BundleConfigImportValidate
Entity config importer validation event subscriber.

Namespace

Drupal\Core\Entity\Event

Code

public function onConfigImporterValidate(ConfigImporterEvent $event) {
  foreach ($event->getChangelist('delete') as $config_name) {
    // Get the config entity type ID. This also ensure we are dealing with a
    // configuration entity.
    if ($entity_type_id = $this->configManager->getEntityTypeIdByName($config_name)) {
      $entity_type = $this->entityManager->getDefinition($entity_type_id);
      // Does this entity type define a bundle of another entity type.
      if ($bundle_of = $entity_type->getBundleOf()) {
        // Work out if there are entities with this bundle.
        $bundle_of_entity_type = $this->entityManager->getDefinition($bundle_of);
        $bundle_id = ConfigEntityStorage::getIDFromConfigName($config_name, $entity_type->getConfigPrefix());
        $entity_query = $this->entityManager->getStorage($bundle_of)->getQuery();
        $entity_ids = $entity_query->condition($bundle_of_entity_type->getKey('bundle'), $bundle_id)
          ->accessCheck(FALSE)
          ->range(0, 1)
          ->execute();
        if (!empty($entity_ids)) {
          $entity = $this->entityManager->getStorage($entity_type_id)->load($bundle_id);
          $event->getConfigImporter()->logError($this->t('Entities exist of type %entity_type and %bundle_label %bundle. These entities need to be deleted before importing.', array('%entity_type' => $bundle_of_entity_type->getLabel(), '%bundle_label' => $bundle_of_entity_type->getBundleLabel(), '%bundle' => $entity->label())));
        }
      }
    }
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!Event!BundleConfigImportValidate.php/function/BundleConfigImportValidate::onConfigImporterValidate/8.1.x