Commit 9b584232 authored by webchick's avatar webchick

Issue #2266859 by tim.plunkett: Move the dependency calculation helper methods...

Issue #2266859 by tim.plunkett: Move the dependency calculation helper methods from ConfigEntityBase to generic traits.
parent bfb8fdf9
......@@ -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);
}
/**
......
<?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);
}
}
}
}
<?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);
}
}
}
......@@ -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;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment