Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Umbraco.Core/Constants-Conventions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ public static class RelationTypes
/// Developers should not manually use these relation types since they will all be cleared whenever an entity
/// (content, media or member) is saved since they are auto-populated based on property values.
/// </remarks>
[Obsolete("This is no longer used, and will be removed in v12")]
public static string[] AutomaticRelationTypes { get; } = { RelatedMediaAlias, RelatedDocumentAlias };

// TODO: return a list of built in types so we can use that to prevent deletion in the UI
Expand Down
6 changes: 6 additions & 0 deletions src/Umbraco.Core/PropertyEditors/IDataValueReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ public interface IDataValueReference
/// <param name="value"></param>
/// <returns></returns>
IEnumerable<UmbracoEntityReference> GetReferences(object? value);

/// <summary>
/// Returns all reference types that are automatically tracked.
/// </summary>
/// <returns></returns>
IEnumerable<string> GetAutomaticRelationTypesAliases() => Enumerable.Empty<string>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -1042,10 +1042,7 @@ protected void PersistRelations(TEntity entity)
var trackedRelations = new List<UmbracoEntityReference>();
trackedRelations.AddRange(_dataValueReferenceFactories.GetAllReferences(entity.Properties, PropertyEditors));

var relationTypeAliases = trackedRelations
.Select(x => x.RelationTypeAlias)
.Distinct()
.ToArray();
var relationTypeAliases = GetAutomaticRelationTypesAliases(entity.Properties, PropertyEditors).ToArray();

// First delete all auto-relations for this entity
RelationRepository.DeleteByParent(entity.Id, relationTypeAliases);
Expand Down Expand Up @@ -1093,6 +1090,31 @@ protected void PersistRelations(TEntity entity)
RelationRepository.SaveBulk(toSave);
}

private IEnumerable<string> GetAutomaticRelationTypesAliases(
IPropertyCollection properties,
PropertyEditorCollection propertyEditors)
{
var automaticRelationTypesAliases = new HashSet<string>(Constants.Conventions.RelationTypes.AutomaticRelationTypes);

foreach (IProperty property in properties)
{
if (propertyEditors.TryGet(property.PropertyType.PropertyEditorAlias, out IDataEditor? editor) is false )
{
continue;
}

if (editor.GetValueEditor() is IDataValueReference reference)
{
foreach (var alias in reference.GetAutomaticRelationTypesAliases())
{
automaticRelationTypesAliases.Add(alias);
}
}
}

return automaticRelationTypesAliases;
}

/// <summary>
/// Inserts property values for the content entity
/// </summary>
Expand Down