You are here

private function BlockFilter::renderBlock in Gutenberg 8

Callback function to process each URL.

File

src/Plugin/Filter/BlockFilter.php, line 87

Class

BlockFilter
Class BlockFilter.

Namespace

Drupal\gutenberg\Plugin\Filter

Code

private function renderBlock($match) {
  $comment = $match[0];
  $attributes = json_decode($match[1]);
  $plugin_id = $attributes->blockId;

  // You can hard code configuration or you load from settings.
  $config = [];
  $plugin_block = $this->blocksRenderer
    ->getBlockFromPluginId($plugin_id, $config);
  $content = '';
  if (!empty($plugin_block)) {
    if ($this->blocksRenderer
      ->isAccessForbidden($plugin_block)) {
      return [
        '#type' => 'html_tag',
        '#tag' => 'h2',
        '#value' => $this
          ->t('Access required'),
      ];
    }
    $content = $this->blocksRenderer
      ->getRenderFromBlockPlugin($plugin_block);
  }

  // Render the css class if available.
  if (!empty($attributes->className)) {
    $prefixed = [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#value' => $content,
      '#attributes' => [
        'class' => [
          $attributes->className,
        ],
      ],
    ];
    $content = $this->renderer
      ->render($prefixed);
  }
  return $comment . $content;
}