Commit 29b27d5d authored by andrewbelcher's avatar andrewbelcher Committed by borisson_

Issue #2880411 by andrewbelcher: Facets added via config don't clear the block cache

parent 34d43b40
......@@ -3,6 +3,7 @@
namespace Drupal\facets\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\facets\Exception\Exception;
use Drupal\facets\Exception\InvalidProcessorException;
use Drupal\facets\FacetInterface;
......@@ -901,4 +902,40 @@ class Facet extends ConfigEntityBase implements FacetInterface {
return $this;
}
/**
* {@inheritdoc}
*/
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
if (!$update) {
self::clearBlockCache();
}
}
/**
* {@inheritdoc}
*/
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
self::clearBlockCache();
}
/**
* Clear the block cache.
*
* This includes resetting the shared plugin block manager as this can result
* in the block definition cache being rebuilt in the same request with stale
* static caches in the deriver.
*/
protected static function clearBlockCache() {
$container = \Drupal::getContainer();
// If the block manager has already been loaded, we may have stale static
// caches in the facet deriver, so lets clear it out.
$container->set('plugin.manager.block', NULL);
// Now rebuild the cache to force a fresh set of data.
$container->get('plugin.manager.block')->clearCachedDefinitions();
}
}
......@@ -2,7 +2,6 @@
namespace Drupal\facets\Form;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
......@@ -48,13 +47,6 @@ class FacetSettingsForm extends EntityForm {
*/
protected $moduleHandler;
/**
* The block manager.
*
* @var \Drupal\Core\Block\BlockManagerInterface
*/
protected $blockManager;
/**
* The url generator.
*
......@@ -73,17 +65,14 @@ class FacetSettingsForm extends EntityForm {
* The plugin manager for processors.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Block\BlockManagerInterface $block_manager
* The block manager.
* @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
* The url generator.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, FacetSourcePluginManager $facet_source_plugin_manager, ProcessorPluginManager $processor_plugin_manager, ModuleHandlerInterface $module_handler, BlockManagerInterface $block_manager, UrlGeneratorInterface $url_generator) {
public function __construct(EntityTypeManagerInterface $entity_type_manager, FacetSourcePluginManager $facet_source_plugin_manager, ProcessorPluginManager $processor_plugin_manager, ModuleHandlerInterface $module_handler, UrlGeneratorInterface $url_generator) {
$this->facetStorage = $entity_type_manager->getStorage('facets_facet');
$this->facetSourcePluginManager = $facet_source_plugin_manager;
$this->processorPluginManager = $processor_plugin_manager;
$this->moduleHandler = $module_handler;
$this->blockManager = $block_manager;
$this->urlGenerator = $url_generator;
}
......@@ -96,7 +85,6 @@ class FacetSettingsForm extends EntityForm {
$container->get('plugin.manager.facets.facet_source'),
$container->get('plugin.manager.facets.processor'),
$container->get('module_handler'),
$container->get('plugin.manager.block'),
$container->get('url_generator')
);
}
......@@ -323,9 +311,6 @@ class FacetSettingsForm extends EntityForm {
drupal_set_message($this->t('Facet %name has been updated.', ['%name' => $facet->getName()]));
}
// Clear Drupal cache for blocks to reflect recent changes.
$this->blockManager->clearCachedDefinitions();
list($type,) = explode(':', $facet_source_id);
if ($type !== 'search_api') {
return $facet;
......
......@@ -2,6 +2,7 @@
namespace Drupal\Tests\facets\Kernel\Entity;
use Drupal\Core\Plugin\PluginBase;
use Drupal\facets\Entity\Facet;
use Drupal\facets\Exception\InvalidProcessorException;
use Drupal\facets\Hierarchy\HierarchyPluginManager;
......@@ -372,4 +373,41 @@ class FacetTest extends KernelTestBase {
$this->assertEquals(['type' => 'taxonomy', 'config' => []], $entity->getHierarchy());
}
/**
* Tests that the block caches are cleared from API calls.
*
* @covers ::postSave
* @covers ::postDelete
* @covers ::clearBlockCache
*/
public function testBlockCache() {
// Block processing requires the system module.
$this->enableModules(['system']);
// Create our facet.
$entity = Facet::create([
'id' => 'test_facet',
'name' => 'Test facet',
]);
$entity->setWidget('links');
$entity->setEmptyBehavior(['behavior' => 'none']);
$block_id = 'facet_block' . PluginBase::DERIVATIVE_SEPARATOR . $entity->id();
// Check we don't have a block yet.
$this->assertFalse($this->container->get('plugin.manager.block')->hasDefinition($block_id));
// Save our facet.
$entity->save();
// Check our block exists.
$this->assertTrue($this->container->get('plugin.manager.block')->hasDefinition($block_id));
// Delete our facet.
$entity->delete();
// Check our block exists.
$this->assertFalse($this->container->get('plugin.manager.block')->hasDefinition($block_id));
}
}
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