username-1.0.x-dev/src/EventSubscriber/ConfigCacheTag.php
src/EventSubscriber/ConfigCacheTag.php
<?php
namespace Drupal\username\EventSubscriber;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* A subscriber invalidating cache tags when username config objects are saved.
*/
class ConfigCacheTag implements EventSubscriberInterface {
/**
* The cache tags invalidator.
*
* @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
*/
protected $cacheTagsInvalidator;
/**
* Constructs a UsernameCacheTag 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 when particular username config objects are saved.
*
* @param \Drupal\Core\Config\ConfigCrudEvent $event
* The Event to process.
*/
public function onSave(ConfigCrudEvent $event) {
// Check if the username settings object has been changed.
if ($event->getConfig()->getName() === 'username.settings') {
// Clear the username cache if the pattern was changed.
username_display_name_delete_all();
// A change to the display-name pattern must invalidate the render cache
// since the display-name could be used anywhere.
$this->cacheTagsInvalidator->invalidateTags(['rendered']);
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[ConfigEvents::SAVE][] = ['onSave'];
return $events;
}
}
