You are here

public function TwitterWidgetSubscriber::onRespond in Twitter Profile Widget 8

Same name and namespace in other branches
  1. 8.2 src/EventSubscriber/TwitterWidgetSubscriber.php \Drupal\twitter_profile_widget\EventSubscriber\TwitterWidgetSubscriber::onRespond()
  2. 3.x src/EventSubscriber/TwitterWidgetSubscriber.php \Drupal\twitter_profile_widget\EventSubscriber\TwitterWidgetSubscriber::onRespond()

Sets extra headers on successful responses.

Parameters

\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event: The event to process.

File

src/EventSubscriber/TwitterWidgetSubscriber.php, line 96

Class

TwitterWidgetSubscriber
Class TwitterWidgetSubscriber.

Namespace

Drupal\twitter_profile_widget\EventSubscriber

Code

public function onRespond(FilterResponseEvent $event) {
  $request = $event
    ->getRequest();
  $response = $event
    ->getResponse();
  $is_cacheable = $response instanceof CacheableResponseInterface && $this->requestPolicy
    ->check($request) === RequestPolicyInterface::ALLOW && $this->responsePolicy
    ->check($response, $request) !== ResponsePolicyInterface::DENY;
  if ($is_cacheable) {
    $response_cacheability = $response
      ->getCacheableMetadata();
    $tags = $response_cacheability
      ->getCacheTags();
    if (in_array('twitter_widget_view', $tags)) {
      $config = \Drupal::config('twitter_profile_widget.settings');
      $expire = $config
        ->get('expire_internal_cache');
      if ($expire) {

        // Only act on pages that render the twitter_widget_view.
        $this
          ->setExpiresCacheLifetime($response);
      }
    }
  }
}