function DependencyTrait::addDependency

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/DependencyTrait.php \Drupal\Core\Entity\DependencyTrait::addDependency()
  2. 8.9.x core/lib/Drupal/Core/Entity/DependencyTrait.php \Drupal\Core\Entity\DependencyTrait::addDependency()
  3. 11.x core/lib/Drupal/Core/Entity/DependencyTrait.php \Drupal\Core\Entity\DependencyTrait::addDependency()

Adds a dependency.

Parameters

string $type: Type of dependency being added: 'module', 'theme', 'config', 'content'.

string $name: If $type is 'module' or 'theme', the name of the module or theme. If $type is 'config' or 'content', the result of EntityInterface::getConfigDependencyName().

Return value

$this

See also

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

7 calls to DependencyTrait::addDependency()
ChangeUserRoleBase::calculateDependencies in core/modules/user/src/Plugin/Action/ChangeUserRoleBase.php
Calculates dependencies for the configured plugin.
Config::calculateDependencies in core/modules/migrate/src/Plugin/migrate/destination/Config.php
Calculates dependencies for the configured plugin.
ConfigEntityBase::addDependency in core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php
Overrides \Drupal\Core\Entity\DependencyTrait:addDependency().
DependencyTrait::addDependencies in core/lib/Drupal/Core/Entity/DependencyTrait.php
Adds multiple dependencies.
DrupalSqlBase::calculateDependencies in core/modules/migrate_drupal/src/Plugin/migrate/source/DrupalSqlBase.php
Calculates dependencies for the configured plugin.

... See full list

File

core/lib/Drupal/Core/Entity/DependencyTrait.php, line 31

Class

DependencyTrait
Provides a trait for managing an object's dependencies.

Namespace

Drupal\Core\Entity

Code

protected function addDependency($type, $name) {
    if (empty($this->dependencies[$type])) {
        $this->dependencies[$type] = [
            $name,
        ];
        if (count($this->dependencies) > 1) {
            // Ensure a consistent order of type keys.
            ksort($this->dependencies);
        }
    }
    elseif (!in_array($name, $this->dependencies[$type])) {
        $this->dependencies[$type][] = $name;
        // Ensure a consistent order of dependency names.
        sort($this->dependencies[$type], SORT_FLAG_CASE);
    }
    return $this;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.