You are here

public function ConfigManager::getConfigEntitiesToChangeOnDependencyRemoval in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Config/ConfigManager.php \Drupal\Core\Config\ConfigManager::getConfigEntitiesToChangeOnDependencyRemoval()

Lists which config entities to update and delete on removal of a dependency.

Parameters

string $type: The type of dependency being checked. Either 'module', 'theme', 'config' or 'content'.

array $names: The specific names to check. If $type equals 'module' or 'theme' then it should be a list of module names or theme names. In the case of 'config' or 'content' it should be a list of configuration dependency names.

bool $dry_run: If set to FALSE the entities returned in the list of updates will be modified. In order to make the changes the caller needs to save them. If set to TRUE the entities returned will not be modified.

Return value

array An array with the keys: 'update', 'delete' and 'unchanged'. The value of each is a list of configuration entities that need to have that action applied when the supplied dependencies are removed. Updates need to be processed before deletes. The order of the deletes is significant and must be processed in the returned order.

Overrides ConfigManagerInterface::getConfigEntitiesToChangeOnDependencyRemoval

1 call to ConfigManager::getConfigEntitiesToChangeOnDependencyRemoval()
ConfigManager::uninstall in core/lib/Drupal/Core/Config/ConfigManager.php
Uninstalls the configuration of a given extension.

File

core/lib/Drupal/Core/Config/ConfigManager.php, line 299
Contains \Drupal\Core\Config\ConfigManager.

Class

ConfigManager
The ConfigManager provides helper functions for the configuration system.

Namespace

Drupal\Core\Config

Code

public function getConfigEntitiesToChangeOnDependencyRemoval($type, array $names, $dry_run = TRUE) {

  // Determine the current list of dependent configuration entities and set up
  // initial values.
  $dependency_manager = $this
    ->getConfigDependencyManager();
  $dependents = $this
    ->findConfigEntityDependentsAsEntities($type, $names, $dependency_manager);
  $original_dependencies = $dependents;
  $update_uuids = [];
  $return = [
    'update' => [],
    'delete' => [],
    'unchanged' => [],
  ];

  // Try to fix any dependencies and find out what will happen to the
  // dependency graph.
  foreach ($dependents as $dependent) {

    /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $dependent */
    if ($dry_run) {

      // Clone the entity so any changes do not change any static caches.
      $dependent = clone $dependent;
    }
    if ($this
      ->callOnDependencyRemoval($dependent, $original_dependencies, $type, $names)) {

      // Recalculate dependencies and update the dependency graph data.
      $dependent
        ->calculateDependencies();
      $dependency_manager
        ->updateData($dependent
        ->getConfigDependencyName(), $dependent
        ->getDependencies());

      // Based on the updated data rebuild the list of dependents.
      $dependents = $this
        ->findConfigEntityDependentsAsEntities($type, $names, $dependency_manager);

      // Ensure that the dependency has actually been fixed. It is possible
      // that the dependent has multiple dependencies that cause it to be in
      // the dependency chain.
      $fixed = TRUE;
      foreach ($dependents as $entity) {
        if ($entity
          ->uuid() == $dependent
          ->uuid()) {
          $fixed = FALSE;
          break;
        }
      }
      if ($fixed) {
        $return['update'][] = $dependent;
        $update_uuids[] = $dependent
          ->uuid();
      }
    }
  }

  // Now that we've fixed all the possible dependencies the remaining need to
  // be deleted. Reverse the deletes so that entities are removed in the
  // correct order of dependence. For example, this ensures that fields are
  // removed before field storages.
  $return['delete'] = array_reverse($dependents);
  $delete_uuids = array_map(function ($dependent) {
    return $dependent
      ->uuid();
  }, $return['delete']);

  // Use the lists of UUIDs to filter the original list to work out which
  // configuration entities are unchanged.
  $return['unchanged'] = array_filter($original_dependencies, function ($dependent) use ($delete_uuids, $update_uuids) {
    return !(in_array($dependent
      ->uuid(), $delete_uuids) || in_array($dependent
      ->uuid(), $update_uuids));
  });
  return $return;
}