LocaleTranslationCacheTag.php

Same filename in other branches
  1. 9 core/modules/locale/src/EventSubscriber/LocaleTranslationCacheTag.php
  2. 8.9.x core/modules/locale/src/EventSubscriber/LocaleTranslationCacheTag.php
  3. 10 core/modules/locale/src/EventSubscriber/LocaleTranslationCacheTag.php

Namespace

Drupal\locale\EventSubscriber

File

core/modules/locale/src/EventSubscriber/LocaleTranslationCacheTag.php

View source
<?php

namespace Drupal\locale\EventSubscriber;

use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\locale\LocaleEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * A subscriber invalidating cache tags when translating a string.
 */
class LocaleTranslationCacheTag implements EventSubscriberInterface {
    
    /**
     * The cache tags invalidator.
     *
     * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
     */
    protected $cacheTagsInvalidator;
    
    /**
     * Constructs a LocaleTranslationCacheTag object.
     *
     * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator
     *   The cache tags invalidator.
     */
    public function __construct(CacheTagsInvalidatorInterface $cache_tags_invalidator) {
        $this->cacheTagsInvalidator = $cache_tags_invalidator;
    }
    
    /**
     * Invalidate cache tags whenever a string is translated.
     */
    public function saveTranslation() {
        $this->cacheTagsInvalidator
            ->invalidateTags([
            'rendered',
            'locale',
            'library_info',
        ]);
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array {
        $events[LocaleEvents::SAVE_TRANSLATION][] = [
            'saveTranslation',
        ];
        return $events;
    }

}

Classes

Title Deprecated Summary
LocaleTranslationCacheTag A subscriber invalidating cache tags when translating a string.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.