You are here

protected function MediaEntityBlockFilter::render in Gutenberg 8

The render function.

Callbacks to preg replace from ::process method and returns rendered media entity.

Parameters

array $match: Array of preg matched items.

Return value

string The rendered media entity.

File

src/Plugin/Filter/MediaEntityBlockFilter.php, line 72

Class

MediaEntityBlockFilter
Class MediaEntityBlockFilter.

Namespace

Drupal\gutenberg\Plugin\Filter

Code

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";
}