getConfigImporter example

/** * Validates the configuration to be imported. * * @param \Drupal\Core\Config\ConfigImporterEvent $event * The Event to process. * * @throws \Drupal\Core\Config\ConfigNameException */
  public function onConfigImporterValidate(ConfigImporterEvent $event) {
    foreach (['delete', 'create', 'update'] as $op) {
      foreach ($event->getConfigImporter()->getUnprocessedConfiguration($op) as $name) {
        try {
          Config::validateName($name);
        }
        catch (ConfigNameException $e) {
          $message = $this->t('The config name @config_name is invalid.', ['@config_name' => $name]);
          $event->getConfigImporter()->logError($message);
        }
      }
    }
    $config_importer = $event->getConfigImporter();
    if ($config_importer->getStorageComparer()->getSourceStorage()->exists('core.extension')) {
      
/** * Validates the configuration to be imported. * * @param \Drupal\Core\Config\ConfigImporterEvent $event * The Event to process. * * @throws \Drupal\Core\Config\ConfigNameException */
  public function onConfigImporterValidate(ConfigImporterEvent $event) {
    if ($this->state->get('config_import_test.config_import_validate_fail', FALSE)) {
      // Log more than one error to test multiple validation errors.       $event->getConfigImporter()->logError('Config import validate error 1.');
      $event->getConfigImporter()->logError('Config import validate error 2.');
    }
  }

  /** * Handles the missing content event. * * @param \Drupal\Core\Config\Importer\MissingContentEvent $event * The missing content event. */
  public function onConfigImporterMissingContentOne(MissingContentEvent $event) {
    
        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()]));
          }
        }
      }
    }
  }

}
/** * Checks that the configuration synchronization is valid. * * This event listener prevents deleting all configuration. If there is * nothing to import then event propagation is stopped because there is no * config import to validate. * * @param \Drupal\Core\Config\ConfigImporterEvent $event * The config import event. */
  public function onConfigImporterValidateNotEmpty(ConfigImporterEvent $event) {
    $importList = $event->getConfigImporter()->getStorageComparer()->getSourceStorage()->listAll();
    if (empty($importList)) {
      $event->getConfigImporter()->logError($this->t('This import is empty and if applied would delete all of your configuration, so has been rejected.'));
      $event->stopPropagation();
    }
  }

  /** * Checks that the configuration synchronization is valid. * * This event listener checks that the system.site:uuid's in the source and * target match. * * @param \Drupal\Core\Config\ConfigImporterEvent $event * The config import event. */

  public function __construct(ConfigManagerInterface $config_manager, EntityTypeManagerInterface $entity_type_manager) {
    $this->configManager = $config_manager;
    $this->entityTypeManager = $entity_type_manager;
  }

  /** * {@inheritdoc} */
  public function onConfigImporterValidate(ConfigImporterEvent $event) {
    foreach (['update', 'delete'] as $op) {
      $unprocessed_configurations = $event->getConfigImporter()->getUnprocessedConfiguration($op);
      foreach ($unprocessed_configurations as $unprocessed_configuration) {
        if ($workflow = $this->getWorkflow($unprocessed_configuration)) {
          if ($op === 'update') {
            $original_workflow_config = $event->getConfigImporter()
              ->getStorageComparer()
              ->getSourceStorage()
              ->read($unprocessed_configuration);
            $workflow_config = $event->getConfigImporter()
              ->getStorageComparer()
              ->getTargetStorage()
              ->read($unprocessed_configuration);
            

  public function onConfigImporterValidate(ConfigImporterEvent $event) {
    // Make sure config syncs performed via the Config UI don't break, but     // don't worry about syncs initiated via the command line.     if (PHP_SAPI === 'cli') {
      return;
    }
    $importer = $event->getConfigImporter();
    $core_extension = $importer->getStorageComparer()->getSourceStorage()->read('core.extension');
    if (!isset($core_extension['module']['config'])) {
      $importer->logError($this->t('Can not uninstall the Configuration module as part of a configuration synchronization through the user interface.'));
    }
  }

  /** * {@inheritdoc} */
  public static function getSubscribedEvents(): array {
    $events[ConfigEvents::IMPORT_VALIDATE][] = ['onConfigImporterValidate', 20];
    
Home | Imprint | This part of the site doesn't use cookies.