Update IContentTypeBase interface to account for group aliases and simplify code for backwards compatibility with group names

This commit is contained in:
Ronald Barendse
2021-07-14 12:02:49 +02:00
parent 8aebf5093b
commit d282a82481
4 changed files with 63 additions and 42 deletions

View File

@@ -304,14 +304,13 @@ namespace Umbraco.Core.Models
/// <inheritdoc />
public abstract bool AddPropertyGroup(string name, string alias);
/// <summary>
/// Adds a PropertyType to a specific PropertyGroup
/// </summary>
/// <param name="propertyType"><see cref="PropertyType"/> to add</param>
/// <param name="propertyGroupName">Name of the PropertyGroup to add the PropertyType to</param>
/// <returns>Returns <c>True</c> if PropertyType was added, otherwise <c>False</c></returns>
/// <inheritdoc />
[Obsolete("Use AddPropertyType(propertyType, groupAlias, groupName) instead to explicitly set the alias of the group (note the slighty different parameter order).")]
public abstract bool AddPropertyType(PropertyType propertyType, string propertyGroupName);
/// <inheritdoc />
public abstract bool AddPropertyType(PropertyType propertyType, string groupAlias, string groupName);
/// <summary>
/// Adds a PropertyType, which does not belong to a PropertyGroup.
/// </summary>
@@ -343,15 +342,18 @@ namespace Umbraco.Core.Models
if (propertyType == null) return false;
// get new group, if required, and ensure it exists
var newPropertyGroup = propertyGroupName == null
? null
: PropertyGroups.FirstOrDefault(x => x.Alias == propertyGroupName)
?? PropertyGroups.FirstOrDefault(x => x.Name.InvariantEquals(propertyGroupName)); // TODO Clean up for v9 (only added for backwards compatibility with names)
if (propertyGroupName != null && newPropertyGroup == null) return false;
PropertyGroup newPropertyGroup = null;
if (propertyGroupName != null)
{
var index = PropertyGroups.IndexOfKey(propertyGroupName);
if (index == -1) return false;
newPropertyGroup = PropertyGroups[index];
}
// get old group
var oldPropertyGroup = PropertyGroups.FirstOrDefault(x =>
x.PropertyTypes.Any(y => y.Alias.InvariantEquals(propertyTypeAlias)));
x.PropertyTypes.Any(y => y.Alias == propertyTypeAlias));
// set new group
propertyType.PropertyGroupId = newPropertyGroup == null ? null : new Lazy<int>(() => newPropertyGroup.Id, false);
@@ -401,9 +403,10 @@ namespace Umbraco.Core.Models
public void RemovePropertyGroup(string propertyGroupName)
{
// if no group exists with that name, do nothing
var group = PropertyGroups.FirstOrDefault(x => x.Alias == propertyGroupName)
?? PropertyGroups.FirstOrDefault(x => x.Name.InvariantEquals(propertyGroupName)); // TODO Clean up for v9 (only added for backwards compatibility with names)
if (group == null) return;
var index = PropertyGroups.IndexOfKey(propertyGroupName);
if (index == -1) return;
var group = PropertyGroups[index];
// first remove the group
PropertyGroups.Remove(group);

View File

@@ -254,24 +254,34 @@ namespace Umbraco.Core.Models
return group;
}
/// <summary>
/// Adds a PropertyType to a specific PropertyGroup
/// </summary>
/// <param name="propertyType"><see cref="PropertyType"/> to add</param>
/// <param name="propertyGroupName">Name of the PropertyGroup to add the PropertyType to</param>
/// <returns>Returns <c>True</c> if PropertyType was added, otherwise <c>False</c></returns>
public override bool AddPropertyType(PropertyType propertyType, string propertyGroupName)
/// <inheritdoc />
[Obsolete("Use AddPropertyType(propertyType, groupAlias, groupName) instead to explicitly set the alias of the group (note the slighty different parameter order).")]
public override bool AddPropertyType(PropertyType propertyType, string propertyGroupName) => AddPropertyType(propertyType, propertyGroupName.ToSafeAlias(true), propertyGroupName);
/// <inheritdoc />
public override bool AddPropertyType(PropertyType propertyType, string groupAlias, string groupName)
{
// ensure no duplicate alias - over all composition properties
if (PropertyTypeExists(propertyType.Alias))
return false;
// get and ensure a group local to this content type
var group = PropertyGroups.FirstOrDefault(x => x.Alias == propertyGroupName)
?? PropertyGroups.FirstOrDefault(x => x.Alias == propertyGroupName.ToSafeAlias(true)) // TODO Remove in v9 (only needed for backwards compatibility with names)
?? AddAndReturnPropertyGroup(propertyGroupName, propertyGroupName.ToSafeAlias(true)); // TODO Do we need both name and alias for this to work?
if (group == null)
PropertyGroup group;
var index = PropertyGroups.IndexOfKey(groupAlias);
if (index != -1)
{
group = PropertyGroups[index];
}
else if (!string.IsNullOrEmpty(groupName))
{
group = AddAndReturnPropertyGroup(groupName, groupAlias);
if (group == null) return false;
}
else
{
// No group name specified, so we can't create a new one and add the property type
return false;
}
// add property to group
propertyType.PropertyGroupId = new Lazy<int>(() => group.Id);

View File

@@ -130,12 +130,26 @@ namespace Umbraco.Core.Models
bool PropertyTypeExists(string propertyTypeAlias);
/// <summary>
/// Adds a PropertyType to a specific PropertyGroup
/// Adds the property type to the specified property group (creates a new group if not found).
/// </summary>
/// <param name="propertyType"><see cref="PropertyType"/> to add</param>
/// <param name="propertyGroupName">Name of the PropertyGroup to add the PropertyType to</param>
/// <returns>Returns <c>True</c> if PropertyType was added, otherwise <c>False</c></returns>
bool AddPropertyType(PropertyType propertyType, string propertyGroupName); // TODO Rename to propertyGroupAlias
/// <param name="propertyType">The property type to add.</param>
/// <param name="propertyGroupName">The name of the property group to add the property type to.</param>
/// <returns>
/// Returns <c>true</c> if the property type was added; otherwise, <c>false</c>.
/// </returns>
[Obsolete("Use AddPropertyType(propertyType, groupAlias, groupName) instead to explicitly set the alias of the group (note the slighty different parameter order).")]
bool AddPropertyType(PropertyType propertyType, string propertyGroupName);
/// <summary>
/// Adds the property type to the specified property group (creates a new group if not found and a name is specified).
/// </summary>
/// <param name="propertyType">The property type to add.</param>
/// <param name="groupAlias">The alias of the property group to add the property type to.</param>
/// <param name="groupName">The name of the property group to create when not found.</param>
/// <returns>
/// Returns <c>true</c> if the property type was added; otherwise, <c>false</c>.
/// </returns>
bool AddPropertyType(PropertyType propertyType, string groupAlias, string groupName); // TODO Make groupName optional (add null as default value) after removing obsolete overload
/// <summary>
/// Adds a PropertyType, which does not belong to a PropertyGroup.

View File

@@ -55,13 +55,10 @@ namespace Umbraco.Core.Models
// TODO Remove this property in v9 (only needed for backwards compatibility with names)
get
{
var item = base[key];
if (item == null && !key.Contains('/'))
{
item = base[key.ToSafeAlias(true)];
}
var index = IndexOfKey(key);
if (index == -1) throw new KeyNotFoundException();
return item;
return this[index];
}
}
@@ -169,11 +166,8 @@ namespace Umbraco.Core.Models
}
}
public new bool Contains(string key)
{
// TODO Remove this method in v9 (only needed for backwards compatibility with names)
return base.Contains(key) || (!key.Contains('/') && base.Contains(key.ToSafeAlias(true)));
}
// TODO Remove this method in v9 (only needed for backwards compatibility with names)
public new bool Contains(string key) => IndexOfKey(key) != -1;
public bool Contains(int id)
{