You are here

public function ReusableBlocksController::load in Gutenberg 8.2

Same name and namespace in other branches
  1. 8 src/Controller/ReusableBlocksController.php \Drupal\gutenberg\Controller\ReusableBlocksController::load()

Returns JSON representing the loaded blocks.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request.

string $block_id: The reusable block id.

Return value

\Symfony\Component\HttpFoundation\JsonResponse The JSON response.

1 string reference to 'ReusableBlocksController::load'
gutenberg.routing.yml in ./gutenberg.routing.yml
gutenberg.routing.yml

File

src/Controller/ReusableBlocksController.php, line 32

Class

ReusableBlocksController
Returns responses for our blocks routes.

Namespace

Drupal\gutenberg\Controller

Code

public function load(Request $request, $block_id = NULL) {
  $headers = [
    'Allow' => 'GET, POST, PUT, PATCH, DELETE',
    'Access-Control-Allow-Methods' => 'OPTIONS, GET, POST, PUT, PATCH, DELETE',
    'Access-Control-Allow-Credentials' => 'true',
    'Access-Control-Allow-Headers' => 'Authorization, Content-Type',
  ];
  if ($block_id && $block_id > 0) {
    $block = $this
      ->loadBlockOrThrow($block_id);
    return new JsonResponse($this
      ->getBlockAttributes($block) + [
      // Kind of a hack but accepted by Gutenberg ;)
      'headers' => $headers,
    ], Response::HTTP_OK, $headers);
  }

  /*
   * TODO this should probably be paginated and lazy loaded in case a site
   *  has hundreds of reusable blocks.
   */
  $ids = \Drupal::entityQuery('block_content')
    ->condition('type', 'reusable_block')
    ->execute();
  $blocks = BlockContent::loadMultiple($ids);
  $result = [];

  /** @var \Drupal\block_content\BlockContentInterface[] $blocks */
  foreach ($blocks as $key => $block) {
    $result[] = $this
      ->getBlockAttributes($block);
  }
  return new JsonResponse($result, Response::HTTP_OK, $headers);
}