class BundleConfigImportValidate

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Entity/Event/BundleConfigImportValidate.php \Drupal\Core\Entity\Event\BundleConfigImportValidate
  2. 10 core/lib/Drupal/Core/Entity/Event/BundleConfigImportValidate.php \Drupal\Core\Entity\Event\BundleConfigImportValidate
  3. 11.x core/lib/Drupal/Core/Entity/Event/BundleConfigImportValidate.php \Drupal\Core\Entity\Event\BundleConfigImportValidate

Entity config importer validation event subscriber.

Hierarchy

Expanded class hierarchy of BundleConfigImportValidate

1 string reference to 'BundleConfigImportValidate'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses BundleConfigImportValidate
entity.bundle_config_import_validator in core/core.services.yml
Drupal\Core\Entity\Event\BundleConfigImportValidate

File

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

Namespace

Drupal\Core\Entity\Event
View source
class BundleConfigImportValidate extends ConfigImportValidateEventSubscriberBase {
    
    /**
     * The config manager.
     *
     * @var \Drupal\Core\Config\ConfigManagerInterface
     */
    protected $configManager;
    
    /**
     * The entity type manager service.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * Constructs the event subscriber.
     *
     * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
     *   The config manager.
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager service.
     */
    public function __construct(ConfigManagerInterface $config_manager, EntityTypeManagerInterface $entity_type_manager) {
        $this->configManager = $config_manager;
        $this->entityTypeManager = $entity_type_manager;
    }
    
    /**
     * Ensures bundles that will be deleted are not in use.
     *
     * @param \Drupal\Core\Config\ConfigImporterEvent $event
     *   The config import event.
     */
    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->entityTypeManager
                    ->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->entityTypeManager
                        ->getDefinition($bundle_of);
                    $bundle_id = ConfigEntityStorage::getIDFromConfigName($config_name, $entity_type->getConfigPrefix());
                    $entity_query = $this->entityTypeManager
                        ->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->entityTypeManager
                            ->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.', [
                            '%entity_type' => $bundle_of_entity_type->getLabel(),
                            '%bundle_label' => $bundle_of_entity_type->getBundleLabel(),
                            '%bundle' => $entity->label(),
                        ]));
                    }
                }
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
BundleConfigImportValidate::$configManager protected property The config manager.
BundleConfigImportValidate::$entityTypeManager protected property The entity type manager service.
BundleConfigImportValidate::onConfigImporterValidate public function Ensures bundles that will be deleted are not in use. Overrides ConfigImportValidateEventSubscriberBase::onConfigImporterValidate
BundleConfigImportValidate::__construct public function Constructs the event subscriber.
ConfigImportValidateEventSubscriberBase::getSubscribedEvents public static function 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.