You are here

trait DependencyTrait in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Entity/DependencyTrait.php \Drupal\Core\Entity\DependencyTrait

Provides a trait for managing an object's dependencies.

Hierarchy

6 files declare their use of DependencyTrait
ChangeUserRoleBase.php in core/modules/user/src/Plugin/Action/ChangeUserRoleBase.php
Contains \Drupal\user\Plugin\Action\ChangeUserRoleBase.
Config.php in core/modules/migrate/src/Plugin/migrate/destination/Config.php
Contains \Drupal\migrate\Plugin\migrate\destination\Config.
DrupalSqlBase.php in core/modules/migrate_drupal/src/Plugin/migrate/source/DrupalSqlBase.php
Contains \Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase.
EmptySource.php in core/modules/migrate_drupal/src/Plugin/migrate/source/EmptySource.php
Contains \Drupal\migrate_drupal\Plugin\migrate\source\EmptySource.
Entity.php in core/modules/migrate/src/Plugin/migrate/destination/Entity.php
Contains \Drupal\migrate\Plugin\migrate\destination\Entity.

... See full list

File

core/lib/Drupal/Core/Entity/DependencyTrait.php, line 13
Contains \Drupal\Core\Entity\DependencyTrait.

Namespace

Drupal\Core\Entity
View source
trait DependencyTrait {

  /**
   * The object's dependencies.
   *
   * @var array
   */
  protected $dependencies = array();

  /**
   * Adds a dependency.
   *
   * @param string $type
   *   Type of dependency being added: 'module', 'theme', 'config', 'content'.
   * @param 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().
   *
   * @see \Drupal\Core\Entity\EntityInterface::getConfigDependencyName()
   *
   * @return $this
   */
  protected function addDependency($type, $name) {
    if (empty($this->dependencies[$type])) {
      $this->dependencies[$type] = array(
        $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;
  }

  /**
   * Adds multiple dependencies.
   *
   * @param array $dependencies.
   *   An array of dependencies keyed by the type of dependency. One example:
   *   @code
   *   array(
   *     'module' => array(
   *       'node',
   *       'field',
   *       'image',
   *     ),
   *   );
   *   @endcode
   *
   * @see \Drupal\Core\Entity\DependencyTrait::addDependency
   */
  protected function addDependencies(array $dependencies) {
    foreach ($dependencies as $dependency_type => $list) {
      foreach ($list as $name) {
        $this
          ->addDependency($dependency_type, $name);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencyTrait::$dependencies protected property The object's dependencies. 1
DependencyTrait::addDependencies protected function Adds multiple dependencies.
DependencyTrait::addDependency protected function Adds a dependency.