DrupalBlockProcessor.php in Gutenberg 8.2
File
src/BlockProcessor/DrupalBlockProcessor.php
View source
<?php
namespace Drupal\gutenberg\BlockProcessor;
use Drupal\Component\Utility\Html;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\gutenberg\BlocksRendererHelper;
class DrupalBlockProcessor implements GutenbergBlockProcessorInterface {
use StringTranslationTrait;
protected $blocksRenderer;
protected $renderer;
public function __construct(BlocksRendererHelper $blocks_renderer, RendererInterface $renderer) {
$this->blocksRenderer = $blocks_renderer;
$this->renderer = $renderer;
}
public function processBlock(array &$block, &$block_content, RefinableCacheableDependencyInterface $bubbleable_metadata) {
$block_attributes = $block['attrs'];
$plugin_id = $block_attributes['blockId'];
$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()) {
$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,
];
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));
}
}
public function isSupported(array $block, $block_content = '') {
return substr($block['blockName'], 0, 12) === 'drupalblock/';
}
}