Commit 3c24816c authored by alexpott's avatar alexpott

Issue #2650588 by tim.plunkett, Wim Leers, alexpott, dawehner: Entities with...

Issue #2650588 by tim.plunkett, Wim Leers, alexpott, dawehner: Entities with plugin collections should be updated before serialization
parent e3da828f
......@@ -352,6 +352,32 @@ public function preSave(EntityStorageInterface $storage) {
}
}
/**
* {@inheritdoc}
*/
public function __sleep() {
$keys_to_unset = [];
if ($this instanceof EntityWithPluginCollectionInterface) {
$vars = get_object_vars($this);
foreach ($this->getPluginCollections() as $plugin_config_key => $plugin_collection) {
// Save any changes to the plugin configuration to the entity.
$this->set($plugin_config_key, $plugin_collection->getConfiguration());
// If the plugin collections are stored as properties on the entity,
// mark them to be unset.
$keys_to_unset += array_filter($vars, function ($value) use ($plugin_collection) {
return $plugin_collection === $value;
});
}
}
$vars = parent::__sleep();
if (!empty($keys_to_unset)) {
$vars = array_diff($vars, array_keys($keys_to_unset));
}
return $vars;
}
/**
* {@inheritdoc}
*/
......
......@@ -7,8 +7,11 @@
namespace Drupal\Tests\Core\Config\Entity;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Language\Language;
use Drupal\Core\Plugin\DefaultLazyPluginCollection;
use Drupal\Tests\Core\Config\Entity\Fixtures\ConfigEntityBaseWithPluginCollections;
use Drupal\Tests\Core\Plugin\Fixtures\TestConfigurablePlugin;
use Drupal\Tests\UnitTestCase;
......@@ -307,6 +310,36 @@ public function testCalculateDependenciesWithThirdPartySettings() {
$this->assertEquals(array('test_provider'), $this->entity->calculateDependencies()->getDependencies()['module']);
}
/**
* @covers ::__sleep
*/
public function testSleepWithPluginCollections() {
$instance_id = 'the_instance_id';
$instance = new TestConfigurablePlugin([], $instance_id, []);
$plugin_manager = $this->prophesize(PluginManagerInterface::class);
$plugin_manager->createInstance($instance_id, ['id' => $instance_id])->willReturn($instance);
$entity_values = ['the_plugin_collection_config' => [$instance_id => ['foo' => 'original_value']]];
$entity = new TestConfigEntityWithPluginCollections($entity_values, $this->entityTypeId);
$entity->setPluginManager($plugin_manager->reveal());
// After creating the entity, change the plugin configuration.
$instance->setConfiguration(['foo' => 'new_value']);
// After changing the plugin configuration, the entity still has the
// original value.
$expected_plugin_config = [$instance_id => ['foo' => 'original_value']];
$this->assertSame($expected_plugin_config, $entity->get('the_plugin_collection_config'));
// Ensure the plugin collection is not stored.
$this->assertNotContains('pluginCollection', $entity->__sleep());
$expected_plugin_config = [$instance_id => ['foo' => 'new_value']];
// Ensure the updated values are stored in the entity.
$this->assertSame($expected_plugin_config, $entity->get('the_plugin_collection_config'));
}
/**
* @covers ::setOriginalId
* @covers ::getOriginalId
......@@ -579,3 +612,20 @@ public function testThirdPartySettings() {
}
}
class TestConfigEntityWithPluginCollections extends ConfigEntityBaseWithPluginCollections {
protected $pluginCollection;
public function setPluginManager(PluginManagerInterface $plugin_manager) {
$this->pluginCollection = new DefaultLazyPluginCollection($plugin_manager, ['the_instance_id' => ['id' => 'the_instance_id']]);
}
/**
* {@inheritdoc}
*/
public function getPluginCollections() {
return ['the_plugin_collection_config' => $this->pluginCollection];
}
}
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