From b7e43a8def6e476c24ba5bfd93be7d7b5a4cf4d4 Mon Sep 17 00:00:00 2001 From: Ronald Barendse Date: Tue, 23 Jan 2024 16:08:17 +0100 Subject: [PATCH] Get references from macro parameters using IDataValueReferenceFactory (#15625) --- .../Templates/HtmlMacroParameterParser.cs | 52 ++++++++----------- 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/src/Umbraco.Infrastructure/Templates/HtmlMacroParameterParser.cs b/src/Umbraco.Infrastructure/Templates/HtmlMacroParameterParser.cs index 721e99bbd7..409c02ed02 100644 --- a/src/Umbraco.Infrastructure/Templates/HtmlMacroParameterParser.cs +++ b/src/Umbraco.Infrastructure/Templates/HtmlMacroParameterParser.cs @@ -1,3 +1,4 @@ +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Umbraco.Cms.Core; @@ -6,6 +7,7 @@ using Umbraco.Cms.Core.Models.Editors; using Umbraco.Cms.Core.PropertyEditors; using Umbraco.Cms.Core.Services; using Umbraco.Cms.Infrastructure.Macros; +using Umbraco.Cms.Web.Common.DependencyInjection; using Umbraco.Extensions; namespace Umbraco.Cms.Infrastructure.Templates; @@ -15,12 +17,23 @@ public sealed class HtmlMacroParameterParser : IHtmlMacroParameterParser private readonly ILogger _logger; private readonly IMacroService _macroService; private readonly ParameterEditorCollection _parameterEditors; + private readonly DataValueReferenceFactoryCollection _dataValueReferenceFactories; + [Obsolete("Use the non-obsolete overload instead, scheduled for removal in v14")] public HtmlMacroParameterParser(IMacroService macroService, ILogger logger, ParameterEditorCollection parameterEditors) + : this( + macroService, + logger, + parameterEditors, + StaticServiceProvider.Instance.GetRequiredService()) + { } + + public HtmlMacroParameterParser(IMacroService macroService, ILogger logger, ParameterEditorCollection parameterEditors, DataValueReferenceFactoryCollection dataValueReferenceFactories) { _macroService = macroService; _logger = logger; _parameterEditors = parameterEditors; + _dataValueReferenceFactories = dataValueReferenceFactories; } /// @@ -41,6 +54,7 @@ public sealed class HtmlMacroParameterParser : IHtmlMacroParameterParser (macroAlias, macroAttributes) => foundMacros.Add(new Tuple>( macroAlias, new Dictionary(macroAttributes, StringComparer.OrdinalIgnoreCase)))); + foreach (UmbracoEntityReference umbracoEntityReference in GetUmbracoEntityReferencesFromMacros(foundMacros)) { yield return umbracoEntityReference; @@ -52,8 +66,7 @@ public sealed class HtmlMacroParameterParser : IHtmlMacroParameterParser /// /// /// - public IEnumerable FindUmbracoEntityReferencesFromGridControlMacros( - IEnumerable macroGridControls) + public IEnumerable FindUmbracoEntityReferencesFromGridControlMacros(IEnumerable macroGridControls) { var foundMacros = new List>>(); @@ -65,8 +78,7 @@ public sealed class HtmlMacroParameterParser : IHtmlMacroParameterParser // Collect any macro parameters that contain the media udi format if (gridMacro is not null && gridMacro.MacroParameters is not null && gridMacro.MacroParameters.Any()) { - foundMacros.Add( - new Tuple>(gridMacro.MacroAlias, gridMacro.MacroParameters)); + foundMacros.Add(new Tuple>(gridMacro.MacroAlias, gridMacro.MacroParameters)); } } @@ -101,14 +113,12 @@ public sealed class HtmlMacroParameterParser : IHtmlMacroParameterParser continue; } - foundMacroUmbracoEntityReferences.Add( - new UmbracoEntityReference(Udi.Create(Constants.UdiEntityType.Macro, macroConfig.Key))); + foundMacroUmbracoEntityReferences.Add(new UmbracoEntityReference(Udi.Create(Constants.UdiEntityType.Macro, macroConfig.Key))); // Only do this if the macros actually have parameters if (macroConfig.Properties.Keys.Any(f => f != "macroAlias")) { - foreach (UmbracoEntityReference umbracoEntityReference in GetUmbracoEntityReferencesFromMacroParameters( - macro.Item2, macroConfig, _parameterEditors)) + foreach (UmbracoEntityReference umbracoEntityReference in GetUmbracoEntityReferencesFromMacroParameters(macro.Item2, macroConfig, _parameterEditors)) { yield return umbracoEntityReference; } @@ -130,41 +140,23 @@ public sealed class HtmlMacroParameterParser : IHtmlMacroParameterParser /// look up the corresponding property editor for a macro parameter /// /// - private IEnumerable GetUmbracoEntityReferencesFromMacroParameters( - Dictionary macroParameters, IMacro macroConfig, ParameterEditorCollection parameterEditors) + private IEnumerable GetUmbracoEntityReferencesFromMacroParameters(Dictionary macroParameters, IMacro macroConfig, ParameterEditorCollection parameterEditors) { - var foundUmbracoEntityReferences = new List(); foreach (IMacroProperty parameter in macroConfig.Properties) { if (macroParameters.TryGetValue(parameter.Alias, out var parameterValue)) { var parameterEditorAlias = parameter.EditorAlias; - - // Lookup propertyEditor from the registered ParameterEditors with the implmementation to avoid looking up for each parameter - IDataEditor? parameterEditor = parameterEditors.FirstOrDefault(f => - string.Equals(f.Alias, parameterEditorAlias, StringComparison.OrdinalIgnoreCase)); + IDataEditor? parameterEditor = parameterEditors.FirstOrDefault(f => string.Equals(f.Alias, parameterEditorAlias, StringComparison.OrdinalIgnoreCase)); if (parameterEditor is not null) { - // Get the ParameterValueEditor for this PropertyEditor (where the GetReferences method is implemented) - cast as IDataValueReference to determine if 'it is' implemented for the editor - if (parameterEditor.GetValueEditor() is IDataValueReference parameterValueEditor) + foreach (UmbracoEntityReference entityReference in _dataValueReferenceFactories.GetReferences(parameterEditor, parameterValue)) { - foreach (UmbracoEntityReference entityReference in parameterValueEditor.GetReferences( - parameterValue)) - { - foundUmbracoEntityReferences.Add(entityReference); - } - } - else - { - _logger.LogInformation( - "{0} doesn't have a ValueEditor that implements IDataValueReference", - parameterEditor.Alias); + yield return entityReference; } } } } - - return foundUmbracoEntityReferences; } // Poco class to deserialise the Json for a Macro Control