diff --git a/core/lib/Drupal/Component/Plugin/ConfigurablePluginInterface.php b/core/lib/Drupal/Component/Plugin/ConfigurablePluginInterface.php index 5311ae8..23311b6 100644 --- a/core/lib/Drupal/Component/Plugin/ConfigurablePluginInterface.php +++ b/core/lib/Drupal/Component/Plugin/ConfigurablePluginInterface.php @@ -36,4 +36,28 @@ public function setConfiguration(array $configuration); */ public function defaultConfiguration(); + /** + * Calculates dependencies for the configured plugin. + * + * Dependencies are saved in the plugin's configuration entity and are used to + * determine configuration synchronization order. For example, if the plugin + * integrates with specific user roles, this method should return an array of + * dependencies listing the specified roles. + * + * @return array + * An array of dependencies grouped by type (module, theme, entity). For + * example: + * @code + * array( + * 'entity' => array('user.role.anonymous', 'user.role.authenticated'), + * 'module' => array('node', 'user'), + * 'theme' => array('seven'), + * ); + * @endcode + * + * @see \Drupal\Core\Config\Entity\ConfigDependencyManager + * @see \Drupal\Core\Config\Entity\ConfigEntityInterface::getConfigDependencyName() + */ + public function calculateDependencies(); + } diff --git a/core/lib/Drupal/Core/Action/ConfigurableActionBase.php b/core/lib/Drupal/Core/Action/ConfigurableActionBase.php index cbd44b1..c32695b 100644 --- a/core/lib/Drupal/Core/Action/ConfigurableActionBase.php +++ b/core/lib/Drupal/Core/Action/ConfigurableActionBase.php @@ -52,4 +52,11 @@ public function setConfiguration(array $configuration) { public function validateConfigurationForm(array &$form, array &$form_state) { } + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + return array(); + } + } diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php index 257d88f..d604caf 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Config\Entity; +use Drupal\Component\Plugin\ConfigurablePluginInterface; use Drupal\Component\Utility\String; use Drupal\Core\Cache\Cache; use Drupal\Core\Entity\Entity; @@ -307,11 +308,11 @@ public function calculateDependencies() { $this->addDependency('module', $definition['provider']); // Plugins can declare additional dependencies in their definition. if (isset($definition['config_dependencies'])) { - foreach ($definition['config_dependencies'] as $type => $dependencies) { - foreach ($dependencies as $dependency) { - $this->addDependency($type, $dependency); - } - } + $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/aggregator/lib/Drupal/aggregator/Plugin/AggregatorPluginSettingsBase.php b/core/modules/aggregator/lib/Drupal/aggregator/Plugin/AggregatorPluginSettingsBase.php index 9ca7c79..80493c0 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/Plugin/AggregatorPluginSettingsBase.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/Plugin/AggregatorPluginSettingsBase.php @@ -29,4 +29,11 @@ public function defaultConfiguration() { public function validateConfigurationForm(array &$form, array &$form_state) { } + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + return array(); + } + } diff --git a/core/modules/block/lib/Drupal/block/BlockBase.php b/core/modules/block/lib/Drupal/block/BlockBase.php index af0c1a4..85f615c 100644 --- a/core/modules/block/lib/Drupal/block/BlockBase.php +++ b/core/modules/block/lib/Drupal/block/BlockBase.php @@ -87,6 +87,13 @@ public function setConfigurationValue($key, $value) { /** * {@inheritdoc} */ + public function calculateDependencies() { + return array(); + } + + /** + * {@inheritdoc} + */ public function access(AccountInterface $account) { // By default, the block is visible unless user-configured rules indicate // that it should be hidden. diff --git a/core/modules/filter/lib/Drupal/filter/Plugin/FilterBase.php b/core/modules/filter/lib/Drupal/filter/Plugin/FilterBase.php index bf5e7a0..facd818 100644 --- a/core/modules/filter/lib/Drupal/filter/Plugin/FilterBase.php +++ b/core/modules/filter/lib/Drupal/filter/Plugin/FilterBase.php @@ -116,6 +116,13 @@ public function defaultConfiguration() { /** * {@inheritdoc} */ + public function calculateDependencies() { + return array(); + } + + /** + * {@inheritdoc} + */ public function getType() { return $this->pluginDefinition['type']; } diff --git a/core/modules/image/lib/Drupal/image/ImageEffectBase.php b/core/modules/image/lib/Drupal/image/ImageEffectBase.php index 5e243fb..ea3208a 100644 --- a/core/modules/image/lib/Drupal/image/ImageEffectBase.php +++ b/core/modules/image/lib/Drupal/image/ImageEffectBase.php @@ -116,4 +116,11 @@ public function defaultConfiguration() { return array(); } + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + return array(); + } + } diff --git a/core/modules/search/lib/Drupal/search/Plugin/ConfigurableSearchPluginBase.php b/core/modules/search/lib/Drupal/search/Plugin/ConfigurableSearchPluginBase.php index 0c233e0..8dcc9a4 100644 --- a/core/modules/search/lib/Drupal/search/Plugin/ConfigurableSearchPluginBase.php +++ b/core/modules/search/lib/Drupal/search/Plugin/ConfigurableSearchPluginBase.php @@ -60,6 +60,13 @@ public function validateConfigurationForm(array &$form, array &$form_state) { /** * {@inheritdoc} */ + public function calculateDependencies() { + return array(); + } + + /** + * {@inheritdoc} + */ public function setSearchPageId($search_page_id) { $this->searchPageId = $search_page_id; return $this; diff --git a/core/modules/system/lib/Drupal/system/Tests/Action/ActionUnitTest.php b/core/modules/system/lib/Drupal/system/Tests/Action/ActionUnitTest.php index 6f97da1..7e2f651 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Action/ActionUnitTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Action/ActionUnitTest.php @@ -83,4 +83,31 @@ public function testOperations() { $this->assertEqual($name, $account->label()); } + /** + * Tests the dependency calculation of actions. + */ + public function testDependencies() { + // Create a new action that depends on a user role. + $action = entity_create('action', array( + 'id' => 'user_add_role_action.' . DRUPAL_ANONYMOUS_RID, + 'type' => 'user', + 'label' => t('Add the anonymous role to the selected users'), + 'configuration' => array( + 'rid' => DRUPAL_ANONYMOUS_RID, + ), + 'plugin' => 'user_add_role_action', + )); + $action->save(); + + $expected = array( + 'entity' => array( + 'user.role.' . DRUPAL_ANONYMOUS_RID, + ), + 'module' => array( + 'user', + ), + ); + $this->assertIdentical($expected, $action->calculateDependencies()); + } + } 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 ef8613f..758d84c 100644 --- a/core/modules/user/lib/Drupal/user/Plugin/Action/ChangeUserRoleBase.php +++ b/core/modules/user/lib/Drupal/user/Plugin/Action/ChangeUserRoleBase.php @@ -8,11 +8,41 @@ namespace Drupal\user\Plugin\Action; use Drupal\Core\Action\ConfigurableActionBase; +use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a base class for operations to change a user's role. */ -abstract class ChangeUserRoleBase extends ConfigurableActionBase { +abstract class ChangeUserRoleBase extends ConfigurableActionBase implements ContainerFactoryPluginInterface { + + /** + * The user role entity type. + * + * @var \Drupal\Core\Entity\EntityTypeInterface + */ + protected $entityType; + + /** + * {@inheritdoc} + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeInterface $entity_type) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->entityType = $entity_type; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity.manager')->getDefinition('user_role') + ); + } /** * {@inheritdoc} @@ -46,4 +76,16 @@ public function submitConfigurationForm(array &$form, array &$form_state) { $this->configuration['rid'] = $form_state['values']['rid']; } + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + $dependencies = array(); + if (!empty($this->configuration['rid'])) { + $prefix = $this->entityType->getConfigPrefix() . '.'; + $dependencies['entity'][] = $prefix . $this->configuration['rid']; + } + return $dependencies; + } + } diff --git a/core/modules/user/tests/Drupal/user/Tests/Plugin/Action/AddRoleUserTest.php b/core/modules/user/tests/Drupal/user/Tests/Plugin/Action/AddRoleUserTest.php index bc32dbc..fdd4485 100644 --- a/core/modules/user/tests/Drupal/user/Tests/Plugin/Action/AddRoleUserTest.php +++ b/core/modules/user/tests/Drupal/user/Tests/Plugin/Action/AddRoleUserTest.php @@ -7,7 +7,6 @@ namespace Drupal\user\Tests\Plugin\Action; -use Drupal\Tests\UnitTestCase; use Drupal\user\Plugin\Action\AddRoleUser; /** @@ -15,15 +14,11 @@ * * @see \Drupal\user\Plugin\Action\AddRoleUser */ -class AddRoleUserTest extends UnitTestCase { +class AddRoleUserTest extends RoleUserTestBase { /** - * The mocked account. - * - * @var \Drupal\user\UserInterface + * {@inheritdoc} */ - protected $account; - public static function getInfo() { return array( 'name' => 'Add user plugin', @@ -33,18 +28,6 @@ public static function getInfo() { } /** - * {@inheritdoc} - */ - protected function setUp() { - parent::setUp(); - - $this->account = $this - ->getMockBuilder('Drupal\user\Entity\User') - ->disableOriginalConstructor() - ->getMock(); - } - - /** * Tests the execute method on a user with a role. */ public function testExecuteAddExistingRole() { @@ -57,7 +40,7 @@ public function testExecuteAddExistingRole() { ->will($this->returnValue(TRUE)); $config = array('rid' => 'test_role_1'); - $remove_role_plugin = new AddRoleUser($config, 'user_add_role_action', array('type' => 'user')); + $remove_role_plugin = new AddRoleUser($config, 'user_add_role_action', array('type' => 'user'), $this->userRoleEntityType); $remove_role_plugin->execute($this->account); } @@ -75,7 +58,7 @@ public function testExecuteAddNonExistingRole() { ->will($this->returnValue(FALSE)); $config = array('rid' => 'test_role_1'); - $remove_role_plugin = new AddRoleUser($config, 'user_remove_role_action', array('type' => 'user')); + $remove_role_plugin = new AddRoleUser($config, 'user_remove_role_action', array('type' => 'user'), $this->userRoleEntityType); $remove_role_plugin->execute($this->account); } diff --git a/core/modules/user/tests/Drupal/user/Tests/Plugin/Action/RemoveRoleUserTest.php b/core/modules/user/tests/Drupal/user/Tests/Plugin/Action/RemoveRoleUserTest.php index 7f650a7..e72113c 100644 --- a/core/modules/user/tests/Drupal/user/Tests/Plugin/Action/RemoveRoleUserTest.php +++ b/core/modules/user/tests/Drupal/user/Tests/Plugin/Action/RemoveRoleUserTest.php @@ -7,7 +7,6 @@ namespace Drupal\user\Tests\Plugin\Action; -use Drupal\Tests\UnitTestCase; use Drupal\user\Plugin\Action\RemoveRoleUser; /** @@ -15,15 +14,11 @@ * * @see \Drupal\user\Plugin\Action\RemoveRoleUser */ -class RemoveRoleUserTest extends UnitTestCase { +class RemoveRoleUserTest extends RoleUserTestBase { /** - * The mocked account. - * - * @var \Drupal\user\UserInterface + * {@inheritdoc} */ - protected $account; - public static function getInfo() { return array( 'name' => 'Remove user plugin', @@ -33,18 +28,6 @@ public static function getInfo() { } /** - * {@inheritdoc} - */ - protected function setUp() { - parent::setUp(); - - $this->account = $this - ->getMockBuilder('Drupal\user\Entity\User') - ->disableOriginalConstructor() - ->getMock(); - } - - /** * Tests the execute method on a user with a role. */ public function testExecuteRemoveExistingRole() { @@ -57,7 +40,7 @@ public function testExecuteRemoveExistingRole() { ->will($this->returnValue(TRUE)); $config = array('rid' => 'test_role_1'); - $remove_role_plugin = new RemoveRoleUser($config, 'user_remove_role_action', array('type' => 'user')); + $remove_role_plugin = new RemoveRoleUser($config, 'user_remove_role_action', array('type' => 'user'), $this->userRoleEntityType); $remove_role_plugin->execute($this->account); } @@ -75,7 +58,7 @@ public function testExecuteRemoveNonExistingRole() { ->will($this->returnValue(FALSE)); $config = array('rid' => 'test_role_1'); - $remove_role_plugin = new RemoveRoleUser($config, 'user_remove_role_action', array('type' => 'user')); + $remove_role_plugin = new RemoveRoleUser($config, 'user_remove_role_action', array('type' => 'user'), $this->userRoleEntityType); $remove_role_plugin->execute($this->account); } diff --git a/core/modules/user/tests/Drupal/user/Tests/Plugin/Action/RoleUserTestBase.php b/core/modules/user/tests/Drupal/user/Tests/Plugin/Action/RoleUserTestBase.php new file mode 100644 index 0000000..54c8fa0 --- /dev/null +++ b/core/modules/user/tests/Drupal/user/Tests/Plugin/Action/RoleUserTestBase.php @@ -0,0 +1,44 @@ +account = $this + ->getMockBuilder('Drupal\user\Entity\User') + ->disableOriginalConstructor() + ->getMock(); + $this->userRoleEntityType = $this->getMock('Drupal\Core\Entity\EntityTypeInterface'); + } + +} diff --git a/core/tests/Drupal/Tests/Core/Plugin/Fixtures/TestConfigurablePlugin.php b/core/tests/Drupal/Tests/Core/Plugin/Fixtures/TestConfigurablePlugin.php index c12ce74..84ccb6f 100644 --- a/core/tests/Drupal/Tests/Core/Plugin/Fixtures/TestConfigurablePlugin.php +++ b/core/tests/Drupal/Tests/Core/Plugin/Fixtures/TestConfigurablePlugin.php @@ -33,4 +33,11 @@ public function defaultConfiguration() { return array(); } + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + return array(); + } + } diff --git a/db.sqlite b/db.sqlite new file mode 100644 index 0000000..e69de29