You are here

ConfigSubscriber.php in Analytics 8

File

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

namespace Drupal\analytics\EventSubscriber;

use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Analytics ConfigSubscriber.
 */
class ConfigSubscriber implements EventSubscriberInterface {

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

  /**
   * Constructs a new ConfigSubscriber.
   *
   * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator
   *   The cache tags invalidator.
   */
  public function __construct(CacheTagsInvalidatorInterface $cache_tags_invalidator) {
    $this->cacheTagsInvalidator = $cache_tags_invalidator;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      ConfigEvents::SAVE => [
        'onConfigSave',
      ],
    ];
  }

  /**
   * Respond to the config save event.
   *
   * @param \Drupal\Core\Config\ConfigCrudEvent $event
   *   The Event to process.
   */
  public function onConfigSave(ConfigCrudEvent $event) {

    // Changing the Analytics settings means that any cached page might
    // result in a different response, so we need to invalidate them all.
    if ($event
      ->getConfig()
      ->getName() === 'analytics.settings') {
      $this->cacheTagsInvalidator
        ->invalidateTags([
        'rendered',
      ]);
    }
  }

}

Classes

Namesort descending Description
ConfigSubscriber Analytics ConfigSubscriber.