You are here

public function BlocksController::loadById in Gutenberg 8.2

Same name and namespace in other branches
  1. 8 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'
gutenberg.routing.yml in ./gutenberg.routing.yml
gutenberg.routing.yml

File

src/Controller/BlocksController.php, line 109

Class

BlocksController
Returns responses for our blocks routes.

Namespace

Drupal\gutenberg\Controller

Code

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 ($plugin_block) {
    $access_result = $this->blocksRenderer
      ->getBlockAccess($plugin_block);
    if ($access_result
      ->isForbidden()) {

      // You might need to add some cache tags/contexts.
      return new JsonResponse([
        'html' => $this
          ->t('Access to this block is denied.'),
      ]);
    }
    $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('Unable to render the content possibly due to path restrictions.');
  }
  return new JsonResponse([
    'html' => $content,
  ]);
}