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 558dbfca35..1d178b80a0 100644 --- a/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php +++ b/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php @@ -791,15 +791,15 @@ public static function validateMediaWidget(array $element, FormStateInterface $f // level (the validation in this method). The field is then additionally // validated by rules applied at the parent form level. // The validation from the parent form can occur before uploaded media is - // added to the field. If the field is required, this can result in a - // validation error due to the parent form checking for the presence of - // required media before it's been transferred to the field. - // To avoid this error, newly added media is added to the field prior to - // parent form validation. - if (count($field_state['items']) === 0 && !empty($media) && !empty($element['#required'])) { - $field_state['items'] = array_filter(array_map(function ($media_item) { + // added to the field. This can result in a validation error due to the + // parent form assuming the presence of media before it's been transferred + // to the field. To avoid this error, newly added media is added to the + // field prior to parent form validation. + if (count($field_state['items']) === 0 && !empty($media)) { + $media_target_ids = array_map(function ($media_item) { return $media_item->access('view') ? ['target_id' => $media_item->id()] : NULL; - }, $media)); + }, $media); + $field_state['items'] = array_filter($media_target_ids); } static::setFieldState($element, $form_state, $field_state); }