DynamicRenderProcessor.php in Gutenberg 8.2
File
src/BlockProcessor/DynamicRenderProcessor.php
View source
<?php
namespace Drupal\gutenberg\BlockProcessor;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Render\RendererInterface;
use Drupal\gutenberg\GutenbergLibraryManagerInterface;
class DynamicRenderProcessor implements GutenbergBlockProcessorInterface {
protected $libraryManager;
protected $renderer;
protected $dynamicBlocks;
protected $moduleHandler;
public function __construct(GutenbergLibraryManagerInterface $library_manager, RendererInterface $renderer, ModuleHandlerInterface $module_handler) {
$this->libraryManager = $library_manager;
$this->renderer = $renderer;
$this->moduleHandler = $module_handler;
}
public function processBlock(array &$block, &$block_content, RefinableCacheableDependencyInterface $bubbleable_metadata) {
$build = [
'#theme' => 'gutenberg_block',
'#block_name' => $block['blockName'],
'#block_attributes' => $block['attrs'],
'#block_content' => [
'#markup' => Markup::create($block_content),
],
'#pre_render' => [],
];
$block_name = str_replace('-', '_', $block['blockName']);
$block_parts = explode('/', $block_name);
$hooks = [
'gutenberg_block_view',
];
$base_hook = 'gutenberg_block_view__';
$hooks[] = $base_hook . $block_parts[0];
if (count($block_parts) === 2) {
$hooks[] = $base_hook . $block_parts[0] . '__' . $block_parts[1];
}
$this->moduleHandler
->alter($hooks, $build, $block_content);
$block_content = $this->renderer
->render($build);
$bubbleable_metadata
->addCacheableDependency(CacheableMetadata::createFromRenderArray($build));
}
public function isSupported(array $block, $block_content = '') {
return isset($this
->getDynamicBlockNames()[$block['blockName']]);
}
protected function getDynamicBlockNames() {
if ($this->dynamicBlocks === NULL) {
$this->dynamicBlocks = [];
foreach ($this->libraryManager
->getDefinitions() as $definition) {
foreach ($definition['dynamic-blocks'] as $block_name => $block_theme_definition) {
$this->dynamicBlocks[$block_name] = $block_name;
}
}
}
return $this->dynamicBlocks;
}
}