You are here

public function ReusableBlocksController::save in Gutenberg 8

Same name and namespace in other branches
  1. 8.2 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.

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) {
  if ($block_id && $block_id > 0) {
    $data = json_decode($request
      ->getContent(), TRUE);
    $block = BlockContent::load($block_id);
    $block->body->value = $data['content'];
    $block->info->value = $data['title'];
  }
  else {
    $params = $request->request
      ->all();
    $block = BlockContent::create([
      'info' => $params['title'],
      'type' => 'reusable_block',
      'body' => [
        'value' => $params['content'],
        'format' => 'full_html',
      ],
    ]);
  }
  $block
    ->save();
  $headers = [
    'Allow' => 'GET, POST',
    'Access-Control-Allow-Methods' => 'OPTIONS, GET, POST, PUT, PATCH, DELETE',
  ];
  return new JsonResponse([
    'id' => (int) $block
      ->id(),
    'title' => [
      'raw' => $block->info->value,
    ],
    'content' => [
      'block_version' => 1,
      'protected' => FALSE,
      'raw' => $block->body->value,
    ],
    'slug' => 'reusable_block_' . $block
      ->id(),
    'type' => 'wp_block',
    'status' => 'publish',
  ], $block_id && $block_id > 0 ? 200 : 201, $headers);
}