public override async Task <IDisplayResult> UpdateAsync(MediaField field, IUpdateModel updater, UpdateFieldEditorContext context) { var model = new EditMediaFieldViewModel(); if (await updater.TryUpdateModelAsync(model, Prefix, f => f.Paths)) { // Deserializing an empty string doesn't return an array var items = string.IsNullOrWhiteSpace(model.Paths) ? new List <EditMediaFieldItemInfo>() : JsonConvert.DeserializeObject <EditMediaFieldItemInfo[]>(model.Paths, Settings).ToList(); // If it's an attached media field editor the files are automatically handled by _attachedMediaFieldFileService if (string.Equals(context.PartFieldDefinition.Editor(), "Attached", StringComparison.OrdinalIgnoreCase)) { try { await _attachedMediaFieldFileService.HandleFilesOnFieldUpdateAsync(items, context.ContentPart.ContentItem); } catch (Exception e) { updater.ModelState.AddModelError(Prefix, nameof(model.Paths), S["{0}: There was an error handling the files.", context.PartFieldDefinition.DisplayName()]); _logger.LogError(e, "Error handling attached media files for field '{Field}'", context.PartFieldDefinition.DisplayName()); } } field.Paths = items.Where(p => !p.IsRemoved).Select(p => p.Path).ToArray() ?? new string[] { }; var settings = context.PartFieldDefinition.GetSettings <MediaFieldSettings>(); if (settings.Required && field.Paths.Length < 1) { updater.ModelState.AddModelError(Prefix, nameof(model.Paths), S["{0}: A media is required.", context.PartFieldDefinition.DisplayName()]); } if (field.Paths.Length > 1 && !settings.Multiple) { updater.ModelState.AddModelError(Prefix, nameof(model.Paths), S["{0}: Selecting multiple media is forbidden.", context.PartFieldDefinition.DisplayName()]); } if (settings.AllowMediaText) { field.MediaTexts = items.Select(t => t.MediaText).ToArray(); } else { field.MediaTexts = Array.Empty <string>(); } if (settings.AllowAnchors) { field.SetAnchors(items.Select(t => t.Anchor).ToArray()); } else if (field.Content.ContainsKey("Anchors")) // Less well known properties should be self healing. { field.Content.Remove("Anchors"); } } return(Edit(field, context)); }
public override async Task <IDisplayResult> UpdateAsync(MediaField field, IUpdateModel updater, UpdateFieldEditorContext context) { var model = new EditMediaFieldViewModel(); if (await updater.TryUpdateModelAsync(model, Prefix, f => f.Paths)) { // Deserializing an empty string doesn't return an array var items = string.IsNullOrWhiteSpace(model.Paths) ? new List <EditMediaFieldItemInfo>() : JsonConvert.DeserializeObject <EditMediaFieldItemInfo[]>(model.Paths).ToList(); // If it's an attached media field editor the files are automatically handled by _attachedMediaFieldFileService if (string.Equals(context.PartFieldDefinition.Editor(), "Attached", StringComparison.OrdinalIgnoreCase)) { try { await _attachedMediaFieldFileService.HandleFilesOnFieldUpdateAsync(items, context.ContentPart.ContentItem); } catch (Exception) { updater.ModelState.AddModelError(Prefix, S["{0}: There was an error handling the files.", context.PartFieldDefinition.DisplayName()]); } } field.Paths = items.Where(p => !p.IsRemoved).Select(p => p.Path).ToArray() ?? new string[] {}; var settings = context.PartFieldDefinition.Settings.ToObject <MediaFieldSettings>(); if (settings.Required && field.Paths.Length < 1) { updater.ModelState.AddModelError(Prefix, S["{0}: A media is required.", context.PartFieldDefinition.DisplayName()]); } if (field.Paths.Length > 1 && !settings.Multiple) { updater.ModelState.AddModelError(Prefix, S["{0}: Selecting multiple media is forbidden.", context.PartFieldDefinition.DisplayName()]); } } return(Edit(field, context)); }