MediaEntityRenderer.php in Gutenberg 8.2
File
src/MediaEntityRenderer.php
View source
<?php
namespace Drupal\gutenberg;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Render\RendererInterface;
class MediaEntityRenderer implements MediaEntityRendererInterface {
use AssertMediaTrait;
protected $renderer;
protected $entityTypeManager;
public function __construct(RendererInterface $renderer, EntityTypeManagerInterface $entity_type_manager) {
$this->renderer = $renderer;
$this->entityTypeManager = $entity_type_manager;
}
public function render($media_entity, string $view_mode = 'full') {
$render_array = $this
->renderArray($media_entity, $view_mode);
return (string) $this->renderer
->render($render_array);
}
public function renderArray($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);
return $this->entityTypeManager
->getViewBuilder('media')
->view($media_entity, $view_mode);
} catch (\Throwable $exception) {
return [];
}
}
}