You are here

public function DsExtrasController::switchViewMode in Display Suite 8.3

Same name and namespace in other branches
  1. 8.4 modules/ds_extras/src/Controller/DsExtrasController.php \Drupal\ds_extras\Controller\DsExtrasController::switchViewMode()
  2. 8.2 modules/ds_extras/src/Controller/DsExtrasController.php \Drupal\ds_extras\Controller\DsExtrasController::switchViewMode()

Returns an node through JSON.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The global request object.

string $entityType: The type of the requested entity.

string $entityId: The id of the requested entity.

string $viewMode: The view mode you wish to render for the requested entity.

Return value

\Drupal\Core\Ajax\AjaxResponse An ajax response with the new view mode.

1 string reference to 'DsExtrasController::switchViewMode'
ds_extras.routing.yml in modules/ds_extras/ds_extras.routing.yml
modules/ds_extras/ds_extras.routing.yml

File

modules/ds_extras/src/Controller/DsExtrasController.php, line 32

Class

DsExtrasController
Returns responses for Display Suite Extra routes.

Namespace

Drupal\ds_extras\Controller

Code

public function switchViewMode(Request $request, $entityType, $entityId, $viewMode) {
  $response = new AjaxResponse();
  $entity = $this
    ->entityTypeManager()
    ->getStorage($entityType)
    ->load($entityId);
  if ($entity
    ->access('view')) {
    $element = $this
      ->entityTypeManager()
      ->getViewBuilder($entityType)
      ->view($entity, $viewMode);
    $content = \Drupal::service('renderer')
      ->render($element, FALSE);
    $response
      ->addCommand(new ReplaceCommand('.' . $request
      ->get('selector'), $content));
  }
  return $response;
}