Skip to content

Commit b8f4f50

Browse files
committed
Issue #3026043 by Berdir: ConfigEntityBase::__sleep() serializes plugin instances if they were not previously initialized
(cherry picked from commit 5fbbbde)
1 parent bc1923b commit b8f4f50

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

lib/Drupal/Core/Config/Entity/ConfigEntityBase.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,11 @@ public function preSave(EntityStorageInterface $storage) {
348348
public function __sleep() {
349349
$keys_to_unset = [];
350350
if ($this instanceof EntityWithPluginCollectionInterface) {
351+
// Get the plugin collections first, so that the properties are
352+
// initialized in $vars and can be found later.
353+
$plugin_collections = $this->getPluginCollections();
351354
$vars = get_object_vars($this);
352-
foreach ($this->getPluginCollections() as $plugin_config_key => $plugin_collection) {
355+
foreach ($plugin_collections as $plugin_config_key => $plugin_collection) {
353356
// Save any changes to the plugin configuration to the entity.
354357
$this->set($plugin_config_key, $plugin_collection->getConfiguration());
355358
// If the plugin collections are stored as properties on the entity,

modules/filter/tests/src/Kernel/FilterAPITest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,4 +509,16 @@ public function testDependencyRemoval() {
509509
$this->assertFalse(isset($filters['filter_test_restrict_tags_and_attributes']), 'The filter plugin filter_test_restrict_tags_and_attributes is not configured by the filtered_html filter format.');
510510
}
511511

512+
/**
513+
* Tests that format entities are serialized without their plugin collection.
514+
*/
515+
public function testSleep() {
516+
$filter_format = FilterFormat::load('filtered_html');
517+
518+
$this->assertNull($filter_format->get('filterCollection'));
519+
$vars = $filter_format->__sleep();
520+
$this->assertContains('filters', $vars);
521+
$this->assertNotContains('filterCollection', $vars);
522+
}
523+
512524
}

tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,12 @@ public function testSleepWithPluginCollections() {
322322
$plugin_manager = $this->prophesize(PluginManagerInterface::class);
323323
$plugin_manager->createInstance($instance_id, ['id' => $instance_id])->willReturn($instance);
324324

325+
// Also set up a container with the plugin manager so that we can assert
326+
// that the plugin manager itself is also not serialized.
327+
$container = new ContainerBuilder();
328+
$container->set('plugin.manager.foo', $plugin_manager);
329+
\Drupal::setContainer($container);
330+
325331
$entity_values = ['the_plugin_collection_config' => [$instance_id => ['foo' => 'original_value']]];
326332
$entity = new TestConfigEntityWithPluginCollections($entity_values, $this->entityTypeId);
327333
$entity->setPluginManager($plugin_manager->reveal());
@@ -334,8 +340,11 @@ public function testSleepWithPluginCollections() {
334340
$expected_plugin_config = [$instance_id => ['foo' => 'original_value']];
335341
$this->assertSame($expected_plugin_config, $entity->get('the_plugin_collection_config'));
336342

337-
// Ensure the plugin collection is not stored.
338-
$this->assertNotContains('pluginCollection', $entity->__sleep());
343+
// Ensure the plugin collection and manager is not stored.
344+
$vars = $entity->__sleep();
345+
$this->assertNotContains('pluginCollection', $vars);
346+
$this->assertNotContains('pluginManager', $vars);
347+
$this->assertSame(['pluginManager' => 'plugin.manager.foo'], $entity->get('_serviceIds'));
339348

340349
$expected_plugin_config = [$instance_id => ['foo' => 'new_value']];
341350
// Ensure the updated values are stored in the entity.
@@ -603,14 +612,19 @@ class TestConfigEntityWithPluginCollections extends ConfigEntityBaseWithPluginCo
603612

604613
protected $pluginCollection;
605614

615+
protected $pluginManager;
616+
606617
public function setPluginManager(PluginManagerInterface $plugin_manager) {
607-
$this->pluginCollection = new DefaultLazyPluginCollection($plugin_manager, ['the_instance_id' => ['id' => 'the_instance_id']]);
618+
$this->pluginManager = $plugin_manager;
608619
}
609620

610621
/**
611622
* {@inheritdoc}
612623
*/
613624
public function getPluginCollections() {
625+
if (!$this->pluginCollection) {
626+
$this->pluginCollection = new DefaultLazyPluginCollection($this->pluginManager, ['the_instance_id' => ['id' => 'the_instance_id']]);
627+
}
614628
return ['the_plugin_collection_config' => $this->pluginCollection];
615629
}
616630

0 commit comments

Comments
 (0)