You are here

public function ReusableBlocksController::save in Gutenberg 8.2

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

Saves reusable block.

Parameters

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

string $block_id: The reusable block id.

Return value

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

Throws

\Drupal\Core\Entity\EntityStorageException

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

File

src/Controller/ReusableBlocksController.php, line 83

Class

ReusableBlocksController
Returns responses for our blocks routes.

Namespace

Drupal\gutenberg\Controller

Code

public function save(Request $request, $block_id = NULL) {
  $updating_block = $block_id && $block_id > 0;

  // Cast to string to avoid null pointer when the title or content is empty.
  $title = (string) $request->request
    ->get('title');
  $content = (string) $request->request
    ->get('content');
  if ($updating_block) {
    $block = $this
      ->loadBlockOrThrow($block_id);
    $block
      ->set('info', $title);
    $block
      ->set('body', $content);
  }
  else {
    $block = BlockContent::create([
      'info' => $title,
      'type' => 'reusable_block',
      'body' => [
        'value' => $content,
        'format' => 'full_html',
      ],
    ]);
  }
  $block
    ->save();
  $headers = [
    'Allow' => 'GET, POST',
    'Access-Control-Allow-Methods' => 'OPTIONS, GET, POST, PUT, PATCH, DELETE',
  ];
  if ($updating_block) {
    $response_code = Response::HTTP_OK;
  }
  else {
    $response_code = Response::HTTP_CREATED;
  }
  return new JsonResponse($this
    ->getBlockAttributes($block), $response_code, $headers);
}