From 6eb2ee073ea493c49078f805e9847b776f6e5f9b Mon Sep 17 00:00:00 2001 From: Dmitry Bastron Date: Tue, 2 Apr 2024 16:30:08 +0200 Subject: [PATCH] Fix reference field types logic to default to webpages rather than content hub --- UMT.Sitecore/Configuration/ContentMapping.cs | 4 +- .../MultipleReferencesFieldTypeConverter.cs | 81 +++++------------ .../Converters/ReferenceFieldTypeConverter.cs | 86 ++++++++++--------- 3 files changed, 66 insertions(+), 105 deletions(-) diff --git a/UMT.Sitecore/Configuration/ContentMapping.cs b/UMT.Sitecore/Configuration/ContentMapping.cs index ff5a68e..eb65165 100644 --- a/UMT.Sitecore/Configuration/ContentMapping.cs +++ b/UMT.Sitecore/Configuration/ContentMapping.cs @@ -26,9 +26,9 @@ public void AddExcludedSubtree(XmlNode node) } } - public bool IsUnderPageRoot(string path) + public bool PathContainsAnyPageRoot(string path) { - return PageRoots.Any(x => path.StartsWith(x, StringComparison.OrdinalIgnoreCase)); + return !string.IsNullOrEmpty(path) && PageRoots.Any(x => path.IndexOf(x, StringComparison.OrdinalIgnoreCase) >= 0); } public bool ShouldBeExcluded(string path) diff --git a/UMT.Sitecore/Converters/MultipleReferencesFieldTypeConverter.cs b/UMT.Sitecore/Converters/MultipleReferencesFieldTypeConverter.cs index 1b8a598..1d365c2 100644 --- a/UMT.Sitecore/Converters/MultipleReferencesFieldTypeConverter.cs +++ b/UMT.Sitecore/Converters/MultipleReferencesFieldTypeConverter.cs @@ -14,43 +14,11 @@ namespace UMT.Sitecore.Converters { public class MultipleReferencesFieldTypeConverter : ReferenceFieldTypeConverter { - public override string GetColumnType(TemplateField field) - { - if (!string.IsNullOrEmpty(field.Source)) - { - var dataSource = StringUtil.ExtractParameter("DataSource", field.Source).Trim(); - var isUnderPageRoot = UMTConfiguration.ContentMapping.IsUnderPageRoot(dataSource); - if (isUnderPageRoot) - { - UMTLog.Info($"Reference field {field.Name} ({field.ID}) with Source=\"{field.Source}\" has been identified as web page reference."); - } - return isUnderPageRoot ? "webpages" : base.GetColumnType(field); - } - return DefaultColumnType; - } - public override DataClassFieldSettings GetFieldSettings(TemplateField field) { - var fieldSettings = new DataClassFieldSettings - { - ControlName = DefaultControlName, - Sortable = true - }; - if (!string.IsNullOrEmpty(field.Source)) - { - var dataSource = StringUtil.ExtractParameter("DataSource", field.Source).Trim(); - var isUnderPageRoot = UMTConfiguration.ContentMapping.IsUnderPageRoot(dataSource); - if (isUnderPageRoot) - { - fieldSettings.ControlName = "Kentico.Administration.WebPageSelector"; - fieldSettings.TreePath = dataSource.Substring("/sitecore/content".Length); - } - else - { - fieldSettings = base.GetFieldSettings(field); - fieldSettings.MaximumPages = 0; - } - } + var fieldSettings = base.GetFieldSettings(field); + fieldSettings.MaximumPages = 0; + return fieldSettings; } @@ -65,37 +33,28 @@ public override TargetFieldValue Convert(Field field, Item item) result.References = new List(); foreach (var linkedItem in linkedItems) { - var isUnderPageRoot = UMTConfiguration.ContentMapping.IsUnderPageRoot(linkedItem.Paths.FullPath); var isContentHubItem = UMTConfiguration.TemplateMapping.IsContentHubTemplate(linkedItem.TemplateID.Guid); - if (isUnderPageRoot && isContentHubItem) + + if (isContentHubItem) { - UMTLog.Warn($"Reference field {field.Name} ({field.ID}) contains a link to the item {linkedItem.Name} ({linkedItem.ID})," + - $"which is under one of page roots but configured as a Content Hub item." + - $"This item is skipped, check contentMapping/pageRoots and templateMapping/contentHubTemplates config sections."); + fieldValue.Add(new ContentItemReferenceField(linkedItem.ID.Guid)); + + result.References = new List + { + new ContentItemReference + { + ContentItemReferenceGUID = field.ID.Guid.GenerateDerivedGuid("ContentItemReference", + item.ID.Guid.ToString(), linkedItem.ID.Guid.ToString()), + ContentItemReferenceSourceCommonDataGuid = + item.ID.Guid.ToContentItemCommonDataGuid(item.Language.Name), + ContentItemReferenceTargetItemGuid = linkedItem.ID.Guid, + ContentItemReferenceGroupGUID = field.ID.Guid + } + }; } else { - if (isContentHubItem) - { - fieldValue.Add(new ContentItemReferenceField(linkedItem.ID.Guid)); - - result.References = new List - { - new ContentItemReference - { - ContentItemReferenceGUID = field.ID.Guid.GenerateDerivedGuid("ContentItemReference", - item.ID.Guid.ToString(), linkedItem.ID.Guid.ToString()), - ContentItemReferenceSourceCommonDataGuid = - item.ID.Guid.ToContentItemCommonDataGuid(item.Language.Name), - ContentItemReferenceTargetItemGuid = linkedItem.ID.Guid, - ContentItemReferenceGroupGUID = field.ID.Guid - } - }; - } - else - { - fieldValue.Add(new WebPageReferenceField(linkedItem.ID.Guid.ToWebPageItemGuid())); - } + fieldValue.Add(new WebPageReferenceField(linkedItem.ID.Guid.ToWebPageItemGuid())); } } result.Value = JsonConvert.SerializeObject(fieldValue); diff --git a/UMT.Sitecore/Converters/ReferenceFieldTypeConverter.cs b/UMT.Sitecore/Converters/ReferenceFieldTypeConverter.cs index 47abaa7..54c2936 100644 --- a/UMT.Sitecore/Converters/ReferenceFieldTypeConverter.cs +++ b/UMT.Sitecore/Converters/ReferenceFieldTypeConverter.cs @@ -20,15 +20,21 @@ public override string GetColumnType(TemplateField field) var columnType = DefaultColumnType; if (!string.IsNullOrEmpty(field.Source)) { - bool isUnderPageRoot; + bool isUnderPageRoot = true; if (ID.IsID(field.Source)) { var item = Factory.GetDatabase(UMTSettings.Database).GetItem(new ID(field.Source)); - isUnderPageRoot = item != null && UMTConfiguration.ContentMapping.IsUnderPageRoot(item.Paths.FullPath); + if (item != null && !UMTConfiguration.ContentMapping.PathContainsAnyPageRoot(item.Paths.FullPath)) + { + isUnderPageRoot = false; + } } else { - isUnderPageRoot = UMTConfiguration.ContentMapping.IsUnderPageRoot(field.Source.Replace("query:", "").Replace("fast:", "")); + if (!UMTConfiguration.ContentMapping.PathContainsAnyPageRoot(field.Source)) + { + isUnderPageRoot = false; + } } if (isUnderPageRoot) @@ -38,9 +44,9 @@ public override string GetColumnType(TemplateField field) } else { + columnType = "contentitemreference"; UMTLog.Info($"Reference field {field.Name} ({field.ID}) with Source=\"{field.Source}\" has been identified as content item reference."); } - } return columnType; } @@ -54,29 +60,31 @@ public override DataClassFieldSettings GetFieldSettings(TemplateField field) }; if (!string.IsNullOrEmpty(field.Source)) { - bool isUnderPageRoot; + bool isUnderPageRoot = true; if (ID.IsID(field.Source)) { var item = Factory.GetDatabase(UMTSettings.Database).GetItem(new ID(field.Source)); - isUnderPageRoot = item != null && UMTConfiguration.ContentMapping.IsUnderPageRoot(item.Paths.FullPath); - if (isUnderPageRoot) + if (item != null && !UMTConfiguration.ContentMapping.PathContainsAnyPageRoot(item.Paths.FullPath)) { - fieldSettings.TreePath = item.Paths.ContentPath; + isUnderPageRoot = false; } } else { - var sourcePath = field.Source.Replace("query:", "").Replace("fast:", ""); - isUnderPageRoot = UMTConfiguration.ContentMapping.IsUnderPageRoot(sourcePath); - if (isUnderPageRoot) + if (!UMTConfiguration.ContentMapping.PathContainsAnyPageRoot(field.Source)) { - fieldSettings.TreePath = sourcePath; + isUnderPageRoot = false; } } + if (isUnderPageRoot) { - fieldSettings.ControlName = "Kentico.Administration.WebPageSelector"; + fieldSettings.TreePath = "/"; } + + fieldSettings.ControlName = isUnderPageRoot + ? "Kentico.Administration.WebPageSelector" + : "Kentico.Administration.ContentItemSelector"; } return fieldSettings; } @@ -85,44 +93,38 @@ public override TargetFieldValue Convert(Field field, Item item) { var result = new TargetFieldValue(); var referenceField = (ReferenceField)field; + if (referenceField?.TargetItem != null) { - var isUnderPageRoot = UMTConfiguration.ContentMapping.IsUnderPageRoot(referenceField.TargetItem.Paths.FullPath); var isContentHubItem = UMTConfiguration.TemplateMapping.IsContentHubTemplate(referenceField.TargetItem.TemplateID.Guid); - if (isUnderPageRoot && isContentHubItem) + + var fieldValue = new List(); + if (isContentHubItem) { - UMTLog.Warn($"Reference field {field.Name} ({field.ID}) contains a link to the item {referenceField.TargetItem.Name} ({referenceField.TargetItem.ID})," + - $"which is under one of page roots but configured as a Content Hub item." + - $"This item is skipped, check contentMapping/pageRoots and templateMapping/contentHubTemplates config sections."); + fieldValue.Add(new ContentItemReferenceField(referenceField.TargetItem.ID.Guid)); + + result.References = new List + { + new ContentItemReference + { + ContentItemReferenceGUID = field.ID.Guid.GenerateDerivedGuid("ContentItemReference", + item.ID.Guid.ToString(), referenceField.TargetItem.ID.Guid.ToString()), + ContentItemReferenceSourceCommonDataGuid = + item.ID.Guid.ToContentItemCommonDataGuid(item.Language.Name), + ContentItemReferenceTargetItemGuid = referenceField.TargetItem.ID.Guid, + ContentItemReferenceGroupGUID = field.ID.Guid + } + }; } else { - var fieldValue = new List(); - if (isContentHubItem) - { - fieldValue.Add(new ContentItemReferenceField(referenceField.TargetItem.ID.Guid)); - - result.References = new List - { - new ContentItemReference - { - ContentItemReferenceGUID = field.ID.Guid.GenerateDerivedGuid("ContentItemReference", - item.ID.Guid.ToString(), referenceField.TargetItem.ID.Guid.ToString()), - ContentItemReferenceSourceCommonDataGuid = - item.ID.Guid.ToContentItemCommonDataGuid(item.Language.Name), - ContentItemReferenceTargetItemGuid = referenceField.TargetItem.ID.Guid, - ContentItemReferenceGroupGUID = field.ID.Guid - } - }; - } - else - { - fieldValue.Add(new WebPageReferenceField(referenceField.TargetItem.ID.Guid.ToWebPageItemGuid())); - } - - result.Value = JsonConvert.SerializeObject(fieldValue); + fieldValue.Add(new WebPageReferenceField(referenceField.TargetItem.ID.Guid.ToWebPageItemGuid())); } + + result.Value = JsonConvert.SerializeObject(fieldValue); + } + return result; } }