You are here

public function StaticPageSubscriber::onRequest in Static Page 8

Performs check on the beginning of a request.

File

src/EventSubscriber/StaticPageSubscriber.php, line 18

Class

StaticPageSubscriber
Static Page subscriber for controller requests.

Namespace

Drupal\static_page\EventSubscriber

Code

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);
    }
  }
}