You are here

public function CustomTag::getCacheTags in Views Custom Cache Tags 8

Gets an array of cache tags for the current view.

Return value

string[] An array of cache tags based on the current view.

Overrides CachePluginBase::getCacheTags

File

src/Plugin/views/cache/CustomTag.php, line 99
Contains \Drupal\views_custom_cache_tag\Plugin\views\cache\CustomTag.

Class

CustomTag
Simple caching of query results for Views displays.

Namespace

Drupal\views_custom_cache_tag\Plugin\views\cache

Code

public function getCacheTags() {
  $tags = parent::getCacheTags();

  // Remove the the list cache tags for the entity types listed in this view.
  // @see CachePluginBase::getCacheTags().
  $entity_information = $this->view
    ->getQuery()
    ->getEntityTableInfo();
  if (!empty($entity_information)) {

    // Add the list cache tags for each entity type used by this view.
    foreach ($entity_information as $table => $metadata) {
      $remove = \Drupal::entityTypeManager()
        ->getDefinition($metadata['entity_type'])
        ->getListCacheTags();
      $tags = array_diff($tags, $remove);
    }
  }
  $custom_tags = preg_split('/\\r\\n|[\\r\\n]/', $this->options['custom_tag']);
  $custom_tags = array_map('trim', $custom_tags);
  $custom_tags = array_map(function ($tag) {
    return $this->view
      ->getStyle()
      ->tokenizeValue($tag, 0);
  }, $custom_tags);
  return Cache::mergeTags($custom_tags, $tags);
}