You are here

public function SinglePageSiteManager::executeAndRenderSubRequest in Single Page Site 2.x

Same name and namespace in other branches
  1. 8 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

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException

File

src/Manager/SinglePageSiteManager.php, line 271

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 RequestEvent($this->httpKernel, $request, $type);
  $this->dispatcher
    ->dispatch(KernelEvents::REQUEST, $event);
  if ($event
    ->hasResponse()) {
    $event = new ControllerEvent($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 ControllerEvent($this->httpKernel, $controller, $request, $type);
  $this->dispatcher
    ->dispatch(KernelEvents::CONTROLLER, $event);
  $controller = $event
    ->getController();

  // Controller arguments.
  $arguments = $this->argumentResolver
    ->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;
}