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,
];