ConfigDependencyManager

Provides a class to discover configuration entity dependencies.

Configuration entities can depend on modules, themes and other configuration entities. The dependency system is used during configuration installation, uninstallation, and synchronization to ensure that configuration entities are handled in the correct order. For example, node types are created before their fields, and both are created before the view display configuration.

The configuration dependency value is structured like this:

array(
  'config' => array(
    // An array of configuration entity object names. Recalculated on save.
  ),
  'content' => array(
    // An array of content entity configuration dependency names. The default
    // format is "ENTITY_TYPE_ID:BUNDLE:UUID". Recalculated on save.
  ),
  'module' => array(
    // An array of module names. Recalculated on save.
  ),
  'theme' => array(
    // An array of theme names. Recalculated on save.
  ),
  'enforced' => array(
    // An array of configuration dependencies that the config entity is
    // ensured to have regardless of the details of the configuration. These
    // dependencies are not recalculated on save.
    'config' => array(),
    'content' => array(),
    'module' => array(),
    'theme' => array(),
  ),
);

Configuration entity dependencies are recalculated on save based on the current values of the configuration. For example, a filter format will depend on the modules that provide the filter plugins it configures. The filter format can be reconfigured to use a different filter plugin provided by another module. If this occurs, the dependencies will be recalculated on save and the old module will be removed from the list of dependencies and replaced with the new one.

Configuration entity classes usually extend \Drupal\Core\Config\Entity\ConfigEntityBase. The base class provides a generic implementation of the calculateDependencies() method that can discover dependencies due to plugins, and third party settings. If the configuration entity has dependencies that cannot be discovered by the base class's implementation, then it needs to implement \Drupal\Core\Config\Entity\ConfigEntityInterface::calculateDependencies() to calculate the dependencies. In this method, use \Drupal\Core\Config\Entity\ConfigEntityBase::addDependency() to add dependencies. Implementations should call the base class implementation to inherit the generic functionality.

Classes for configurable plugins are a special case. They can either declare their configuration dependencies using the calculateDependencies() method described in the paragraph above, or if they have only static dependencies, these can be declared using the 'config_dependencies' annotation key.

If an extension author wants a configuration entity to depend on something that is not calculable then they can add these dependencies to the enforced dependencies key. For example, the Forum module provides the forum node type and in order for it to be deleted when the forum module is uninstalled it has an enforced dependency on the module. The dependency on the Forum module can not be calculated since there is nothing inherent in the state of the node type configuration entity that depends on functionality provided by the Forum module.

Once declared properly, dependencies are saved to the configuration entity's configuration object so that they can be checked without the module that provides the configuration entity class being installed. This is important for configuration synchronization, which needs to be able to validate configuration in the sync directory before the synchronization has occurred. Also, if you have a configuration entity object and you want to get the current dependencies (without recalculation), you can use \Drupal\Core\Config\Entity\ConfigEntityInterface::getDependencies().

When uninstalling a module or a theme, configuration entities that are dependent will also be removed. This default behavior can lead to undesirable side effects, such as a node view mode being entirely removed when the module defining a field or formatter it uses is uninstalled. To prevent this, configuration entity classes can implement \Drupal\Core\Config\Entity\ConfigEntityInterface::onDependencyRemoval(), which allows the entity class to remove dependencies instead of being deleted themselves. Implementations should save the entity if dependencies have been successfully removed, in order to register the newly cleaned-out dependency list. So, for example, the node view mode configuration entity class should implement this method to remove references to formatters if the plugin that supplies them depends on a module that is being uninstalled.

If a configuration entity is provided as default configuration by an extension (module, theme, or profile), the extension has to depend on any modules or themes that the configuration depends on. For example, if a view configuration entity is provided by an installation profile and the view will not work without a certain module, the profile must declare a dependency on this module in its info.yml file. If you do not want your extension to always depend on a particular module that one of its default configuration entities depends on, you can use a sub-module: move the configuration entity to the sub-module instead of including it in the main extension, and declare the module dependency in the sub-module only.

Hierarchy

See also

\Drupal\Core\Config\Entity\ConfigEntityInterface::calculateDependencies()

\Drupal\Core\Config\Entity\ConfigEntityInterface::getDependencies()

\Drupal\Core\Config\Entity\ConfigEntityInterface::onDependencyRemoval()

\Drupal\Core\Config\Entity\ConfigEntityBase::addDependency()

\Drupal\Core\Config\ConfigInstallerInterface::installDefaultConfig()

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

\Drupal\Core\Config\Entity\ConfigEntityDependency

\Drupal\Core\Entity\EntityInterface::getConfigDependencyName()

\Drupal\Core\Plugin\PluginDependencyTrait

File

core/lib/Drupal/Core/Config/Entity/ConfigDependencyManager.php, line 122

Namespace

Drupal\Core\Config\Entity

Members

Name Modifiers Type Description
ConfigDependencyManager::$data protected property The config entity data.
ConfigDependencyManager::$graph protected property The directed acyclic graph.
ConfigDependencyManager::createGraphConfigEntityDependencies protected function Creates a graph of config entity dependencies.
ConfigDependencyManager::getDependentEntities public function Gets dependencies.
ConfigDependencyManager::getGraph protected function Gets the dependency graph of all the config entities.
ConfigDependencyManager::prepareMultisort protected function Extracts data from the graph for use in array_multisort().
ConfigDependencyManager::setData public function Sets data to calculate dependencies for.
ConfigDependencyManager::sortAll public function Sorts the dependencies in order of most dependent last.
ConfigDependencyManager::sortGraph Deprecated public static function Sorts the dependency graph by reverse weight and alphabetically.
ConfigDependencyManager::sortGraphByWeight Deprecated protected static function Sorts the dependency graph by weight and alphabetically.
ConfigDependencyManager::updateData public function Updates one of the lightweight ConfigEntityDependency objects.
doc_Drupal
2016-10-29 08:52:19
Comments
Leave a Comment

Please login to continue.