Skip to content
Snippets Groups Projects
Commit 26b7e60d authored by catch's avatar catch
Browse files

Issue #3515774 by prudloff, smustgrave: Cache tags added by...

Issue #3515774 by prudloff, smustgrave: Cache tags added by hook_block_view_BASE_BLOCK_ID_alter are ignored on blocks that implement MainContentBlockPluginInterface or TitleBlockPluginInterface
parent cca39816
Branches
Tags
No related merge requests found
Pipeline #509106 passed with warnings
Pipeline: drupal

#509109

    ......@@ -67,7 +67,11 @@ public function viewMultiple(array $entities = [], $view_mode = 'full', $langcod
    if ($plugin instanceof MainContentBlockPluginInterface || $plugin instanceof TitleBlockPluginInterface) {
    // Immediately build a #pre_render-able block, since this block cannot
    // be built lazily.
    $build[$entity_id] += static::buildPreRenderableBlock($entity, $this->moduleHandler());
    $cacheableMetadata = CacheableMetadata::createFromRenderArray($build[$entity_id]);
    $preRenderableBlock = static::buildPreRenderableBlock($entity, $this->moduleHandler());
    $cacheableMetadata->addCacheableDependency(CacheableMetadata::createFromRenderArray($preRenderableBlock));
    $build[$entity_id] += $preRenderableBlock;
    $cacheableMetadata->applyTo($build[$entity_id]);
    }
    else {
    // Assign a #lazy_builder callback, which will generate a #pre_render-
    ......
    ......@@ -36,6 +36,16 @@ public function blockViewTestCacheAlter(array &$build, BlockPluginInterface $blo
    }
    }
    /**
    * Implements hook_block_view_BASE_BLOCK_ID_alter().
    *
    * @see \Drupal\Tests\block\Kernel\BlockViewBuilderTest::testBlockViewBuilderCacheTitleBlock()
    */
    #[Hook('block_view_page_title_block_alter')]
    public function blockViewPageTitleBlockAlter(array &$build, BlockPluginInterface $block): void {
    $build['#cache']['tags'][] = 'custom_cache_tag';
    }
    /**
    * Implements hook_block_build_BASE_BLOCK_ID_alter().
    */
    ......
    ......@@ -163,6 +163,30 @@ public function testBlockViewBuilderCache(): void {
    $this->verifyRenderCacheHandling();
    }
    /**
    * Tests title block render cache handling.
    *
    * @see \Drupal\block_test\Hook\BlockTestHooks::blockViewPageTitleBlockAlter()
    */
    public function testBlockViewBuilderCacheTitleBlock(): void {
    // Create title block.
    $this->block = $this->controller->create([
    'id' => 'test_block_title',
    'theme' => 'stark',
    'plugin' => 'page_title_block',
    ]);
    $this->block->save();
    $entity = Block::load('test_block_title');
    $builder = \Drupal::entityTypeManager()->getViewBuilder('block');
    $output = $builder->view($entity, 'block');
    $this->assertSame(
    ['block_view', 'config:block.block.test_block_title', 'custom_cache_tag'],
    $output['#cache']['tags']
    );
    }
    /**
    * Verifies render cache handling of the block being tested.
    *
    ......
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment