Correct the population of the parent for sibling items when retrieved under a folder (#20083)

Correct the population of the parent for sibling items when retrieved under a folder.
This commit is contained in:
Andy Butland
2025-09-10 07:45:36 +02:00
committed by GitHub
parent 4ba01394b8
commit c93d4d9318
2 changed files with 33 additions and 3 deletions

View File

@@ -73,9 +73,7 @@ public abstract class EntityTreeControllerBase<TItem> : ManagementApiControllerB
}
IEntitySlim? entity = siblings.FirstOrDefault();
Guid? parentKey = entity?.ParentId > 0
? EntityService.GetKey(entity.ParentId, ItemObjectType).Result
: Constants.System.RootKey;
Guid? parentKey = GetParentKey(entity);
TItem[] treeItemViewModels = MapTreeItemViewModels(parentKey, siblings);
@@ -86,6 +84,14 @@ public abstract class EntityTreeControllerBase<TItem> : ManagementApiControllerB
return Ok(result);
}
/// <summary>
/// Gets the parent key for an entity, or root if null or no parent.
/// </summary>
protected virtual Guid? GetParentKey(IEntitySlim? entity) =>
entity?.ParentId > 0
? EntityService.GetKey(entity.ParentId, ItemObjectType).Result
: Constants.System.RootKey;
protected virtual async Task<ActionResult<IEnumerable<TItem>>> GetAncestors(Guid descendantKey, bool includeSelf = true)
{
IEntitySlim[] ancestorEntities = await GetAncestorEntitiesAsync(descendantKey, includeSelf);

View File

@@ -45,6 +45,30 @@ public abstract class FolderTreeControllerBase<TItem> : NamedEntityTreeControlle
protected abstract UmbracoObjectTypes FolderObjectType { get; }
/// <inheritdoc/>
protected override Guid? GetParentKey(IEntitySlim? entity)
{
if (entity is null || entity.ParentId <= 0)
{
return Constants.System.RootKey;
}
Attempt<Guid> getKeyAttempt = EntityService.GetKey(entity.ParentId, ItemObjectType);
if (getKeyAttempt.Success)
{
return getKeyAttempt.Result;
}
// Parent could be a folder, so try that too.
getKeyAttempt = EntityService.GetKey(entity.ParentId, FolderObjectType);
if (getKeyAttempt.Success)
{
return getKeyAttempt.Result;
}
return Constants.System.RootKey;
}
protected void RenderFoldersOnly(bool foldersOnly) => _foldersOnly = foldersOnly;
protected override IEntitySlim[] GetPagedRootEntities(int skip, int take, out long totalItems)