class MediaEntityRenderer in Gutenberg 8.2
Same name and namespace in other branches
- 8 src/MediaEntityRenderer.php \Drupal\gutenberg\MediaEntityRenderer
Renders media entities to the HTML.
@package Drupal\gutenberg
Hierarchy
- class \Drupal\gutenberg\MediaEntityRenderer implements MediaEntityRendererInterface uses AssertMediaTrait
Expanded class hierarchy of MediaEntityRenderer
1 string reference to 'MediaEntityRenderer'
1 service uses MediaEntityRenderer
File
- src/
MediaEntityRenderer.php, line 13
Namespace
Drupal\gutenbergView source
class MediaEntityRenderer implements MediaEntityRendererInterface {
use AssertMediaTrait;
/**
* The Drupal renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* MediaEntityRenderer constructor.
*
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(RendererInterface $renderer, EntityTypeManagerInterface $entity_type_manager) {
$this->renderer = $renderer;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritDoc}
*/
public function render($media_entity, string $view_mode = 'full') {
$render_array = $this
->renderArray($media_entity, $view_mode);
return (string) $this->renderer
->render($render_array);
}
/**
* {@inheritDoc}
*/
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 [];
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AssertMediaTrait:: |
protected | function | Assert if provided entity is type of FileInterface. | |
AssertMediaTrait:: |
protected | function | Assert if provided entity is type of MediaInterface. | |
MediaEntityRenderer:: |
protected | property | The entity type manager. | |
MediaEntityRenderer:: |
protected | property | The Drupal renderer. | |
MediaEntityRenderer:: |
public | function |
Render media entities. Overrides MediaEntityRendererInterface:: |
|
MediaEntityRenderer:: |
public | function |
Return the media entity render array. Overrides MediaEntityRendererInterface:: |
|
MediaEntityRenderer:: |
public | function | MediaEntityRenderer constructor. |