Added flag to display a single model error for MediaLibraryPickerField (#8700)

* Added flag to display model error by LocalizationExtensionHandler if media have been removed from the field only. This ensures that no duplicate model error has displayed.

* Restored formatting for else branches

---------

Co-authored-by: Matteo Piovanelli <MatteoPiovanelli-Laser@users.noreply.github.com>
This commit is contained in:
Andrea Piovanelli
2023-06-30 08:49:20 +02:00
committed by GitHub
co-authored by Matteo Piovanelli
parent 0485e44378
commit 0b001c76c2
@@ -45,6 +45,12 @@ namespace Orchard.MediaLibrary.Handlers {
//try to replace items in the field with their translation //try to replace items in the field with their translation
var itemsInField = _contentManager.GetMany<ContentItem>(field.Ids, VersionOptions.Latest, QueryHints.Empty); var itemsInField = _contentManager.GetMany<ContentItem>(field.Ids, VersionOptions.Latest, QueryHints.Empty);
var mediaIds = new List<int>(); var mediaIds = new List<int>();
// Flag to check if media have been removed from the field.
// This happens when the field is set to remove items without localization and one or more media items cannot be localized.
// This flag is used to display a model error when the field is required and every media item has been removed from it.
var mediaRemoved = false;
foreach (var item in itemsInField) { foreach (var item in itemsInField) {
// negatives id whoud be localized // negatives id whoud be localized
var mediaItem = _contentManager.Get(item.Id, VersionOptions.Latest); var mediaItem = _contentManager.Get(item.Id, VersionOptions.Latest);
@@ -55,7 +61,7 @@ namespace Orchard.MediaLibrary.Handlers {
if (contentCulture == mediaCulture) { if (contentCulture == mediaCulture) {
// The content culture and the media culture match // The content culture and the media culture match
mediaIds.Add(mediaItem.Id); mediaIds.Add(mediaItem.Id);
} }
else { else {
if (mediaCulture == null) { if (mediaCulture == null) {
// The media has not a culture, so it takes the content culture // The media has not a culture, so it takes the content culture
@@ -65,7 +71,7 @@ namespace Orchard.MediaLibrary.Handlers {
"{0}: the media item {1} was culture neutral and it has been localized", "{0}: the media item {1} was culture neutral and it has been localized",
field.DisplayName, field.DisplayName,
mediaItem.As<MediaPart>().FileName)); mediaItem.As<MediaPart>().FileName));
} }
else { else {
// The media has a culture // The media has a culture
var localizedMedia = _localizationServices.GetLocalizedContentItem(mediaItem, contentCulture); var localizedMedia = _localizationServices.GetLocalizedContentItem(mediaItem, contentCulture);
@@ -76,7 +82,7 @@ namespace Orchard.MediaLibrary.Handlers {
"{0}: the media item {1} has been replaced by its localized version", "{0}: the media item {1} has been replaced by its localized version",
field.DisplayName, field.DisplayName,
mediaItem.As<MediaPart>().FileName)); mediaItem.As<MediaPart>().FileName));
} }
else { else {
if (!settings.RemoveItemsWithoutLocalization) { if (!settings.RemoveItemsWithoutLocalization) {
// The media supports translations but have not a localized version, so it will be cloned in the right language // The media supports translations but have not a localized version, so it will be cloned in the right language
@@ -92,33 +98,35 @@ namespace Orchard.MediaLibrary.Handlers {
"{0}: a localized version of media item {1} has been created", "{0}: a localized version of media item {1} has been created",
field.DisplayName, field.DisplayName,
mediaItem.As<MediaPart>().FileName)); mediaItem.As<MediaPart>().FileName));
} }
else { else {
_orchardServices.Notifier.Warning(T( _orchardServices.Notifier.Warning(T(
"{0}: the media item {1} has been removed from the field because its culture differs from content's culture", "{0}: the media item {1} has been removed from the field because its culture differs from content's culture",
field.DisplayName, field.DisplayName,
mediaItem.As<MediaPart>().FileName)); mediaItem.As<MediaPart>().FileName));
mediaRemoved = true;
} }
} }
} }
} }
} }
else if (mediaItem != null && !mediaIsLocalizable) { else if (mediaItem != null && !mediaIsLocalizable) {
if (!settings.RemoveItemsWithNoLocalizationPart) { if (!settings.RemoveItemsWithNoLocalizationPart) {
mediaIds.Add(mediaItem.Id); mediaIds.Add(mediaItem.Id);
} }
else { else {
_orchardServices.Notifier.Warning(T( _orchardServices.Notifier.Warning(T(
"{0}: the media item {1} has been removed from the field because culture neutral", "{0}: the media item {1} has been removed from the field because culture neutral",
field.DisplayName, field.DisplayName,
mediaItem.As<MediaPart>().FileName)); mediaItem.As<MediaPart>().FileName));
mediaRemoved = true;
} }
} }
} }
field.Ids = mediaIds.Distinct().ToArray(); field.Ids = mediaIds.Distinct().ToArray();
if (field.Ids.Length == 0 && fieldSettings.Required) { if (field.Ids.Length == 0 && fieldSettings.Required && mediaRemoved) {
context.Updater.AddModelError("Id", T("The {0} field is required.", field.DisplayName)); context.Updater.AddModelError("Id", T("The {0} field is required.", field.DisplayName));
} }
} }
@@ -126,4 +134,4 @@ namespace Orchard.MediaLibrary.Handlers {
} }
} }
} }
} }