You are here

public function DrupalBlockProcessor::processBlock in Gutenberg 8.2

Process the Gutenberg block and its content.

The content and block can be manipulated here. Return FALSE to ensure that no other plugins are ran after this instance.

Parameters

array $block: The block object.

string $block_content: The inner block content.

\Drupal\Core\Cache\RefinableCacheableDependencyInterface $bubbleable_metadata: The bubbleable metadata.

Return value

bool|null Return FALSE if further processing should be stopped.

Overrides GutenbergBlockProcessorInterface::processBlock

File

src/BlockProcessor/DrupalBlockProcessor.php, line 53

Class

DrupalBlockProcessor
Processes Drupal blocks than can be embedded.

Namespace

Drupal\gutenberg\BlockProcessor

Code

public function processBlock(array &$block, &$block_content, RefinableCacheableDependencyInterface $bubbleable_metadata) {
  $block_attributes = $block['attrs'];

  /* TODO: Check if the block is currently in the "allowed" list?.
   *  Don't think this is possible in the backend as the content has no
   *  direct reference to the parent entity. Might have to be handled in the
   *  Gutenberg editor on load.
   */
  $plugin_id = $block_attributes['blockId'];

  // TODO: load the config from the block attributes.
  $config = $block_attributes['config'] ?? [];
  $plugin_block = $this->blocksRenderer
    ->getBlockFromPluginId($plugin_id, $config);
  if ($plugin_block) {
    $access_result = $this->blocksRenderer
      ->getBlockAccess($plugin_block);
    $bubbleable_metadata
      ->addCacheableDependency($access_result);
    if ($access_result
      ->isForbidden()) {

      /*
       * Add as a comment in the HTML.
       * Fixme: Is this a good idea (as we're exposing backend info to the
       *  frontend)? I'm leaning towards removing it.
       */
      $render_content = [
        '#prefix' => Markup::create('<!-- '),
        '#markup' => $this
          ->t('Block access denied: @plugin_id', [
          '@plugin_id' => $plugin_id,
        ]),
        '#suffix' => Markup::create(' -->'),
      ];
    }
    else {
      $render_content = $this->blocksRenderer
        ->getRenderFromBlockPlugin($plugin_block, FALSE);
    }
    $render = [
      'content' => $render_content,
    ];

    // Render the css class if available.
    if (!empty($block_attributes['className'])) {
      $render['#prefix'] = sprintf('<div class="%s">', Html::escape($block_attributes['className']));
      $render['#suffix'] = '</div>';
    }
    $block_content = $this->renderer
      ->render($render);
    $bubbleable_metadata
      ->addCacheableDependency(CacheableMetadata::createFromRenderArray($render));
  }
}