Skip to content

Commit

Permalink
Fix reference field types logic to default to webpages rather than co…
Browse files Browse the repository at this point in the history
…ntent hub
  • Loading branch information
Dmitry Bastron committed Apr 2, 2024
1 parent 9a5d737 commit 6eb2ee0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 105 deletions.
4 changes: 2 additions & 2 deletions UMT.Sitecore/Configuration/ContentMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
81 changes: 20 additions & 61 deletions UMT.Sitecore/Converters/MultipleReferencesFieldTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -65,37 +33,28 @@ public override TargetFieldValue Convert(Field field, Item item)
result.References = new List<ContentItemReference>();
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<ContentItemReference>
{
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<ContentItemReference>
{
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);
Expand Down
86 changes: 44 additions & 42 deletions UMT.Sitecore/Converters/ReferenceFieldTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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<IReferenceField>();
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<ContentItemReference>
{
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<IReferenceField>();
if (isContentHubItem)
{
fieldValue.Add(new ContentItemReferenceField(referenceField.TargetItem.ID.Guid));

result.References = new List<ContentItemReference>
{
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;
}
}
Expand Down

0 comments on commit 6eb2ee0

Please sign in to comment.