You are here

public function MediaEntityRenderer::render in Gutenberg 8

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

Render media entities.

Parameters

\Drupal\media\Entity\Media|string $media_entity: Media entity ID or loaded media entity instance.

string $view_mode: (optional) View mode of media entity, 'full' is default.

Return value

string Rendered HTML of the media entity.

Overrides MediaEntityRendererInterface::render

File

src/MediaEntityRenderer.php, line 47

Class

MediaEntityRenderer
Renders media entities to the HTML.

Namespace

Drupal\gutenberg

Code

public function render($media_entity, string $view_mode = 'full') {
  try {
    if (is_numeric($media_entity)) {
      $media_entity = $this->entityTypeManager
        ->getStorage('media')
        ->load($media_entity);
    }
    $this
      ->assertIsMediaEntity($media_entity);
    $build = $this->entityTypeManager
      ->getViewBuilder('media')
      ->view($media_entity, $view_mode);
    return (string) $this->renderer
      ->render($build);
  } catch (\Throwable $exception) {
    return '';
  }
}