Commit 79e45043 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

(cherry picked from commit 26b7e60d)
parent 98cb8e27
Loading
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -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-
+10 −0
Original line number Diff line number Diff line
@@ -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().
   */
+24 −0
Original line number Diff line number Diff line
@@ -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.
   *