public function BlocksController::loadById in Gutenberg 8
Same name and namespace in other branches
- 8.2 src/Controller/BlocksController.php \Drupal\gutenberg\Controller\BlocksController::loadById()
Returns JSON representing the loaded blocks.
Parameters
string $plugin_id: Plugin ID.
Return value
\Symfony\Component\HttpFoundation\JsonResponse The JSON response.
1 string reference to 'BlocksController::loadById'
File
- src/
Controller/ BlocksController.php, line 109
Class
- BlocksController
- Returns responses for our blocks routes.
Namespace
Drupal\gutenberg\ControllerCode
public function loadById($plugin_id) {
/* TODO: We can get a specific instance/derivative of block and load it's config */
$config = [];
$plugin_block = $this->blocksRenderer
->getBlockFromPluginId($plugin_id, $config);
$content = '';
if (!empty($plugin_block)) {
if ($this->blocksRenderer
->isAccessForbidden($plugin_block)) {
// You might need to add some cache tags/contexts.
return new JsonResponse([
'html' => $this
->t('No access.'),
]);
}
$content = $this->blocksRenderer
->getRenderFromBlockPlugin($plugin_block);
}
// If the block is a view with contexts defined, it may
// not render on the editor because of, for example, the
// node path. Let's just write some warning if no content.
if ($content === '') {
$content = $this
->t('Not able to render the content on editor possibly due to path restrictions.');
}
return new JsonResponse([
'html' => $content,
]);
}