diff --git a/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php b/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
index 153bc4db99..361e13c6cb 100644
--- a/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
+++ b/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
@@ -300,10 +300,22 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
],
];
+ // When the list of allowed types in the field configuration is null or
+ // empty, ::getAllowedMediaTypeIdsSorted() returns all existing media types.
+ // When there are no media types, we show a message to users and ask them to
+ // create a media type if they have permissions.
$allowed_media_type_ids = $this->getAllowedMediaTypeIdsSorted();
if (!$allowed_media_type_ids) {
+ if ($this->accessManager->checkNamedRoute('entity.media_type.add_form')) {
+ $empty_message = $this->t('There are no media types available. Please add a media type and enable it for this field.', [
+ ':url' => Url::fromRoute('entity.media_type.add_form')->toString(),
+ ]);
+ }
+ else {
+ $empty_message = $this->t('There are no media types available. Please contact the site administrator.'),
+ }
$element['empty_types'] = [
- '#markup' => $this->accessManager->checkNamedRoute('entity.media_type.add_form') ? $this->t('There are no media types available. Please add a media type and enable it for this field.', [':url' => Url::fromRoute('entity.media_type.add_form')->toString()]) : $this->t('There are no media types available. Please contact the site administrator.'),
+ '#markup' => $empty_message,
];
return $element;
}