You are here

public function ConfigTest::onDependencyRemoval in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/config/tests/config_test/src/Entity/ConfigTest.php \Drupal\config_test\Entity\ConfigTest::onDependencyRemoval()
  2. 10 core/modules/config/tests/config_test/src/Entity/ConfigTest.php \Drupal\config_test\Entity\ConfigTest::onDependencyRemoval()

Informs the entity that entities it depends on will be deleted.

This method allows configuration entities to remove dependencies instead of being deleted themselves. Configuration entities can use this method to avoid being unnecessarily deleted during an extension uninstallation. For example, entity displays remove references to widgets and formatters if the plugin that supplies them depends on a module that is being uninstalled.

If this method returns TRUE then the entity needs to be re-saved by the caller for the changes to take effect. Implementations should not save the entity.

Parameters

array $dependencies: An array of dependencies that will be deleted keyed by dependency type. Dependency types are, for example, entity, module and theme.

Return value

bool TRUE if the entity has been changed as a result, FALSE if not.

Overrides ConfigEntityBase::onDependencyRemoval

See also

\Drupal\Core\Config\Entity\ConfigDependencyManager

\Drupal\Core\Config\ConfigEntityBase::preDelete()

\Drupal\Core\Config\ConfigManager::uninstall()

\Drupal\Core\Entity\EntityDisplayBase::onDependencyRemoval()

File

core/modules/config/tests/config_test/src/Entity/ConfigTest.php, line 139

Class

ConfigTest
Defines the ConfigTest configuration entity.

Namespace

Drupal\config_test\Entity

Code

public function onDependencyRemoval(array $dependencies) {

  // Record which entities have this method called on and what dependencies
  // are passed.
  $called = \Drupal::state()
    ->get('config_test.on_dependency_removal_called', []);
  $called[$this
    ->id()] = $dependencies;
  $called[$this
    ->id()]['config'] = array_keys($called[$this
    ->id()]['config']);
  $called[$this
    ->id()]['content'] = array_keys($called[$this
    ->id()]['content']);
  \Drupal::state()
    ->set('config_test.on_dependency_removal_called', $called);
  $changed = parent::onDependencyRemoval($dependencies);
  if (!isset($this->dependencies['enforced']['config'])) {
    return $changed;
  }
  $fix_deps = \Drupal::state()
    ->get('config_test.fix_dependencies', []);
  foreach ($dependencies['config'] as $entity) {
    if (in_array($entity
      ->getConfigDependencyName(), $fix_deps)) {
      $key = array_search($entity
        ->getConfigDependencyName(), $this->dependencies['enforced']['config']);
      if ($key !== FALSE) {
        $changed = TRUE;
        unset($this->dependencies['enforced']['config'][$key]);
      }
    }
  }

  // If any of the dependencies removed still exists, return FALSE.
  if (array_intersect_key(array_flip($this->dependencies['enforced']['config']), $dependencies['config'])) {
    return FALSE;
  }
  return $changed;
}