You are here

public function MediaController::render in Gutenberg 8

Same name and namespace in other branches
  1. 8.2 src/Controller/MediaController.php \Drupal\gutenberg\Controller\MediaController::render()

Render provided media entity.

Parameters

string $media: Media data (numeric or stringified JSON for media data processing).

Return value

\Symfony\Component\HttpFoundation\JsonResponse The JSON response.

1 string reference to 'MediaController::render'
gutenberg.routing.yml in ./gutenberg.routing.yml
gutenberg.routing.yml

File

src/Controller/MediaController.php, line 120

Class

MediaController
Returns responses for our image routes.

Namespace

Drupal\gutenberg\Controller

Code

public function render(string $media) {
  $media_entities = $this->mediaSelectionProcessorManager
    ->processData($media);
  try {
    if (!$media_entities) {
      throw new MediaEntityNotFoundException();
    }
    $media_entity = reset($media_entities);
    return new JsonResponse([
      'view_modes' => $this->mediaService
        ->getRenderedMediaEntity($media_entity),
      'media_entity' => [
        'id' => $media_entity
          ->id(),
        'type' => $media_entity
          ->bundle(),
      ],
    ]);
  } catch (MediaEntityNotFoundException $exception) {
    return new JsonResponse([
      'error' => $this
        ->t($exception
        ->getMessage()),
    ], 404);
  }
}