diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php index e020a2ebedd56edb3d98c9751ea5554a99fff2d5..948ec6f4d1f6d393bd991c6ffe649b495eddb4b6 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php @@ -14,6 +14,7 @@ use Drupal\Core\Config\ConfigDuplicateUUIDException; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Language\Language; +use Drupal\Core\Plugin\PluginDependencyTrait; /** * Defines a base configuration entity class. @@ -22,6 +23,10 @@ */ abstract class ConfigEntityBase extends Entity implements ConfigEntityInterface { + use PluginDependencyTrait { + addDependency as addDependencyTrait; + } + /** * The original ID of the configuration entity. * @@ -72,13 +77,6 @@ abstract class ConfigEntityBase extends Entity implements ConfigEntityInterface */ private $isUninstalling = FALSE; - /** - * The configuration entity's dependencies. - * - * @var array - */ - protected $dependencies = array(); - /** * The language code of the entity's default language. * @@ -305,17 +303,8 @@ public function calculateDependencies() { // Configuration entities need to depend on the providers of any plugins // that they store the configuration for. $plugin_bag = $this->getPluginBag(); - foreach($plugin_bag as $instance) { - $definition = $instance->getPluginDefinition(); - $this->addDependency('module', $definition['provider']); - // Plugins can declare additional dependencies in their definition. - if (isset($definition['config_dependencies'])) { - $this->addDependencies($definition['config_dependencies']); - } - // If a plugin is configurable, calculate its dependencies. - if ($instance instanceof ConfigurablePluginInterface && $plugin_dependencies = $instance->calculateDependencies()) { - $this->addDependencies($plugin_dependencies); - } + foreach ($plugin_bag as $instance) { + $this->calculatePluginDependencies($instance); } } return $this->dependencies; @@ -343,18 +332,7 @@ public function url($rel = 'edit-form', $options = array()) { } /** - * Creates a dependency. - * - * @param string $type - * The type of dependency being checked. Either 'module', 'theme', 'entity'. - * @param string $name - * If $type equals 'module' or 'theme' then it should be the name of the - * module or theme. In the case of entity it should be the full - * configuration object name. - * - * @see \Drupal\Core\Config\Entity\ConfigEntityInterface::getConfigDependencyName() - * - * @return $this + * {@inheritdoc} */ protected function addDependency($type, $name) { // A config entity is always dependent on its provider. There is no need to @@ -364,44 +342,8 @@ protected function addDependency($type, $name) { if ($type == 'module' && ($name == $this->getEntityType()->getProvider() || $name == 'Core')) { return $this; } - 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 ::addDependency - */ - protected function addDependencies(array $dependencies) { - foreach ($dependencies as $dependency_type => $list) { - foreach ($list as $name) { - $this->addDependency($dependency_type, $name); - } - } + return $this->addDependencyTrait($type, $name); } /** diff --git a/core/lib/Drupal/Core/Entity/DependencyTrait.php b/core/lib/Drupal/Core/Entity/DependencyTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..2c77834a494d9ed4f819b5a8e56176b86c8daaeb --- /dev/null +++ b/core/lib/Drupal/Core/Entity/DependencyTrait.php @@ -0,0 +1,77 @@ +<?php + +/** + * @file + * Contains \Drupal\Core\Entity\DependencyTrait. + */ + +namespace Drupal\Core\Entity; + +/** + * Provides a trait for managing an object's dependencies. + */ +trait DependencyTrait { + + /** + * The object's dependencies. + * + * @var array + */ + protected $dependencies = array(); + + /** + * Creates a dependency. + * + * @param string $type + * The type of dependency being checked. Either 'module', 'theme', 'entity'. + * @param string $name + * If $type equals 'module' or 'theme' then it should be the name of the + * module or theme. In the case of entity it should be the full + * configuration object name. + * + * @see \Drupal\Core\Config\Entity\ConfigEntityInterface::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 ::addDependency + */ + protected function addDependencies(array $dependencies) { + foreach ($dependencies as $dependency_type => $list) { + foreach ($list as $name) { + $this->addDependency($dependency_type, $name); + } + } + } + +} diff --git a/core/lib/Drupal/Core/Plugin/PluginDependencyTrait.php b/core/lib/Drupal/Core/Plugin/PluginDependencyTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..21ec3ba6c957aed3a6a18c77b5d29565b3f6794b --- /dev/null +++ b/core/lib/Drupal/Core/Plugin/PluginDependencyTrait.php @@ -0,0 +1,40 @@ +<?php + +/** + * @file + * Contains \Drupal\Core\Plugin\PluginDependencyTrait. + */ + +namespace Drupal\Core\Plugin; + +use Drupal\Component\Plugin\ConfigurablePluginInterface; +use Drupal\Component\Plugin\PluginInspectionInterface; +use Drupal\Core\Entity\DependencyTrait; + +/** + * Provides a trait for calculating the dependencies of a plugin. + */ +trait PluginDependencyTrait { + + use DependencyTrait; + + /** + * Calculates the dependencies of a specific plugin instance. + * + * @param \Drupal\Component\Plugin\PluginInspectionInterface $instance + * The plugin instance. + */ + protected function calculatePluginDependencies(PluginInspectionInterface $instance) { + $definition = $instance->getPluginDefinition(); + $this->addDependency('module', $definition['provider']); + // Plugins can declare additional dependencies in their definition. + if (isset($definition['config_dependencies'])) { + $this->addDependencies($definition['config_dependencies']); + } + // If a plugin is configurable, calculate its dependencies. + if ($instance instanceof ConfigurablePluginInterface && $plugin_dependencies = $instance->calculateDependencies()) { + $this->addDependencies($plugin_dependencies); + } + } + +} diff --git a/core/modules/user/lib/Drupal/user/Plugin/Action/ChangeUserRoleBase.php b/core/modules/user/lib/Drupal/user/Plugin/Action/ChangeUserRoleBase.php index 758d84c8ff767d0a992b3267ef852fb157e86764..f844743c6c8f7579d22f2b667ca87da0417e07cd 100644 --- a/core/modules/user/lib/Drupal/user/Plugin/Action/ChangeUserRoleBase.php +++ b/core/modules/user/lib/Drupal/user/Plugin/Action/ChangeUserRoleBase.php @@ -8,6 +8,7 @@ namespace Drupal\user\Plugin\Action; use Drupal\Core\Action\ConfigurableActionBase; +use Drupal\Core\Entity\DependencyTrait; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -17,6 +18,8 @@ */ abstract class ChangeUserRoleBase extends ConfigurableActionBase implements ContainerFactoryPluginInterface { + use DependencyTrait; + /** * The user role entity type. * @@ -80,12 +83,11 @@ public function submitConfigurationForm(array &$form, array &$form_state) { * {@inheritdoc} */ public function calculateDependencies() { - $dependencies = array(); if (!empty($this->configuration['rid'])) { $prefix = $this->entityType->getConfigPrefix() . '.'; - $dependencies['entity'][] = $prefix . $this->configuration['rid']; + $this->addDependency('entity', $prefix . $this->configuration['rid']); } - return $dependencies; + return $this->dependencies; } }