diff --git a/core/lib/Drupal/Component/Plugin/PluginBag.php b/core/lib/Drupal/Component/Plugin/PluginBag.php index 977cab26075e538f5c1c49e2f2b0bfe2534d4058..9631e22cc0c1c7e0e698b2d94f7cfa67e5617808 100644 --- a/core/lib/Drupal/Component/Plugin/PluginBag.php +++ b/core/lib/Drupal/Component/Plugin/PluginBag.php @@ -115,8 +115,10 @@ public function remove($instance_id) { * * @param string $id * The ID of the plugin instance to add. + * @param array|null $configuration + * (optional) The configuration used by this instance. Defaults to NULL. */ - public function addInstanceId($id) { + public function addInstanceId($id, $configuration = NULL) { if (!isset($this->instanceIDs[$id])) { $this->instanceIDs[$id] = $id; } @@ -132,21 +134,11 @@ public function getInstanceIds() { return $this->instanceIDs; } - /** - * Sets all instance IDs. - * - * @param array $instance_ids - * An associative array of instance IDs. - */ - public function setInstanceIds(array $instance_ids) { - $this->instanceIDs = $instance_ids; - } - /** * Removes an instance ID. * * @param string $instance_id - * An image effect instance IDs. + * The ID of the plugin instance to remove. */ public function removeInstanceId($instance_id) { unset($this->instanceIDs[$instance_id]); diff --git a/core/lib/Drupal/Core/Plugin/DefaultPluginBag.php b/core/lib/Drupal/Core/Plugin/DefaultPluginBag.php index ff6d922d0bbb3ddcb3d3c71bbe9e876f1b079e6e..a6bb5c9cb8ca1d6f5b8c63e8850b200abdfb5950 100644 --- a/core/lib/Drupal/Core/Plugin/DefaultPluginBag.php +++ b/core/lib/Drupal/Core/Plugin/DefaultPluginBag.php @@ -138,15 +138,6 @@ public function setConfiguration($configuration) { return $this; } - /** - * {@inheritdoc} - */ - public function setInstanceIds(array $instance_ids) { - parent::setInstanceIds($instance_ids); - // Ensure the new order matches the original order. - $this->instanceIDs = $this->originalOrder = array_intersect_assoc($this->originalOrder, $this->instanceIDs); - } - /** * Updates the configuration for a plugin instance. * @@ -166,6 +157,19 @@ public function setInstanceConfiguration($instance_id, array $configuration) { } } + /** + * {@inheritdoc} + */ + public function addInstanceId($id, $configuration = NULL) { + parent::addInstanceId($id); + if ($configuration !== NULL) { + $this->setInstanceConfiguration($id, $configuration); + } + if (!isset($this->originalOrder[$id])) { + $this->originalOrder[$id] = $id; + } + } + /** * {@inheritdoc} */ diff --git a/core/lib/Drupal/Core/Plugin/DefaultSinglePluginBag.php b/core/lib/Drupal/Core/Plugin/DefaultSinglePluginBag.php index 7b1c816ad7d91ef4633480fa3286bbc74be83eff..2108ce1d1618ccf423b59d7da7cff0cd4ec172fc 100644 --- a/core/lib/Drupal/Core/Plugin/DefaultSinglePluginBag.php +++ b/core/lib/Drupal/Core/Plugin/DefaultSinglePluginBag.php @@ -93,4 +93,14 @@ public function setConfiguration($configuration) { return $this; } + /** + * {@inheritdoc} + */ + public function addInstanceId($id, $configuration = NULL) { + parent::addInstanceId($id, $configuration); + if ($configuration !== NULL) { + $this->setConfiguration($configuration); + } + } + } diff --git a/core/modules/image/lib/Drupal/image/ImageEffectBag.php b/core/modules/image/lib/Drupal/image/ImageEffectBag.php index 2dc76a329d2749a10be7b38b978ee67a226d90d0..ad9f48525184f90c427ea1db25c153477bccf469 100644 --- a/core/modules/image/lib/Drupal/image/ImageEffectBag.php +++ b/core/modules/image/lib/Drupal/image/ImageEffectBag.php @@ -42,8 +42,7 @@ public function updateConfiguration(array $configuration) { $configuration['uuid'] = $uuid_generator->generate(); } $instance_id = $configuration['uuid']; - $this->setInstanceConfiguration($instance_id, $configuration); - $this->addInstanceId($instance_id); + $this->addInstanceId($instance_id, $configuration); return $instance_id; } diff --git a/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginBagTest.php b/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginBagTest.php index 8f9cce0a33b2f5ffb2dc0714744efd3d16b02e89..18eec549a10fead5f6e451740c8a488667aacd11 100644 --- a/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginBagTest.php +++ b/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginBagTest.php @@ -127,6 +127,34 @@ public function testGetConfiguration() { $this->assertSame($expected, array_keys($ids), 'After sorting, the order of the instances is also sorted.'); } + /** + * Tests the addInstanceId() method. + */ + public function testAddInstanceId() { + $this->setupPluginBag($this->exactly(4)); + $expected = array( + 'banana' => 'banana', + 'cherry' => 'cherry', + 'apple' => 'apple', + ); + $this->defaultPluginBag->addInstanceId('apple'); + $result = $this->defaultPluginBag->getInstanceIds(); + $this->assertSame($expected, $result); + $this->assertSame($expected, array_intersect_key($result, $this->defaultPluginBag->getConfiguration())); + + $expected = array( + 'cherry' => 'cherry', + 'apple' => 'apple', + 'banana' => 'banana', + ); + $this->defaultPluginBag->removeInstanceId('banana'); + $this->defaultPluginBag->addInstanceId('banana', $this->config['banana']); + + $result = $this->defaultPluginBag->getInstanceIds(); + $this->assertSame($expected, $result); + $this->assertSame($expected, array_intersect_key($result, $this->defaultPluginBag->getConfiguration())); + } + /** * Tests the removeInstanceId() method. * @@ -175,27 +203,6 @@ public function testClear() { $this->defaultPluginBag->getConfiguration(); } - /** - * Tests the setInstanceIds() method. - */ - public function testSetInstanceIds() { - $this->setupPluginBag($this->any()); - // Set the instance IDs in a different order than the original. - $this->defaultPluginBag->setInstanceIds(array( - 'apple' => 'apple', - 'cherry' => 'cherry', - )); - - $expected = array( - 'cherry' => 'cherry', - 'apple' => 'apple', - ); - $config = $this->defaultPluginBag->getConfiguration(); - $instance_ids = $this->defaultPluginBag->getInstanceIds(); - $this->assertSame($expected, $instance_ids); - $this->assertSame(array_keys($expected), array_keys($config)); - } - /** * Tests the set() method. */