You are here

public function MainController::displayEntity in View Mode Page 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Controller/MainController.php \Drupal\view_mode_page\Controller\MainController::displayEntity()
  2. 3.2.x src/Controller/MainController.php \Drupal\view_mode_page\Controller\MainController::displayEntity()

Display a entity in a given view mode.

Parameters

string $view_mode: The view mode.

string $entity_type: The entity type.

mixed $entity_id: The entity id.

Return value

\Symfony\Component\HttpFoundation\Response The Symfony response.

1 string reference to 'MainController::displayEntity'
view_mode_page.routing.yml in ./view_mode_page.routing.yml
view_mode_page.routing.yml

File

src/Controller/MainController.php, line 78

Class

MainController
Class MainController.

Namespace

Drupal\view_mode_page\Controller

Code

public function displayEntity($view_mode, $entity_type, $entity_id) {
  $entity = $this
    ->entityTypeManager()
    ->getStorage($entity_type)
    ->load($entity_id);
  $entity_url = $entity
    ->toUrl();

  // Response method as used in
  // core/lib/Drupal/Core/EventSubscriber/DefaultExceptionHtmlSubscriber.php:122
  // but with access aware router.
  $request = $this->requestStack
    ->getCurrentRequest();
  $sub_request = clone $request;
  $sub_request->attributes
    ->add($this->router
    ->match('/' . $entity_url
    ->getInternalPath()));
  $sub_request->attributes
    ->add([
    'view_mode' => $view_mode,
  ]);
  $response = $this->httpKernel
    ->handle($sub_request, HttpKernelInterface::SUB_REQUEST);
  return $response;
}