diff --git a/src/Umbraco.Core/Models/ContentTypeBase.cs b/src/Umbraco.Core/Models/ContentTypeBase.cs
index ef5eb3dfc3..ea18923e68 100644
--- a/src/Umbraco.Core/Models/ContentTypeBase.cs
+++ b/src/Umbraco.Core/Models/ContentTypeBase.cs
@@ -304,14 +304,13 @@ namespace Umbraco.Core.Models
///
public abstract bool AddPropertyGroup(string name, string alias);
- ///
- /// Adds a PropertyType to a specific PropertyGroup
- ///
- /// to add
- /// Name of the PropertyGroup to add the PropertyType to
- /// Returns True if PropertyType was added, otherwise False
+ ///
+ [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);
+ ///
+ public abstract bool AddPropertyType(PropertyType propertyType, string groupAlias, string groupName);
+
///
/// Adds a PropertyType, which does not belong to a PropertyGroup.
///
@@ -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(() => 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);
diff --git a/src/Umbraco.Core/Models/ContentTypeCompositionBase.cs b/src/Umbraco.Core/Models/ContentTypeCompositionBase.cs
index 67ec52a8b0..999d0043ed 100644
--- a/src/Umbraco.Core/Models/ContentTypeCompositionBase.cs
+++ b/src/Umbraco.Core/Models/ContentTypeCompositionBase.cs
@@ -254,24 +254,34 @@ namespace Umbraco.Core.Models
return group;
}
- ///
- /// Adds a PropertyType to a specific PropertyGroup
- ///
- /// to add
- /// Name of the PropertyGroup to add the PropertyType to
- /// Returns True if PropertyType was added, otherwise False
- public override bool AddPropertyType(PropertyType propertyType, string propertyGroupName)
+ ///
+ [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);
+
+ ///
+ 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(() => group.Id);
diff --git a/src/Umbraco.Core/Models/IContentTypeBase.cs b/src/Umbraco.Core/Models/IContentTypeBase.cs
index c489139970..7152ccb0f9 100644
--- a/src/Umbraco.Core/Models/IContentTypeBase.cs
+++ b/src/Umbraco.Core/Models/IContentTypeBase.cs
@@ -130,12 +130,26 @@ namespace Umbraco.Core.Models
bool PropertyTypeExists(string propertyTypeAlias);
///
- /// Adds a PropertyType to a specific PropertyGroup
+ /// Adds the property type to the specified property group (creates a new group if not found).
///
- /// to add
- /// Name of the PropertyGroup to add the PropertyType to
- /// Returns True if PropertyType was added, otherwise False
- bool AddPropertyType(PropertyType propertyType, string propertyGroupName); // TODO Rename to propertyGroupAlias
+ /// The property type to add.
+ /// The name of the property group to add the property type to.
+ ///
+ /// Returns true if the property type was added; otherwise, false.
+ ///
+ [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);
+
+ ///
+ /// Adds the property type to the specified property group (creates a new group if not found and a name is specified).
+ ///
+ /// The property type to add.
+ /// The alias of the property group to add the property type to.
+ /// The name of the property group to create when not found.
+ ///
+ /// Returns true if the property type was added; otherwise, false.
+ ///
+ bool AddPropertyType(PropertyType propertyType, string groupAlias, string groupName); // TODO Make groupName optional (add null as default value) after removing obsolete overload
///
/// Adds a PropertyType, which does not belong to a PropertyGroup.
diff --git a/src/Umbraco.Core/Models/PropertyGroupCollection.cs b/src/Umbraco.Core/Models/PropertyGroupCollection.cs
index 7b20957cd2..6ad85639e4 100644
--- a/src/Umbraco.Core/Models/PropertyGroupCollection.cs
+++ b/src/Umbraco.Core/Models/PropertyGroupCollection.cs
@@ -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)
{