From 6d9af75fbedda66f36c9782c66d126366019e501 Mon Sep 17 00:00:00 2001 From: Shannon Date: Wed, 2 Oct 2019 13:49:50 +0200 Subject: [PATCH] Fixes the media refs within the RTE in the Grid --- .../PropertyEditors/GridPropertyEditor.cs | 50 ++++++++++++++++--- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/src/Umbraco.Web/PropertyEditors/GridPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/GridPropertyEditor.cs index f940ed5c15..719377ca72 100644 --- a/src/Umbraco.Web/PropertyEditors/GridPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/GridPropertyEditor.cs @@ -1,5 +1,6 @@ using Newtonsoft.Json; using System; +using System.Collections.Generic; using System.Linq; using Umbraco.Core; using Umbraco.Core.Logging; @@ -83,12 +84,9 @@ namespace Umbraco.Web.PropertyEditors // editorValue.Value is a JSON string of the grid var rawJson = editorValue.Value.ToString(); - var grid = JsonConvert.DeserializeObject(rawJson); - - // Find all controls that use the RTE editor - var controls = grid.Sections.SelectMany(x => x.Rows.SelectMany(r => r.Areas).SelectMany(a => a.Controls)); - var rtes = controls.Where(x => x.Editor.Alias.ToLowerInvariant() == "rte"); + var grid = DeserializeGridValue(rawJson, out var rtes); + //process the rte values foreach(var rte in rtes) { // Parse the HTML @@ -96,13 +94,53 @@ namespace Umbraco.Web.PropertyEditors var userId = _umbracoContextAccessor.UmbracoContext?.Security.CurrentUser.Id ?? Constants.Security.SuperUserId; - var parsedHtml = TemplateUtilities.FindAndPersistPastedTempImages(html, mediaParentId, userId, _mediaService, _contentTypeBaseServiceProvider, _logger); + var editorValueWithMediaUrlsRemoved = TemplateUtilities.RemoveMediaUrlsFromTextString(html); + + var parsedHtml = TemplateUtilities.FindAndPersistPastedTempImages(editorValueWithMediaUrlsRemoved, mediaParentId, userId, _mediaService, _contentTypeBaseServiceProvider, _logger); rte.Value = parsedHtml; } // Convert back to raw JSON for persisting return JsonConvert.SerializeObject(grid); } + + /// + /// Ensures that the rich text editor values are processed within the grid + /// + /// + /// + /// + /// + /// + public override object ToEditor(Property property, IDataTypeService dataTypeService, string culture = null, string segment = null) + { + var val = property.GetValue(culture, segment); + if (val == null) return string.Empty; + + var grid = DeserializeGridValue(val.ToString(), out var rtes); + + //process the rte values + foreach (var rte in rtes.ToList()) + { + var html = rte.Value?.ToString(); + + var propertyValueWithMediaResolved = TemplateUtilities.ResolveMediaFromTextString(html); + rte.Value = propertyValueWithMediaResolved; + } + + return grid; + } + + private GridValue DeserializeGridValue(string rawJson, out IEnumerable richTextValues) + { + var grid = JsonConvert.DeserializeObject(rawJson); + + // Find all controls that use the RTE editor + var controls = grid.Sections.SelectMany(x => x.Rows.SelectMany(r => r.Areas).SelectMany(a => a.Controls)); + richTextValues = controls.Where(x => x.Editor.Alias.ToLowerInvariant() == "rte"); + + return grid; + } } } }