class MediaEntityRenderer in Gutenberg 8
Same name and namespace in other branches
- 8.2 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;
/**
* Drupal\Core\Render\RendererInterface instance.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Entity type manager instance.
*
* @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') {
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 '';
}
}
}
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 | Entity type manager instance. | |
MediaEntityRenderer:: |
protected | property | Drupal\Core\Render\RendererInterface instance. | |
MediaEntityRenderer:: |
public | function |
Render media entities. Overrides MediaEntityRendererInterface:: |
|
MediaEntityRenderer:: |
public | function | MediaEntityRenderer constructor. |