mergeTags example

'top_all_num' => 3,
      'top_last_num' => 3,
    ]);

    // Get some page and check if the block is displayed.     $this->drupalGet('user');
    $this->assertSession()->pageTextContains('Popular content');
    $this->assertSession()->pageTextContains("Today's");
    $this->assertSession()->pageTextContains('All time');
    $this->assertSession()->pageTextContains('Last viewed');

    $tags = Cache::mergeTags($node->getCacheTags()$block->getCacheTags());
    $tags = Cache::mergeTags($tags$this->blockingUser->getCacheTags());
    $tags = Cache::mergeTags($tags['block_view', 'config:block_list', 'node_list', 'rendered', 'user_view']);
    $this->assertCacheTags($tags);
    $contexts = Cache::mergeContexts($node->getCacheContexts()$block->getCacheContexts());
    $contexts = Cache::mergeContexts($contexts['url.query_args:_wrapper_format', 'url.site']);
    $this->assertCacheContexts($contexts);

    // Check if the node link is displayed.     $this->assertSession()->responseContains(Link::fromTextAndUrl($node->label()$node->toUrl('canonical'))->toString());
  }

}


    $expected = $serializer->serialize($entities, 'json');

    $actual_json = $this->drupalGet('test/serialize/entity', ['query' => ['_format' => 'json']]);
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSame($expected$actual_json, 'The expected JSON output was found.');
    $expected_cache_tags = $view->getCacheTags();
    $expected_cache_tags[] = 'entity_test_list';
    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    foreach ($entities as $entity) {
      $expected_cache_tags = Cache::mergeTags($expected_cache_tags$entity->getCacheTags());
    }
    $this->assertCacheTags($expected_cache_tags);
    $this->assertCacheContexts(['languages:language_interface', 'theme', 'entity_test_view_grants', 'request_format']);

    // Change the format to xml.     $view->setDisplay('rest_export_1');
    $view->getDisplay()->setOption('style', [
      'type' => 'serializer',
      'options' => [
        'uses_fields' => FALSE,
        'formats' => [
          
    // instead (so $element['#printed'] will not be set).     $element = $test_element;
    $this->renderer->renderRoot($element);
    $this->assertFalse(isset($element['#printed']), 'Cache hit');

    // Test that cache tags are correctly collected from the render element,     // including the ones from its subchild.     $this->assertEquals($expected_tags$element['#cache']['tags'], 'Cache tags were collected from the element and its subchild.');

    // The cache item also has a 'rendered' cache tag.     $cache_item = $this->cacheFactory->get('render')->get('render_cache_test:en:stark');
    $this->assertSame(Cache::mergeTags($expected_tags['rendered'])$cache_item->tags);
  }

  /** * @covers ::render * @covers ::doRender * @covers \Drupal\Core\Render\RenderCache::get * @covers \Drupal\Core\Render\RenderCache::set * @covers \Drupal\Core\Render\RenderCache::createCacheID * * @dataProvider providerTestRenderCacheMaxAge */
  
$this->assertResourceResponse(200, FALSE, $response);
    $updated_normalization = Json::decode((string) $response->getBody());
    $this->assertSame($normalization['data']['attributes']['path']['alias']$updated_normalization['data']['attributes']['path']['alias']);
  }

  /** * {@inheritdoc} */
  protected function getExpectedCacheTags(array $sparse_fieldset = NULL) {
    $tags = parent::getExpectedCacheTags($sparse_fieldset);
    if ($sparse_fieldset === NULL || in_array('description', $sparse_fieldset)) {
      $tags = Cache::mergeTags($tags['config:filter.format.plain_text', 'config:filter.settings']);
    }
    return $tags;
  }

  /** * {@inheritdoc} */
  protected function getExpectedCacheContexts(array $sparse_fieldset = NULL) {
    $contexts = parent::getExpectedCacheContexts($sparse_fieldset);
    if ($sparse_fieldset === NULL || in_array('description', $sparse_fieldset)) {
      $contexts = Cache::mergeContexts($contexts['languages:language_interface', 'theme']);
    }
    // corresponding $request->isMethodCacheable() checks are removed from     // Drupal\Core\Render\Renderer.     $request_stack = $this->container->get('request_stack');
    $request_stack->push(new Request());
    $this->container->get('renderer')->renderRoot($build);
    $request_stack->pop();

    // Expected keys, contexts, and tags for the block.     // @see \Drupal\block\BlockViewBuilder::viewMultiple()     $expected_block_cache_keys = ['entity_view', 'block', $block->id()];
    $expected_block_cache_contexts = ['languages:' . LanguageInterface::TYPE_INTERFACE, 'theme', 'user.permissions'];
    $expected_block_cache_tags = Cache::mergeTags(['block_view', 'rendered']$block->getCacheTags());
    $expected_block_cache_tags = Cache::mergeTags($expected_block_cache_tags$block->getPlugin()->getCacheTags());

    // Expected contexts and tags for the BlockContent entity.     // @see \Drupal\Core\Entity\EntityViewBuilder::getBuildDefaults().     $expected_entity_cache_contexts = ['theme'];
    $expected_entity_cache_tags = Cache::mergeTags(['block_content_view']$this->entity->getCacheTags());
    $expected_entity_cache_tags = Cache::mergeTags($expected_entity_cache_tags$this->getAdditionalCacheTagsForEntity($this->entity));

    // Verify that what was render cached matches the above expectations.     $cid = $this->createCacheId($expected_block_cache_keys$expected_block_cache_contexts);
    $redirected_cid = $this->createCacheId($expected_block_cache_keys, Cache::mergeContexts($expected_block_cache_contexts$expected_entity_cache_contexts));
    


  /** * {@inheritdoc} */
  protected function invalidateTagsOnSave($update) {
    $tags = $this->getListCacheTagsToInvalidate();
    // Always invalidate the 404 or 403 response cache because while files do     // not have a canonical URL as such, they may be served via routes such as     // private files.     // Creating or updating an entity may change a cached 403 or 404 response.     $tags = Cache::mergeTags($tags['4xx-response']);
    if ($update) {
      $tags = Cache::mergeTags($tags$this->getCacheTagsToInvalidate());
    }
    Cache::invalidateTags($tags);
  }

}


    // Node search results for shop, should return node:1 (bike shed shop) and     // node:2 (Llama shop). The related authors cache tags should be visible as     // well.     $edit = [];
    $edit['keys'] = 'shop';
    $this->drupalGet('search/node');
    $this->submitForm($edit, 'Search');
    $this->assertSession()->pageTextContains('bike shed shop');
    $this->assertSession()->pageTextContains('Llama shop');
    $expected_cache_tags = Cache::mergeTags($default_search_tags[
      'node:1',
      'user:2',
      'node:2',
      'user:3',
      'node_view',
      'config:filter.format.plain_text',
    ]);
    $this->assertCacheTags($expected_cache_tags);

    // Only get the new node in the search results, should result in node:1,     // node:2 and user:3 as cache tags even though only node:1 is shown. This is
if ($this->isNew()) {
      return [];
    }
    return [$this->entityTypeId . ':' . $this->id()];
  }

  /** * {@inheritdoc} */
  public function getCacheTags() {
    if ($this->cacheTags) {
      return Cache::mergeTags($this->getCacheTagsToInvalidate()$this->cacheTags);
    }
    return $this->getCacheTagsToInvalidate();
  }

  /** * {@inheritdoc} */
  public function getCacheMaxAge() {
    return $this->cacheMaxAge;
  }

  
/** * Applies the cacheability of the current display to the given render array. * * @param array $element * The render array with updated cacheability metadata. */
  protected function applyDisplayCacheabilityMetadata(array &$element) {
    /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache */
    $cache = $this->getPlugin('cache');

    (new CacheableMetadata())
      ->setCacheTags(Cache::mergeTags($this->view->getCacheTags()$this->display['cache_metadata']['tags'] ?? []))
      ->setCacheContexts($this->display['cache_metadata']['contexts'] ?? [])
      ->setCacheMaxAge(Cache::mergeMaxAges($cache->getCacheMaxAge()$this->display['cache_metadata']['max-age'] ?? Cache::PERMANENT))
      ->merge(CacheableMetadata::createFromRenderArray($element))
      ->applyTo($element);
  }

  /** * {@inheritdoc} */
  public function elementPreRender(array $element) {
    $view = $element['#view'];
    
$query->sort($label_field);
    }

    $entities = $this->entityTypeManager()
      ->getStorage($entity_type_id)
      ->loadMultiple($query->execute());

    $cache_tags = [];
    $labels = [];
    foreach ($entities as $entity) {
      $labels[] = $entity->label();
      $cache_tags = Cache::mergeTags($cache_tags$entity->getCacheTags());
    }
    // Always associate the list cache tag, otherwise the cached empty result     // wouldn't be invalidated. This would continue to show nothing matches the     // query, even though a newly created entity might match the query.     $cache_tags = Cache::mergeTags($cache_tags$entity_type_definition->getListCacheTags());

    return [
      '#theme' => 'item_list',
      '#items' => $labels,
      '#title' => $entity_type_id . ' entities',
      '#cache' => [
        

  protected function getExpectedUnauthorizedEntityAccessCacheability($is_authenticated) {
    // @see \Drupal\block_content\BlockContentAccessControlHandler()     return parent::getExpectedUnauthorizedEntityAccessCacheability($is_authenticated)
      ->addCacheTags(['block_content:1']);
  }

  /** * {@inheritdoc} */
  protected function getExpectedCacheTags() {
    return Cache::mergeTags(parent::getExpectedCacheTags()['config:filter.format.plain_text']);
  }

  /** * {@inheritdoc} */
  protected function getExpectedCacheContexts() {
    return Cache::mergeContexts(['url.site']$this->container->getParameter('renderer.config')['required_cache_contexts']);
  }

}
    $base_cache_tags = [];
    if (!empty($image_style_setting)) {
      $image_style = $this->imageStyleStorage->load($image_style_setting);
      $base_cache_tags = $image_style->getCacheTags();
    }

    foreach ($files as $delta => $file) {
      if (isset($link_file)) {
        $image_uri = $file->getFileUri();
        $url = $this->fileUrlGenerator->generate($image_uri);
      }
      $cache_tags = Cache::mergeTags($base_cache_tags$file->getCacheTags());

      // Extract field item attributes for the theme function, and unset them       // from the $item so that the field template does not re-render them.       $item = $file->_referringItem;
      $item_attributes = $item->_attributes;
      unset($item->_attributes);

      $image_loading_settings = $this->getSetting('image_loading');
      $item_attributes['loading'] = $image_loading_settings['attribute'];

      $elements[$delta] = [
        
/** * {@inheritdoc} */
  public function getCacheContexts() {
    return $this->cacheContexts;
  }

  /** * {@inheritdoc} */
  public function getCacheTags() {
    return Cache::mergeTags(['config:' . $this->name]$this->cacheTags);
  }

  /** * {@inheritdoc} */
  public function getCacheMaxAge() {
    return $this->cacheMaxAge;
  }

  /** * Casts any objects that implement MarkupInterface to string. * * @param mixed $data * The configuration data. * * @return mixed * The data with any safe strings cast to string. */
/** * {@inheritdoc} */
  public function viewMultiple(array $entities = []$view_mode = 'full', $langcode = NULL) {
    /** @var \Drupal\tour\TourInterface[] $entities */
    $tour = [];
    $cache_tags = [];
    $total_tips = 0;
    foreach ($entities as $entity_id => $entity) {
      $tour[$entity_id] = $entity->getTips();
      $total_tips += count($tour[$entity_id]);
      $cache_tags = Cache::mergeTags($cache_tags$entity->getCacheTags());
    }

    $items = [];
    foreach ($tour as $tour_id => $tips) {
      $tourEntity = $entities[$tour_id];

      foreach ($tips as $index => $tip) {
        $classes = [
          'tip-module-' . Html::getClass($tourEntity->getModule()),
          'tip-type-' . Html::getClass($tip->getPluginId()),
          'tip-' . Html::getClass($tip->id()),
        ];
$nonempty_entity_listing_url = Url::fromRoute('entity.entity_test.collection_labels_alphabetically', ['entity_type_id' => $entity_type]);

    // The default cache contexts for rendered entities.     $default_cache_contexts = ['languages:' . LanguageInterface::TYPE_INTERFACE, 'theme', 'user.permissions'];
    $entity_cache_contexts = Cache::mergeContexts($default_cache_contexts['url.site']);
    $page_cache_contexts = Cache::mergeContexts($default_cache_contexts['url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT]);

    // Cache tags present on every rendered page.     // 'user.permissions' is a required cache context, and responses that vary     // by this cache context when requested by anonymous users automatically     // also get this cache tag, to ensure correct invalidation.     $page_cache_tags = Cache::mergeTags(['http_response', 'rendered']['config:user.role.anonymous']);
    // If the block module is used, the Block page display variant is used,     // which adds the block config entity type's list cache tags.     $page_cache_tags = Cache::mergeTags($page_cache_tags, \Drupal::moduleHandler()->moduleExists('block') ? ['config:block_list'] : []);

    $page_cache_tags_referencing_entity = in_array('user.permissions', $this->getAccessCacheContextsForEntity($this->referencingEntity)) ? ['config:user.role.anonymous'] : [];

    $view_cache_tag = [];
    if ($this->entity->getEntityType()->hasHandlerClass('view_builder')) {
      $view_cache_tag = \Drupal::entityTypeManager()->getViewBuilder($entity_type)
        ->getCacheTags();
    }

    
Home | Imprint | This part of the site doesn't use cookies.