You are here

public function CKEditor5MediaController::mediaEntityMetadata in Drupal 10

Returns JSON response containing metadata about media entity.

Parameters

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

Return value

\Symfony\Component\HttpFoundation\JsonResponse A JSON object including the response.

Throws

\Symfony\Component\HttpKernel\Exception\BadRequestHttpException Thrown when no media UUID is provided.

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException Thrown when no media with the provided UUID exists.

1 string reference to 'CKEditor5MediaController::mediaEntityMetadata'
ckeditor5.routing.yml in core/modules/ckeditor5/ckeditor5.routing.yml
core/modules/ckeditor5/ckeditor5.routing.yml

File

core/modules/ckeditor5/src/Controller/CKEditor5MediaController.php, line 93

Class

CKEditor5MediaController
Provides an API for checking if a media entity has image field.

Namespace

Drupal\ckeditor5\Controller

Code

public function mediaEntityMetadata(Request $request) {
  $uuid = $request->query
    ->get('uuid');
  if (!$uuid || !Uuid::isValid($uuid)) {
    throw new BadRequestHttpException();
  }

  // Access is enforced on route level.
  // @see \Drupal\ckeditor5\Controller\CKEditor5MediaController::access().
  if (!($media = $this->entityRepository
    ->loadEntityByUuid('media', $uuid))) {
    throw new NotFoundHttpException();
  }
  $image_field = $this
    ->getMediaImageSourceFieldName($media);
  $response = [];
  $response['type'] = $media
    ->bundle();
  if ($image_field) {
    $response['imageSourceMetadata'] = [
      'alt' => $this->entityRepository
        ->getTranslationFromContext($media)->{$image_field}->alt,
    ];
  }

  // Note that we intentionally do not use:
  // - \Drupal\Core\Cache\CacheableResponse because caching it on the server
  //   side is wasteful, hence there is no need for cacheability metadata.
  // - \Drupal\Core\Render\HtmlResponse because there is no need for
  //   attachments nor cacheability metadata.
  return (new JsonResponse($response, 200))
    ->setPrivate()
    ->setMaxAge(300);
}