You are here

class ConfigSubscriber in JSON:API Image Styles 2.0.x

Same name and namespace in other branches
  1. 3.0.x src/EventSubscriber/ConfigSubscriber.php \Drupal\jsonapi_image_styles\EventSubscriber\ConfigSubscriber

Associates config cache tag with all JSON:API responses.

Hierarchy

  • class \Drupal\jsonapi_image_styles\EventSubscriber\ConfigSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of ConfigSubscriber

1 string reference to 'ConfigSubscriber'
jsonapi_image_styles.services.yml in ./jsonapi_image_styles.services.yml
jsonapi_image_styles.services.yml
1 service uses ConfigSubscriber
jsonapi_image_styles.config_subscriber in ./jsonapi_image_styles.services.yml
Drupal\jsonapi_image_styles\EventSubscriber\ConfigSubscriber

File

src/EventSubscriber/ConfigSubscriber.php, line 13

Namespace

Drupal\jsonapi_image_styles\EventSubscriber
View source
class ConfigSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() : array {

    // Run before
    // \Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber::onResponse()
    // (priority 128) so config cache tag can be added.
    $events[KernelEvents::RESPONSE][] = [
      'onResponse',
      129,
    ];
    return $events;
  }

  /**
   * Associates config cache tag with all JSON:API responses.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   Response event.
   */
  public function onResponse(FilterResponseEvent $event) : void {
    if ($event
      ->getRequest()
      ->getRequestFormat() !== 'api_json') {
      return;
    }
    $response = $event
      ->getResponse();
    if (!$response instanceof CacheableResponseInterface) {
      return;
    }
    $response
      ->getCacheableMetadata()
      ->addCacheTags([
      'config:jsonapi_image_styles.settings',
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigSubscriber::getSubscribedEvents public static function
ConfigSubscriber::onResponse public function Associates config cache tag with all JSON:API responses.