You are here

public function EntityView::processRequest in Services 9.0.x

Same name and namespace in other branches
  1. 8.4 src/Plugin/ServiceDefinition/EntityView.php \Drupal\services\Plugin\ServiceDefinition\EntityView::processRequest()

Processes the request and returns an array of data as appropriate.

Parameters

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

\Drupal\Core\Routing\RouteMatchInterface $route_match: The route match object.

\Symfony\Component\Serializer\SerializerInterface $serializer: The serializer. Some methods might require the plugin to leverage the serializer after extracting the request contents.

Return value

array The response.

Throws

\Symfony\Component\HttpKernel\Exception\HttpException

Overrides ServiceDefinitionInterface::processRequest

File

src/Plugin/ServiceDefinition/EntityView.php, line 65

Class

EntityView
Plugin annotation @ServiceDefinition( id = "entity_view", methods = { "GET" }, translatable = true, deriver = "\Drupal\services\Plugin\Deriver\EntityView" )

Namespace

Drupal\services\Plugin\ServiceDefinition

Code

public function processRequest(Request $request, RouteMatchInterface $route_match, SerializerInterface $serializer) {
  $view_mode = 'full';
  if ($request->query
    ->has('view_mode')) {
    $view_mode = $request->query
      ->get('view_mode');
  }

  /* @var $entity \Drupal\Core\Entity\EntityInterface */
  $entity = $this
    ->getContextValue($this
    ->getDerivativeId());
  $view_builder = \Drupal::entityTypeManager()
    ->getViewBuilder($entity
    ->getEntityTypeId());
  $render_array = $view_builder
    ->view($entity, $view_mode);
  $result = [];
  $result['body'] = $this->renderer
    ->renderRoot($render_array);
  $all_assets = $this
    ->gatherAssetMarkup($render_array);
  $result += $this
    ->renderAssets($all_assets);
  return $result;
}