You are here

public function SinglePageSiteManager::executeAndRenderSubRequest in Single Page Site 8

Same name and namespace in other branches
  1. 2.x src/Manager/SinglePageSiteManager.php \Drupal\single_page_site\Manager\SinglePageSiteManager::executeAndRenderSubRequest()
  2. 2.0.x src/Manager/SinglePageSiteManager.php \Drupal\single_page_site\Manager\SinglePageSiteManager::executeAndRenderSubRequest()

Mimics the rendering of page content.

Parameters

string $href: String with href.

Return value

mixed Return result of function call_user_func_array with $controller and $arguments.

Throws

\Drupal\single_page_site\Manager\NotFoundHttpException

File

src/Manager/SinglePageSiteManager.php, line 265

Class

SinglePageSiteManager
Class SinglePageSiteManager.

Namespace

Drupal\single_page_site\Manager

Code

public function executeAndRenderSubRequest($href) {
  $type = HttpKernelInterface::SUB_REQUEST;
  $request = Request::create($href, 'GET');

  // Request.
  $event = new GetResponseEvent($this->httpKernel, $request, $type);
  $this->dispatcher
    ->dispatch(KernelEvents::REQUEST, $event);
  if ($event
    ->hasResponse()) {
    $event = new FilterResponseEvent($this->httpKernel, $request, $type, $event
      ->getResponse());
    $this->dispatcher
      ->dispatch(KernelEvents::RESPONSE, $event);
    $this->dispatcher
      ->dispatch(KernelEvents::FINISH_REQUEST, new FinishRequestEvent($this->httpKernel, $request, $type));
    return $event
      ->getResponse();
  }

  // Load controller.
  if (FALSE === ($controller = $this->resolver
    ->getController($request))) {
    throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s". The route is wrongly configured.', $request
      ->getPathInfo()));
  }
  $event = new FilterControllerEvent($this->httpKernel, $controller, $request, $type);
  $this->dispatcher
    ->dispatch(KernelEvents::CONTROLLER, $event);
  $controller = $event
    ->getController();

  // Controller arguments.
  $arguments = $this->resolver
    ->getArguments($request, $controller);

  // Call controller.
  $build = call_user_func_array($controller, $arguments);

  // Remove all meta tags rendered by this sub page.
  if (isset($build['#attached']['html_head_link'])) {
    unset($build['#attached']['html_head_link']);
  }
  return $build;
}