ฉันมีบล็อกพื้นฐานมากซึ่งเพิ่งแสดง ID ของโหนดปัจจุบัน
<?php
/**
 * @file
 * Contains \Drupal\mymodule\Plugin\Block\ExampleEmptyBlock.
 */
namespace Drupal\mymodule\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
/**
 * @Block(
 *   id = "example_empty",
 *   admin_label = @Translation("Example: empty block")
 * )
 */
class ExampleEmptyBlock extends BlockBase {
  /**
   * {@inheritdoc}
   */
  public function build() {
    $node = \Drupal::routeMatch()->getParameter('node');
    $build = array();
    if ($node) {
      $config = \Drupal::config('system.site');
      $build = array(
        '#type' => 'markup',
        '#markup' => '<p>' . $node->id() . '<p>',
        '#cache' => array(
          'tags' => $this->getCacheTags(),
          'contexts' => $this->getCacheContexts(),
        ),
      );
    }
    return $build;
  }
  /**
   * {@inheritdoc}
   */
  public function getCacheTags() {
    $node = \Drupal::routeMatch()->getParameter('node');
    return Cache::mergeTags(parent::getCacheTags(), ["node:{$node->id()}"]);
  }
  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() {
    return Cache::mergeContexts(parent::getCacheContexts(), ['user.node_grants:view']);
  }
}แต่เมื่อแคชบล็อกยังคงเหมือนเดิมโดยไม่คำนึงถึงโหนดที่ฉันเข้าชม ฉันจะแคชผลลัพธ์ต่อ ID โหนดได้อย่างถูกต้องได้อย่างไร
getCacheTags()จาก BlockBase คุณเพียงแค่ต้องเพิ่มแท็กที่แสดงถึงโหนดของคุณ (โหนด: {nid}) ขอโทษฉันรีบตอนนี้ฉันสามารถอธิบายได้ดีขึ้นในภายหลัง