3 namespace Drupal\Core\Entity;
6 * Provides a trait for managing an object's dependencies.
8 trait DependencyTrait {
11 * The object's dependencies.
15 protected $dependencies = [];
21 * Type of dependency being added: 'module', 'theme', 'config', 'content'.
23 * If $type is 'module' or 'theme', the name of the module or theme. If
24 * $type is 'config' or 'content', the result of
25 * EntityInterface::getConfigDependencyName().
27 * @see \Drupal\Core\Entity\EntityInterface::getConfigDependencyName()
31 protected function addDependency($type, $name) {
32 if (empty($this->dependencies[$type])) {
33 $this->dependencies[$type] = [$name];
34 if (count($this->dependencies) > 1) {
35 // Ensure a consistent order of type keys.
36 ksort($this->dependencies);
39 elseif (!in_array($name, $this->dependencies[$type])) {
40 $this->dependencies[$type][] = $name;
41 // Ensure a consistent order of dependency names.
42 sort($this->dependencies[$type], SORT_FLAG_CASE);
48 * Adds multiple dependencies.
50 * @param array $dependencies
51 * An array of dependencies keyed by the type of dependency. One example:
62 * @see \Drupal\Core\Entity\DependencyTrait::addDependency
64 protected function addDependencies(array $dependencies) {
65 foreach ($dependencies as $dependency_type => $list) {
66 foreach ($list as $name) {
67 $this->addDependency($dependency_type, $name);