You are here

protected function ConfigImporter::checkOp in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Config/ConfigImporter.php \Drupal\Core\Config\ConfigImporter::checkOp()

Checks that the operation is still valid.

During a configuration import secondary writes and deletes are possible. This method checks that the operation is still valid before processing a configuration change.

Parameters

string $collection: The configuration collection.

string $op: The change operation.

string $name: The name of the configuration to process.

Return value

bool TRUE is to continue processing, FALSE otherwise.

Throws

\Drupal\Core\Config\ConfigImporterException

1 call to ConfigImporter::checkOp()
ConfigImporter::processConfigurations in core/lib/Drupal/Core/Config/ConfigImporter.php
Processes configuration as a batch operation.

File

core/lib/Drupal/Core/Config/ConfigImporter.php, line 852

Class

ConfigImporter
Defines a configuration importer.

Namespace

Drupal\Core\Config

Code

protected function checkOp($collection, $op, $name) {
  if ($op == 'rename') {
    $names = $this->storageComparer
      ->extractRenameNames($name);
    $target_exists = $this->storageComparer
      ->getTargetStorage($collection)
      ->exists($names['new_name']);
    if ($target_exists) {

      // If the target exists, the rename has already occurred as the
      // result of a secondary configuration write. Change the operation
      // into an update. This is the desired behavior since renames often
      // have to occur together. For example, renaming a node type must
      // also result in renaming its fields and entity displays.
      $this->storageComparer
        ->moveRenameToUpdate($name);
      return FALSE;
    }
    return TRUE;
  }
  $target_exists = $this->storageComparer
    ->getTargetStorage($collection)
    ->exists($name);
  switch ($op) {
    case 'delete':
      if (!$target_exists) {

        // The configuration has already been deleted. For example, a field
        // is automatically deleted if all the instances are.
        $this
          ->setProcessedConfiguration($collection, $op, $name);
        return FALSE;
      }
      break;
    case 'create':
      if ($target_exists) {

        // If the target already exists, use the entity storage to delete it
        // again, if is a simple config, delete it directly.
        if ($entity_type_id = $this->configManager
          ->getEntityTypeIdByName($name)) {
          $entity_storage = $this->configManager
            ->getEntityTypeManager()
            ->getStorage($entity_type_id);
          $entity_type = $this->configManager
            ->getEntityTypeManager()
            ->getDefinition($entity_type_id);
          $entity = $entity_storage
            ->load($entity_storage
            ->getIDFromConfigName($name, $entity_type
            ->getConfigPrefix()));
          $entity
            ->delete();
          $this
            ->logError($this
            ->t('Deleted and replaced configuration entity "@name"', [
            '@name' => $name,
          ]));
        }
        else {
          $this->storageComparer
            ->getTargetStorage($collection)
            ->delete($name);
          $this
            ->logError($this
            ->t('Deleted and replaced configuration "@name"', [
            '@name' => $name,
          ]));
        }
        return TRUE;
      }
      break;
    case 'update':
      if (!$target_exists) {
        $this
          ->logError($this
          ->t('Update target "@name" is missing.', [
          '@name' => $name,
        ]));

        // Mark as processed so that the synchronization continues. Once the
        // the current synchronization is complete it will show up as a
        // create.
        $this
          ->setProcessedConfiguration($collection, $op, $name);
        return FALSE;
      }
      break;
  }
  return TRUE;
}