Commit 85f243e2 authored by webchick's avatar webchick

Issue #2267329 by tim.plunkett | joachim: Move EntityWithPluginBagInterface...

Issue #2267329 by tim.plunkett | joachim: Move EntityWithPluginBagInterface out of Drupal\Core\Config\Entity and make it useful for more than one bag.
parent fa17d471
......@@ -35,7 +35,7 @@
* \Drupal\Core\Config\Entity\ConfigEntityBase::calculateDependencies() which
* resets the dependencies and provides an implementation to determine the
* plugin providers for configuration entities that implement
* \Drupal\Core\Config\Entity\EntityWithPluginBagInterface.
* \Drupal\Core\Entity\EntityWithPluginBagsInterface.
*
* The configuration manager service provides methods to find dependencies for
* a specified module, theme or configuration entity.
......
......@@ -13,6 +13,7 @@
use Drupal\Core\Entity\Entity;
use Drupal\Core\Config\ConfigDuplicateUUIDException;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityWithPluginBagsInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Plugin\PluginDependencyTrait;
......@@ -136,10 +137,11 @@ public function get($property_name) {
* {@inheritdoc}
*/
public function set($property_name, $value) {
if ($this instanceof EntityWithPluginBagInterface) {
if ($property_name == $this->pluginConfigKey) {
if ($this instanceof EntityWithPluginBagsInterface) {
$plugin_bags = $this->getPluginBags();
if (isset($plugin_bags[$property_name])) {
// If external code updates the settings, pass it along to the plugin.
$this->getPluginBag()->setConfiguration($value);
$plugin_bags[$property_name]->setConfiguration($value);
}
}
......@@ -257,11 +259,12 @@ public function toArray() {
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
if ($this instanceof EntityWithPluginBagInterface) {
if ($this instanceof EntityWithPluginBagsInterface) {
// Any changes to the plugin configuration must be saved to the entity's
// copy as well.
$plugin_bag = $this->getPluginBag();
$this->set($this->pluginConfigKey, $plugin_bag->getConfiguration());
foreach ($this->getPluginBags() as $plugin_config_key => $plugin_bag) {
$this->set($plugin_config_key, $plugin_bag->getConfiguration());
}
}
// Ensure this entity's UUID does not exist with a different ID, regardless
......@@ -297,16 +300,15 @@ public function calculateDependencies() {
// Dependencies should be recalculated on every save. This ensures stale
// dependencies are never saved.
$this->dependencies = array();
// @todo When \Drupal\Core\Config\Entity\EntityWithPluginBagInterface moves
// to a trait, switch to class_uses() instead.
if ($this instanceof EntityWithPluginBagInterface) {
if ($this instanceof EntityWithPluginBagsInterface) {
// Configuration entities need to depend on the providers of any plugins
// that they store the configuration for.
$plugin_bag = $this->getPluginBag();
foreach ($this->getPluginBags() as $plugin_bag) {
foreach ($plugin_bag as $instance) {
$this->calculatePluginDependencies($instance);
}
}
}
return $this->dependencies;
}
......
<?php
/**
* @file
* Contains \Drupal\Core\Config\Entity\EntityWithPluginBagInterface.
*/
namespace Drupal\Core\Config\Entity;
/**
* Provides an interface for an object utilizing a plugin bag.
*
* @see \Drupal\Component\Plugin\PluginBag
*/
interface EntityWithPluginBagInterface extends ConfigEntityInterface {
/**
* Returns the plugin bag used by this entity.
*
* @return \Drupal\Component\Plugin\PluginBag
*/
public function getPluginBag();
}
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityWithPluginBagsInterface.
*/
namespace Drupal\Core\Entity;
/**
* Provides an interface for an object utilizing a plugin bag.
*
* @see \Drupal\Component\Plugin\PluginBag
*/
interface EntityWithPluginBagsInterface extends EntityInterface {
/**
* Returns the plugin bags used by this entity.
*
* @return \Drupal\Component\Plugin\PluginBag[]
* An array of plugin bags, keyed by the property name they use to store
* their configuration.
*/
public function getPluginBags();
}
......@@ -12,7 +12,7 @@
use Drupal\block\BlockPluginBag;
use Drupal\block\BlockInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Config\Entity\EntityWithPluginBagInterface;
use Drupal\Core\Entity\EntityWithPluginBagsInterface;
use Drupal\Core\Entity\EntityStorageInterface;
/**
......@@ -42,7 +42,7 @@
* }
* )
*/
class Block extends ConfigEntityBase implements BlockInterface, EntityWithPluginBagInterface {
class Block extends ConfigEntityBase implements BlockInterface, EntityWithPluginBagsInterface {
/**
* The ID of the block.
......@@ -86,11 +86,6 @@ class Block extends ConfigEntityBase implements BlockInterface, EntityWithPlugin
*/
protected $pluginBag;
/**
* {@inheritdoc}
*/
protected $pluginConfigKey = 'settings';
/**
* The visibility settings.
*
......@@ -106,15 +101,25 @@ public function getPlugin() {
}
/**
* {@inheritdoc}
* Encapsulates the creation of the block's PluginBag.
*
* @return \Drupal\Component\Plugin\PluginBag
* The block's plugin bag.
*/
public function getPluginBag() {
protected function getPluginBag() {
if (!$this->pluginBag) {
$this->pluginBag = new BlockPluginBag(\Drupal::service('plugin.manager.block'), $this->plugin, $this->get('settings'), $this->id());
}
return $this->pluginBag;
}
/**
* {@inheritdoc}
*/
public function getPluginBags() {
return array('settings' => $this->getPluginBag());
}
/**
* Overrides \Drupal\Core\Entity\Entity::label();
*/
......
......@@ -88,10 +88,10 @@ public function setUp() {
*/
public function testCalculateDependencies() {
$values = array('theme' => 'stark');
// Mock the entity under test so that we can mock getPluginBag().
// Mock the entity under test so that we can mock getPluginBags().
$entity = $this->getMockBuilder('\Drupal\block\Entity\Block')
->setConstructorArgs(array($values, $this->entityTypeId))
->setMethods(array('getPluginBag'))
->setMethods(array('getPluginBags'))
->getMock();
// Create a configurable plugin that would add a dependency.
$instance_id = $this->randomName();
......@@ -110,8 +110,8 @@ public function testCalculateDependencies() {
// Return the mocked plugin bag.
$entity->expects($this->once())
->method('getPluginBag')
->will($this->returnValue($plugin_bag));
->method('getPluginBags')
->will($this->returnValue(array($plugin_bag)));
$dependencies = $entity->calculateDependencies();
$this->assertContains('test', $dependencies['module']);
......
......@@ -95,7 +95,7 @@ public function calculateDependencies() {
parent::calculateDependencies();
// Create a dependency on the associated FilterFormat.
$this->addDependency('entity', $this->getFilterFormat()->getConfigDependencyName());
// @todo use EntityWithPluginBagInterface so configuration between config
// @todo use EntityWithPluginBagsInterface so configuration between config
// entity and dependency on provider is managed automatically.
$definition = $this->editorPluginManager()->createInstance($this->editor)->getPluginDefinition();
$this->addDependency('module', $definition['provider']);
......
......@@ -8,7 +8,7 @@
namespace Drupal\filter\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Config\Entity\EntityWithPluginBagInterface;
use Drupal\Core\Entity\EntityWithPluginBagsInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\filter\FilterFormatInterface;
use Drupal\filter\FilterBag;
......@@ -43,7 +43,7 @@
* }
* )
*/
class FilterFormat extends ConfigEntityBase implements FilterFormatInterface, EntityWithPluginBagInterface {
class FilterFormat extends ConfigEntityBase implements FilterFormatInterface, EntityWithPluginBagsInterface {
/**
* Unique machine name of the format.
......@@ -125,11 +125,6 @@ class FilterFormat extends ConfigEntityBase implements FilterFormatInterface, En
*/
protected $filterBag;
/**
* {@inheritdoc}
*/
protected $pluginConfigKey = 'filters';
/**
* {@inheritdoc}
*/
......@@ -141,22 +136,21 @@ public function id() {
* {@inheritdoc}
*/
public function filters($instance_id = NULL) {
$filter_bag = $this->getPluginBag();
if (!isset($this->filterBag)) {
$this->filterBag = new FilterBag(\Drupal::service('plugin.manager.filter'), $this->filters);
$this->filterBag->sort();
}
if (isset($instance_id)) {
return $filter_bag->get($instance_id);
return $this->filterBag->get($instance_id);
}
return $filter_bag;
return $this->filterBag;
}
/**
* {@inheritdoc}
*/
public function getPluginBag() {
if (!isset($this->filterBag)) {
$this->filterBag = new FilterBag(\Drupal::service('plugin.manager.filter'), $this->filters);
$this->filterBag->sort();
}
return $this->filterBag;
public function getPluginBags() {
return array('filters' => $this->filters());
}
/**
......
......@@ -9,8 +9,8 @@
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Config\Entity\EntityWithPluginBagInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityWithPluginBagsInterface;
use Drupal\Core\Routing\RequestHelper;
use Drupal\image\ImageEffectBag;
use Drupal\image\ImageEffectInterface;
......@@ -47,7 +47,7 @@
* }
* )
*/
class ImageStyle extends ConfigEntityBase implements ImageStyleInterface, EntityWithPluginBagInterface {
class ImageStyle extends ConfigEntityBase implements ImageStyleInterface, EntityWithPluginBagsInterface {
/**
* The name of the image style to use as replacement upon delete.
......@@ -84,11 +84,6 @@ class ImageStyle extends ConfigEntityBase implements ImageStyleInterface, Entity
*/
protected $effectsBag;
/**
* {@inheritdoc}
*/
protected $pluginConfigKey = 'effects';
/**
* Overrides Drupal\Core\Entity\Entity::id().
*/
......@@ -347,8 +342,8 @@ public function getEffects() {
/**
* {@inheritdoc}
*/
public function getPluginBag() {
return $this->getEffects();
public function getPluginBags() {
return array('effects' => $this->getEffects());
}
/**
......
......@@ -9,9 +9,9 @@
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Config\Entity\EntityWithPluginBagInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Core\Entity\EntityWithPluginBagsInterface;
use Drupal\search\Plugin\SearchIndexingInterface;
use Drupal\search\Plugin\SearchPluginBag;
use Drupal\search\SearchPageInterface;
......@@ -50,7 +50,7 @@
* }
* )
*/
class SearchPage extends ConfigEntityBase implements SearchPageInterface, EntityWithPluginBagInterface {
class SearchPage extends ConfigEntityBase implements SearchPageInterface, EntityWithPluginBagsInterface {
/**
* The name (plugin ID) of the search page entity.
......@@ -103,11 +103,6 @@ class SearchPage extends ConfigEntityBase implements SearchPageInterface, Entity
*/
protected $pluginBag;
/**
* {@inheritdoc}
*/
protected $pluginConfigKey = 'configuration';
/**
* {@inheritdoc}
*/
......@@ -116,15 +111,25 @@ public function getPlugin() {
}
/**
* {@inheritdoc}
* Encapsulates the creation of the search page's PluginBag.
*
* @return \Drupal\Component\Plugin\PluginBag
* The search page's plugin bag.
*/
public function getPluginBag() {
protected function getPluginBag() {
if (!$this->pluginBag) {
$this->pluginBag = new SearchPluginBag($this->searchPluginManager(), $this->plugin, $this->configuration, $this->id());
}
return $this->pluginBag;
}
/**
* {@inheritdoc}
*/
public function getPluginBags() {
return array('configuration' => $this->getPluginBag());
}
/**
* {@inheritdoc}
*/
......
......@@ -9,7 +9,7 @@
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Config\Entity\EntityWithPluginBagInterface;
use Drupal\Core\Entity\EntityWithPluginBagsInterface;
use Drupal\system\ActionConfigEntityInterface;
use Drupal\Core\Action\ActionBag;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
......@@ -27,7 +27,7 @@
* }
* )
*/
class Action extends ConfigEntityBase implements ActionConfigEntityInterface, EntityWithPluginBagInterface {
class Action extends ConfigEntityBase implements ActionConfigEntityInterface, EntityWithPluginBagsInterface {
/**
* The name (plugin ID) of the action.
......@@ -72,20 +72,25 @@ class Action extends ConfigEntityBase implements ActionConfigEntityInterface, En
protected $pluginBag;
/**
* {@inheritdoc}
*/
protected $pluginConfigKey = 'configuration';
/**
* {@inheritdoc}
* Encapsulates the creation of the action's PluginBag.
*
* @return \Drupal\Component\Plugin\PluginBag
* The action's plugin bag.
*/
public function getPluginBag() {
protected function getPluginBag() {
if (!$this->pluginBag) {
$this->pluginBag = new ActionBag(\Drupal::service('plugin.manager.action'), $this->plugin, $this->configuration);
}
return $this->pluginBag;
}
/**
* {@inheritdoc}
*/
public function getPluginBags() {
return array('configuration' => $this->getPluginBag());
}
/**
* {@inheritdoc}
*/
......
......@@ -7,7 +7,7 @@
namespace Drupal\system\Tests\Entity;
use Drupal\Core\Config\Entity\EntityWithPluginBagInterface;
use Drupal\Core\Entity\EntityWithPluginBagsInterface;
use Drupal\simpletest\WebTestBase;
/**
......@@ -101,7 +101,7 @@ protected function doFilterFormatUpdate() {
/** @var $entity \Drupal\filter\Entity\FilterFormat */
$entity = entity_load('filter_format', 'plain_text');
$plugin_bag = $entity->getPluginBag();
$plugin_bag = $entity->getPluginBags()['filters'];
$filters = $entity->get('filters');
$this->assertIdentical(72, $filters['filter_url']['settings']['filter_url_length']);
......@@ -113,7 +113,7 @@ protected function doFilterFormatUpdate() {
$this->assertIdentical($filters, $plugin_bag->getConfiguration());
$filters['filter_url']['settings']['filter_url_length'] = -100;
$entity->getPluginBag()->setConfiguration($filters);
$entity->getPluginBags()['filters']->setConfiguration($filters);
$entity->save();
$this->assertIdentical($filters, $entity->get('filters'));
$this->assertIdentical($filters, $plugin_bag->getConfiguration());
......@@ -133,7 +133,7 @@ protected function doImageStyleUpdate() {
/** @var $entity \Drupal\image\Entity\ImageStyle */
$entity = entity_load('image_style', 'thumbnail');
$plugin_bag = $entity->getPluginBag();
$plugin_bag = $entity->getPluginBags()['effects'];
$effects = $entity->get('effects');
$effect_id = key($effects);
......@@ -147,7 +147,7 @@ protected function doImageStyleUpdate() {
$this->assertIdentical($effects, $plugin_bag->getConfiguration());
$effects[$effect_id]['data']['height'] = -50;
$entity->getPluginBag()->setConfiguration($effects);
$entity->getPluginBags()['effects']->setConfiguration($effects);
$entity->save();
// Ensure the entity and plugin have the correct configuration.
$this->assertIdentical($effects, $entity->get('effects'));
......@@ -184,7 +184,7 @@ protected function doSearchPageUpdate() {
/**
* Tests that a single set of plugin config stays in sync.
*
* @param \Drupal\Core\Config\Entity\EntityWithPluginBagInterface $entity
* @param \Drupal\Core\Entity\EntityWithPluginBagsInterface $entity
* The entity.
* @param string $config_key
* Where the plugin config is stored.
......@@ -193,8 +193,8 @@ protected function doSearchPageUpdate() {
* @param mixed $expected
* The expected default value of the plugin config setting.
*/
protected function checkSinglePluginConfigSync(EntityWithPluginBagInterface $entity, $config_key, $setting_key, $expected) {
$plugin_bag = $entity->getPluginBag();
protected function checkSinglePluginConfigSync(EntityWithPluginBagsInterface $entity, $config_key, $setting_key, $expected) {
$plugin_bag = $entity->getPluginBags()[$config_key];
$settings = $entity->get($config_key);
// Ensure the default config exists.
......
......@@ -210,13 +210,13 @@ public function testAddDependency() {
/**
* @covers ::calculateDependencies
*
* @dataProvider providerCalculateDependenciesWithPluginBag
* @dataProvider providerCalculateDependenciesWithPluginBags
*/
public function testCalculateDependenciesWithPluginBag($definition, $expected_dependencies) {
public function testCalculateDependenciesWithPluginBags($definition, $expected_dependencies) {
$values = array();
$this->entity = $this->getMockBuilder('\Drupal\Tests\Core\Config\Entity\Fixtures\ConfigEntityBaseWithPluginBag')
$this->entity = $this->getMockBuilder('\Drupal\Tests\Core\Config\Entity\Fixtures\ConfigEntityBaseWithPluginBags')
->setConstructorArgs(array($values, $this->entityTypeId))
->setMethods(array('getPluginBag'))
->setMethods(array('getPluginBags'))
->getMock();
// Create a configurable plugin that would add a dependency.
......@@ -236,18 +236,18 @@ public function testCalculateDependenciesWithPluginBag($definition, $expected_de
// Return the mocked plugin bag.
$this->entity->expects($this->once())
->method('getPluginBag')
->will($this->returnValue($pluginBag));
->method('getPluginBags')
->will($this->returnValue(array($pluginBag)));
$this->assertEquals($expected_dependencies, $this->entity->calculateDependencies());
}
/**
* Data provider for testCalculateDependenciesWithPluginBag.
* Data provider for testCalculateDependenciesWithPluginBags.
*
* @return array
*/
public function providerCalculateDependenciesWithPluginBag() {
public function providerCalculateDependenciesWithPluginBags() {
// Start with 'a' so that order of the dependency array is fixed.
$instance_dependency_1 = 'a' . $this->randomName(10);
$instance_dependency_2 = 'a' . $this->randomName(11);
......
......@@ -2,19 +2,19 @@
/**
* @file
* Contains \Drupal\Tests\Core\Config\Entity\Fixtures\ConfigEntityBaseWithPluginBag.
* Contains \Drupal\Tests\Core\Config\Entity\Fixtures\ConfigEntityBaseWithPluginBags.
*/
namespace Drupal\Tests\Core\Config\Entity\Fixtures;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Config\Entity\EntityWithPluginBagInterface;
use Drupal\Core\Entity\EntityWithPluginBagsInterface;
/**
* Enables testing of dependency calculation.
*
* @see \Drupal\Tests\Core\Config\Entity\ConfigEntityBaseUnitTest::testCalculateDependenciesWithPluginBag()
* @see \Drupal\Tests\Core\Config\Entity\ConfigEntityBaseUnitTest::testCalculateDependenciesWithPluginBags()
* @see \Drupal\Core\Config\Entity\ConfigEntityBase::calculateDependencies()
*/
abstract class ConfigEntityBaseWithPluginBag extends ConfigEntityBase implements EntityWithPluginBagInterface {
abstract class ConfigEntityBaseWithPluginBags extends ConfigEntityBase implements EntityWithPluginBagsInterface {
}
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