You are here

ConfigCacheTag.php in Real Name 2.x

Same filename and directory in other branches
  1. 8 src/EventSubscriber/ConfigCacheTag.php

File

src/EventSubscriber/ConfigCacheTag.php
View source
<?php

namespace Drupal\realname\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 realname config objects are saved.
 */
class ConfigCacheTag implements EventSubscriberInterface {

  /**
   * The cache tags invalidator.
   *
   * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
   */
  protected $cacheTagsInvalidator;

  /**
   * Constructs a RealnameCacheTag 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 realname config objects are saved.
   *
   * @param \Drupal\Core\Config\ConfigCrudEvent $event
   *   The Event to process.
   */
  public function onSave(ConfigCrudEvent $event) {

    // Check if realname settings object has been changed.
    if ($event
      ->getConfig()
      ->getName() === 'realname.settings') {

      // Clear the realname cache if the pattern was changed.
      realname_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;
  }

}

Classes

Namesort descending Description
ConfigCacheTag A subscriber invalidating cache tags when realname config objects are saved.