Skip to content
Snippets Groups Projects
Commit e5752f27 authored by Thomas Seidl's avatar Thomas Seidl
Browse files

Fixed "rendered in current request" detection code for block views to take...

Fixed "rendered in current request" detection code for block views to take current theme into account.
parent 60e61975
No related branches found
Tags 8.x-1.4
No related merge requests found
......@@ -2,6 +2,9 @@
namespace Drupal\search_api\Plugin\search_api\display;
use Drupal\Core\Theme\ThemeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Represents a Views block display.
*
......@@ -13,6 +16,45 @@ namespace Drupal\search_api\Plugin\search_api\display;
*/
class ViewsBlock extends ViewsDisplayBase {
/**
* The theme manager.
*
* @var \Drupal\Core\Theme\ThemeManagerInterface|null
*/
protected ?ThemeManagerInterface $themeManager;
/**
* @inheritDoc
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$plugin = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$plugin->setThemeManager($container->get('theme.manager'));
return $plugin;
}
/**
* Retrieves the theme manager.
*
* @return \Drupal\Core\Theme\ThemeManagerInterface
* The theme manager.
*/
public function getThemeManager(): ThemeManagerInterface {
return $this->themeManager ?: \Drupal::service('theme.manager');
}
/**
* Sets the theme manager.
*
* @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
* The new theme manager.
*
* @return $this
*/
public function setThemeManager(ThemeManagerInterface $theme_manager): self {
$this->themeManager = $theme_manager;
return $this;
}
/**
* {@inheritdoc}
*/
......@@ -22,7 +64,11 @@ class ViewsBlock extends ViewsDisplayBase {
$plugin_id = 'views_block:' . $this->pluginDefinition['view_id'] . '-' . $this->pluginDefinition['view_display'];
$blocks = $this->getEntityTypeManager()
->getStorage('block')
->loadByProperties(['plugin' => $plugin_id]);
->loadByProperties([
'plugin' => $plugin_id,
'theme' => $this->getThemeManager()->getActiveTheme()->getName(),
]);
/** @var \Drupal\block\BlockInterface $block */
foreach ($blocks as $block) {
if ($block->access('view')) {
return TRUE;
......
......@@ -2,34 +2,40 @@
namespace Drupal\Tests\search_api\Kernel\Views;
use Drupal\Core\Theme\ActiveTheme;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\search_api\Utility\Utility;
use Drupal\Tests\block\Traits\BlockCreationTrait;
/**
* Tests whether Views pages correctly create search display plugins.
* Tests whether search display plugins for Views work correctly.
*
* @group search_api
*/
class ViewsDisplayTest extends KernelTestBase {
use BlockCreationTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'block',
'entity_test',
'field',
'rest',
'search_api',
'search_api_db',
'search_api_test',
'search_api_test_db',
'search_api_test_example_content',
'search_api_test_views',
'search_api_test',
'user',
'serialization',
'system',
'entity_test',
'text',
'user',
'views',
'rest',
'serialization',
];
/**
......@@ -95,4 +101,36 @@ class ViewsDisplayTest extends KernelTestBase {
$this->assertContains('views.view.search_api_test_view', $dependencies['config']);
}
/**
* Tests whether block displays' "rendered in current request" work correctly.
*/
public function testBlockRenderedInCurrentRequest() {
$this->installConfig('search_api_test_views');
\Drupal::service('theme_installer')->install(['stable9']);
\Drupal::service('theme_installer')->install(['claro']);
$block = $this->placeBlock('views_block:search_api_test_block_view-block_1', [
'theme' => 'stable9',
]);
$this->assertTrue($block->status());
/** @var \Drupal\search_api\Plugin\search_api\display\ViewsBlock $display */
$display = $this->container
->get('plugin.manager.search_api.display')
->createInstance('views_block:search_api_test_block_view__block_1');
$this->assertEquals('views_block:search_api_test_block_view__block_1', $display->getPluginId());
$theme = $this->createMock(ActiveTheme::class);
$theme->method('getName')->willReturn('claro', 'stable9');
$theme_manager = $this->createMock(ThemeManagerInterface::class);
$theme_manager->method('getActiveTheme')->willReturn($theme);
$display->setThemeManager($theme_manager);
// In the first call, active theme will be reported as "claro", so block
// would not be rendered; in the second call, with theme "stable9", it
// should be.
$this->assertFalse($display->isRenderedInCurrentRequest());
$this->assertTrue($display->isRenderedInCurrentRequest());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment