You are here

class StaticPageSubscriber in Static Page 8

Static Page subscriber for controller requests.

Hierarchy

  • class \Drupal\static_page\EventSubscriber\StaticPageSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of StaticPageSubscriber

1 string reference to 'StaticPageSubscriber'
static_page.services.yml in ./static_page.services.yml
static_page.services.yml
1 service uses StaticPageSubscriber
static_page.subscriber in ./static_page.services.yml
Drupal\static_page\EventSubscriber\StaticPageSubscriber

File

src/EventSubscriber/StaticPageSubscriber.php, line 13

Namespace

Drupal\static_page\EventSubscriber
View source
class StaticPageSubscriber implements EventSubscriberInterface {

  /**
   * Performs check on the beginning of a request.
   */
  public function onRequest(GetResponseEvent $event) {

    // $current_path = \Drupal::service('path.current')->getPath();
    // $url_object = \Drupal::service('path.validator')->getUrlIfValid($form_state->getValue($current_path));
    // $route_name = $url_object->getRouteName();
    $route_name = \Drupal::routeMatch()
      ->getRouteName();
    if ($route_name == 'entity.node.canonical') {
      $node = \Drupal::routeMatch()
        ->getParameter('node');
    }
    elseif ($route_name == 'entity.node.revision') {
      $vid = \Drupal::routeMatch()
        ->getParameter('node_revision');
      $node = node_revision_load($vid);
    }
    if (!empty($node)) {
      $type = $node
        ->getType();
      $config = \Drupal::config('static_page.fields');
      $static_fields = $config
        ->get('fields');
      if (!empty($static_fields[$type])) {
        $static_page = $node
          ->get($static_fields[$type])->value;
        $response = new Response($static_page);
        $event
          ->setResponse($response);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = [
      'onRequest',
    ];

    // $events[KernelEvents::RESPONSE][] = array('onResponse');
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StaticPageSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
StaticPageSubscriber::onRequest public function Performs check on the beginning of a request.