Commit 85f243e2 authored by webchick's avatar webchick
Browse files

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 @@ ...@@ -35,7 +35,7 @@
* \Drupal\Core\Config\Entity\ConfigEntityBase::calculateDependencies() which * \Drupal\Core\Config\Entity\ConfigEntityBase::calculateDependencies() which
* resets the dependencies and provides an implementation to determine the * resets the dependencies and provides an implementation to determine the
* plugin providers for configuration entities that implement * 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 * The configuration manager service provides methods to find dependencies for
* a specified module, theme or configuration entity. * a specified module, theme or configuration entity.
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
use Drupal\Core\Entity\Entity; use Drupal\Core\Entity\Entity;
use Drupal\Core\Config\ConfigDuplicateUUIDException; use Drupal\Core\Config\ConfigDuplicateUUIDException;
use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityWithPluginBagsInterface;
use Drupal\Core\Language\Language; use Drupal\Core\Language\Language;
use Drupal\Core\Plugin\PluginDependencyTrait; use Drupal\Core\Plugin\PluginDependencyTrait;
...@@ -136,10 +137,11 @@ public function get($property_name) { ...@@ -136,10 +137,11 @@ public function get($property_name) {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function set($property_name, $value) { public function set($property_name, $value) {
if ($this instanceof EntityWithPluginBagInterface) { if ($this instanceof EntityWithPluginBagsInterface) {
if ($property_name == $this->pluginConfigKey) { $plugin_bags = $this->getPluginBags();
if (isset($plugin_bags[$property_name])) {
// If external code updates the settings, pass it along to the plugin. // 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() { ...@@ -257,11 +259,12 @@ public function toArray() {
public function preSave(EntityStorageInterface $storage) { public function preSave(EntityStorageInterface $storage) {
parent::preSave($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 // Any changes to the plugin configuration must be saved to the entity's
// copy as well. // copy as well.
$plugin_bag = $this->getPluginBag(); foreach ($this->getPluginBags() as $plugin_config_key => $plugin_bag) {
$this->set($this->pluginConfigKey, $plugin_bag->getConfiguration()); $this->set($plugin_config_key, $plugin_bag->getConfiguration());
}
} }
// Ensure this entity's UUID does not exist with a different ID, regardless // Ensure this entity's UUID does not exist with a different ID, regardless
...@@ -297,14 +300,13 @@ public function calculateDependencies() { ...@@ -297,14 +300,13 @@ public function calculateDependencies() {
// Dependencies should be recalculated on every save. This ensures stale // Dependencies should be recalculated on every save. This ensures stale
// dependencies are never saved. // dependencies are never saved.
$this->dependencies = array(); $this->dependencies = array();
// @todo When \Drupal\Core\Config\Entity\EntityWithPluginBagInterface moves if ($this instanceof EntityWithPluginBagsInterface) {
// to a trait, switch to class_uses() instead.
if ($this instanceof EntityWithPluginBagInterface) {
// Configuration entities need to depend on the providers of any plugins // Configuration entities need to depend on the providers of any plugins
// that they store the configuration for. // that they store the configuration for.
$plugin_bag = $this->getPluginBag(); foreach ($this->getPluginBags() as $plugin_bag) {
foreach ($plugin_bag as $instance) { foreach ($plugin_bag as $instance) {
$this->calculatePluginDependencies($instance); $this->calculatePluginDependencies($instance);
}
} }
} }
return $this->dependencies; 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 @@ ...@@ -12,7 +12,7 @@
use Drupal\block\BlockPluginBag; use Drupal\block\BlockPluginBag;
use Drupal\block\BlockInterface; use Drupal\block\BlockInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface; use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Config\Entity\EntityWithPluginBagInterface; use Drupal\Core\Entity\EntityWithPluginBagsInterface;
use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityStorageInterface;
/** /**
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
* } * }
* ) * )
*/ */
class Block extends ConfigEntityBase implements BlockInterface, EntityWithPluginBagInterface { class Block extends ConfigEntityBase implements BlockInterface, EntityWithPluginBagsInterface {
/** /**
* The ID of the block. * The ID of the block.
...@@ -86,11 +86,6 @@ class Block extends ConfigEntityBase implements BlockInterface, EntityWithPlugin ...@@ -86,11 +86,6 @@ class Block extends ConfigEntityBase implements BlockInterface, EntityWithPlugin
*/ */
protected $pluginBag; protected $pluginBag;
/**
* {@inheritdoc}
*/
protected $pluginConfigKey = 'settings';
/** /**
* The visibility settings. * The visibility settings.
* *
...@@ -106,15 +101,25 @@ public function getPlugin() { ...@@ -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) { if (!$this->pluginBag) {
$this->pluginBag = new BlockPluginBag(\Drupal::service('plugin.manager.block'), $this->plugin, $this->get('settings'), $this->id()); $this->pluginBag = new BlockPluginBag(\Drupal::service('plugin.manager.block'), $this->plugin, $this->get('settings'), $this->id());
} }
return $this->pluginBag; return $this->pluginBag;
} }
/**
* {@inheritdoc}
*/
public function getPluginBags() {
return array('settings' => $this->getPluginBag());
}
/** /**
* Overrides \Drupal\Core\Entity\Entity::label(); * Overrides \Drupal\Core\Entity\Entity::label();
*/ */
......
...@@ -88,10 +88,10 @@ public function setUp() { ...@@ -88,10 +88,10 @@ public function setUp() {
*/ */
public function testCalculateDependencies() { public function testCalculateDependencies() {
$values = array('theme' => 'stark'); $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') $entity = $this->getMockBuilder('\Drupal\block\Entity\Block')
->setConstructorArgs(array($values, $this->entityTypeId)) ->setConstructorArgs(array($values, $this->entityTypeId))
->setMethods(array('getPluginBag')) ->setMethods(array('getPluginBags'))
->getMock(); ->getMock();
// Create a configurable plugin that would add a dependency. // Create a configurable plugin that would add a dependency.
$instance_id = $this->randomName(); $instance_id = $this->randomName();
...@@ -110,8 +110,8 @@ public function testCalculateDependencies() { ...@@ -110,8 +110,8 @@ public function testCalculateDependencies() {
// Return the mocked plugin bag. // Return the mocked plugin bag.
$entity->expects($this->once()) $entity->expects($this->once())
->method('getPluginBag') ->method('getPluginBags')
->will($this->returnValue($plugin_bag)); ->will($this->returnValue(array($plugin_bag)));
$dependencies = $entity->calculateDependencies(); $dependencies = $entity->calculateDependencies();
$this->assertContains('test', $dependencies['module']); $this->assertContains('test', $dependencies['module']);
......
...@@ -95,7 +95,7 @@ public function calculateDependencies() { ...@@ -95,7 +95,7 @@ public function calculateDependencies() {
parent::calculateDependencies(); parent::calculateDependencies();
// Create a dependency on the associated FilterFormat. // Create a dependency on the associated FilterFormat.
$this->addDependency('entity', $this->getFilterFormat()->getConfigDependencyName()); $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. // entity and dependency on provider is managed automatically.
$definition = $this->editorPluginManager()->createInstance($this->editor)->getPluginDefinition(); $definition = $this->editorPluginManager()->createInstance($this->editor)->getPluginDefinition();
$this->addDependency('module', $definition['provider']); $this->addDependency('module', $definition['provider']);
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
namespace Drupal\filter\Entity; namespace Drupal\filter\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Config\Entity\EntityWithPluginBagInterface; use Drupal\Core\Entity\EntityWithPluginBagsInterface;
use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\filter\FilterFormatInterface; use Drupal\filter\FilterFormatInterface;
use Drupal\filter\FilterBag; use Drupal\filter\FilterBag;
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
* } * }
* ) * )
*/ */
class FilterFormat extends ConfigEntityBase implements FilterFormatInterface, EntityWithPluginBagInterface { class FilterFormat extends ConfigEntityBase implements FilterFormatInterface, EntityWithPluginBagsInterface {
/** /**
* Unique machine name of the format. * Unique machine name of the format.
...@@ -125,11 +125,6 @@ class FilterFormat extends ConfigEntityBase implements FilterFormatInterface, En ...@@ -125,11 +125,6 @@ class FilterFormat extends ConfigEntityBase implements FilterFormatInterface, En
*/ */
protected $filterBag; protected $filterBag;
/**
* {@inheritdoc}
*/
protected $pluginConfigKey = 'filters';
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -141,22 +136,21 @@ public function id() { ...@@ -141,22 +136,21 @@ public function id() {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function filters($instance_id = NULL) { 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)) { if (isset($instance_id)) {
return $filter_bag->get($instance_id); return $this->filterBag->get($instance_id);
} }
return $filter_bag; return $this->filterBag;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPluginBag() { public function getPluginBags() {
if (!isset($this->filterBag)) { return array('filters' => $this->filters());
$this->filterBag = new FilterBag(\Drupal::service('plugin.manager.filter'), $this->filters);
$this->filterBag->sort();
}
return $this->filterBag;
} }
/** /**
......
...@@ -9,8 +9,8 @@ ...@@ -9,8 +9,8 @@
use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Config\Entity\EntityWithPluginBagInterface;
use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityWithPluginBagsInterface;
use Drupal\Core\Routing\RequestHelper; use Drupal\Core\Routing\RequestHelper;
use Drupal\image\ImageEffectBag; use Drupal\image\ImageEffectBag;
use Drupal\image\ImageEffectInterface; use Drupal\image\ImageEffectInterface;
...@@ -47,7 +47,7 @@ ...@@ -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. * The name of the image style to use as replacement upon delete.
...@@ -84,11 +84,6 @@ class ImageStyle extends ConfigEntityBase implements ImageStyleInterface, Entity ...@@ -84,11 +84,6 @@ class ImageStyle extends ConfigEntityBase implements ImageStyleInterface, Entity
*/ */
protected $effectsBag; protected $effectsBag;
/**
* {@inheritdoc}
*/
protected $pluginConfigKey = 'effects';
/** /**
* Overrides Drupal\Core\Entity\Entity::id(). * Overrides Drupal\Core\Entity\Entity::id().
*/ */
...@@ -347,8 +342,8 @@ public function getEffects() { ...@@ -347,8 +342,8 @@ public function getEffects() {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPluginBag() { public function getPluginBags() {
return $this->getEffects(); return array('effects' => $this->getEffects());
} }
/** /**
......
...@@ -9,9 +9,9 @@ ...@@ -9,9 +9,9 @@
use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Config\Entity\ConfigEntityInterface; use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Config\Entity\EntityWithPluginBagInterface;
use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Component\Plugin\ConfigurablePluginInterface; use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Core\Entity\EntityWithPluginBagsInterface;
use Drupal\search\Plugin\SearchIndexingInterface; use Drupal\search\Plugin\SearchIndexingInterface;
use Drupal\search\Plugin\SearchPluginBag; use Drupal\search\Plugin\SearchPluginBag;
use Drupal\search\SearchPageInterface; use Drupal\search\SearchPageInterface;
...@@ -50,7 +50,7 @@ ...@@ -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. * The name (plugin ID) of the search page entity.
...@@ -103,11 +103,6 @@ class SearchPage extends ConfigEntityBase implements SearchPageInterface, Entity ...@@ -103,11 +103,6 @@ class SearchPage extends ConfigEntityBase implements SearchPageInterface, Entity
*/ */
protected $pluginBag; protected $pluginBag;
/**
* {@inheritdoc}
*/
protected $pluginConfigKey = 'configuration';
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -116,15 +111,25 @@ public function getPlugin() { ...@@ -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) { if (!$this->pluginBag) {
$this->pluginBag = new SearchPluginBag($this->searchPluginManager(), $this->plugin, $this->configuration, $this->id()); $this->pluginBag = new SearchPluginBag($this->searchPluginManager(), $this->plugin, $this->configuration, $this->id());
} }
return $this->pluginBag; return $this->pluginBag;
} }
/**
* {@inheritdoc}
*/
public function getPluginBags() {
return array('configuration' => $this->getPluginBag());
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Config\Entity\ConfigEntityInterface; use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Config\Entity\EntityWithPluginBagInterface; use Drupal\Core\Entity\EntityWithPluginBagsInterface;
use Drupal\system\ActionConfigEntityInterface; use Drupal\system\ActionConfigEntityInterface;
use Drupal\Core\Action\ActionBag; use Drupal\Core\Action\ActionBag;
use Drupal\Component\Plugin\ConfigurablePluginInterface; use Drupal\Component\Plugin\ConfigurablePluginInterface;
...@@ -27,7 +27,7 @@ ...@@ -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. * The name (plugin ID) of the action.
...@@ -72,20 +72,25 @@ class Action extends ConfigEntityBase implements ActionConfigEntityInterface, En ...@@ -72,20 +72,25 @@ class Action extends ConfigEntityBase implements ActionConfigEntityInterface, En
protected $pluginBag; protected $pluginBag;
/** /**
* {@inheritdoc} * Encapsulates the creation of the action's PluginBag.
*/ *
protected $pluginConfigKey = 'configuration'; * @return \Drupal\Component\Plugin\PluginBag
* The action's plugin bag.
/**
* {@inheritdoc}
*/ */
public function getPluginBag() { protected function getPluginBag() {
if (!$this->pluginBag) { if (!$this->pluginBag) {
$this->pluginBag = new ActionBag(\Drupal::service('plugin.manager.action'), $this->plugin, $this->configuration); $this->pluginBag = new ActionBag(\Drupal::service('plugin.manager.action'), $this->plugin, $this->configuration);
} }
return $this->pluginBag; return $this->pluginBag;
} }
/**
* {@inheritdoc}
*/
public function getPluginBags() {
return array('configuration' => $this->getPluginBag());
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
namespace Drupal\system\Tests\Entity; namespace Drupal\system\Tests\Entity;
use Drupal\Core\Config\Entity\EntityWithPluginBagInterface; use Drupal\Core\Entity\EntityWithPluginBagsInterface;
use Drupal\simpletest\WebTestBase; use Drupal\simpletest\WebTestBase;
/** /**
...@@ -101,7 +101,7 @@ protected function doFilterFormatUpdate() { ...@@ -101,7 +101,7 @@ protected function doFilterFormatUpdate() {
/** @var $entity \Drupal\filter\Entity\FilterFormat */ /** @var $entity \Drupal\filter\Entity\FilterFormat */
$entity = entity_load('filter_format', 'plain_text'); $entity = entity_load('filter_format', 'plain_text');
$plugin_bag = $entity->getPluginBag(); $plugin_bag = $entity->getPluginBags()['filters'];