function CKEditor5MediaController::mediaEntityMetadata

Same name in other branches
  1. 9 core/modules/ckeditor5/src/Controller/CKEditor5MediaController.php \Drupal\ckeditor5\Controller\CKEditor5MediaController::mediaEntityMetadata()
  2. 11.x core/modules/ckeditor5/src/Controller/CKEditor5MediaController.php \Drupal\ckeditor5\Controller\CKEditor5MediaController::mediaEntityMetadata()

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 81

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 this uses the image media source and the "alt" field is enabled,
    // expose additional metadata.
    // @see \Drupal\media\Plugin\media\Source\Image
    // @see core/modules/ckeditor5/js/ckeditor5_plugins/drupalMedia/src/mediaimagetextalternative/mediaimagetextalternativeui.js
    if ($image_field) {
        $settings = $media->{$image_field}
            ->getItemDefinition()
            ->getSettings();
        if (!empty($settings['alt_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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.