MediaEntityBlockFilter.php in Gutenberg 8
File
src/Plugin/Filter/MediaEntityBlockFilter.php
View source
<?php
namespace Drupal\gutenberg\Plugin\Filter;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Drupal\gutenberg\MediaEntityRendererInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MediaEntityBlockFilter extends FilterBase implements ContainerFactoryPluginInterface {
protected $mediaEntityRenderer;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MediaEntityRendererInterface $media_entity_renderer_interface) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->mediaEntityRenderer = $media_entity_renderer_interface;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('gutenberg.media_entity.renderer'));
}
public function process($text, $langcode) {
$content = preg_replace_callback('#(<!-- wp:drupalmedia\\/drupal-media-entity (\\{.*\\}) \\/-->)#', [
$this,
'render',
], $text);
return new FilterProcessResult($content);
}
protected function render(array $match) {
if (!isset($match[2])) {
return '';
}
$full_block_comment = str_replace('/-->', '-->', $match[1]);
$block_config = json_decode($match[2], TRUE);
if (json_last_error() !== JSON_ERROR_NONE || empty($block_config['mediaEntityIds'])) {
return '';
}
$media_ouput = $this->mediaEntityRenderer
->render(reset($block_config['mediaEntityIds']), $block_config['viewMode'] ?? 'default');
$output = $media_ouput;
if (isset($block_config['caption'])) {
$output = '<figure class="wp-block-drupalmedia-drupal-media-entity">' . $media_ouput . '<figcaption>' . $block_config['caption'] . '</figcaption></figure>';
}
return $full_block_comment . "\n" . $output . "\n<!-- /wp:drupalmedia/drupal-media-entity -->\n";
}
}