Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Umbraco copy content with descendants does not preserve sorting order #13464

Closed
denhaandrei opened this issue Nov 23, 2022 · 3 comments · Fixed by #13644
Closed

Umbraco copy content with descendants does not preserve sorting order #13464

denhaandrei opened this issue Nov 23, 2022 · 3 comments · Fixed by #13644

Comments

@denhaandrei
Copy link

denhaandrei commented Nov 23, 2022

Which Umbraco version are you using? (Please write the exact version, example: 10.1.0)

10.3.2

Bug summary

Hi,
I'm using late Umbraco 10 and my content editors asked me if there's any possible way to copy nested content items with sorting preserved when they copy their parent item.

For example, when I copy an item with the structure:

Parent item
 Nested item A
  Nested Nested Item I
  Nested Nested Item II
  Nested Nested Item III
 Nested item B
 Nested item C

I will get a new item:

Parent item
 Nested item B
  Nested Nested Item II
  Nested Nested Item I
  Nested Nested Item III
 Nested item A
 Nested item C

Specifics

Please note, it reproduce for all Umbraco version 10

Steps to reproduce

  1. Create a new Content page: TestSort
  2. Create a 3 section for TestSort(order is important!):

Nested item B
Nested item A
Nested item C

  1. Create a 3 page for Nested item B(order is important!):

Nested Nested Item II
Nested Nested Item I
Nested Nested Item III

  1. Sort in TestSort:

Nested item A
Nested item B
Nested item C

  1. Sort in Nested item B:

Nested Nested Item I
Nested Nested Item II
Nested Nested Item III

  1. Copy TestSort(Relate to original - uncheck, Include descendants - checked)

Expected result / actual result

Actual Result:
The content copied as created without applying new sorting:

Parent item
 Nested item B
  Nested Nested Item II
  Nested Nested Item I
  Nested Nested Item III
 Nested item A
 Nested item C

Expected Result:
The content copied with the actual applying sorting:

Parent item
 Nested item A
  Nested Nested Item I
  Nested Nested Item II
  Nested Nested Item III
 Nested item B
 Nested item C
@elit0451
Copy link
Contributor

elit0451 commented Nov 24, 2022

Hi @denhaandrei, thanks for reaching our 💪
I can confirm that this is an issue not only on v10.3.2 but also on previous major Umbraco versions, so the problem has been there for a while... 🤷 The steps I used to reproduce are the ones specified but some additional clarification is needed, so we don't confuse it with a concept in Umbraco called Nested content. 😉


Note ❕ : This is an issue that has been reported in the past (#8033) and there were some attempts to fix it (#8158). Based on my investigation, I would provide some nodes and mark it as up-for-grabs for now 🙂

The issue is about creating a Content node with nested Content nodes (descendants), sorting them and copying the parent Content node - as a result, the sorted order of the copied content nodes is not persisted. In other words:

  1. Create a Document type: "TestSort"
    1.1. Under Permissions add the same Document type as "Allowed child node types" of TestSort
  2. Create a content node from TestSort and name it "Parent item"
  3. Add 3 new items under the Parent item (3 content nodes as direct children)
    order is important:

Nested item B
Nested item A
Nested item C

  1. Create 3 new nodes under "Nested item B"
    order is important:

Nested Nested Item II
Nested Nested Item I
Nested Nested Item III

  1. Sort the following content nodes, like:

Nested item A
Nested item B
Nested item C

  1. Sort in Nested item B descendants:

Nested Nested Item I
Nested Nested Item II
Nested Nested Item III

  1. Copy the Parent item to the content tree (Relate to original - uncheck, Include descendants - checked)

We should end up with the following structure:

↖️ Sorted items on the left; Copied items on the right ↗️



Investigation + notes:

Looking at the database (umbracoNode table), we can see the problem in the sortOrder column. The first 3 highlighted items are the original ones and the order is sorted as per the steps above, while the other 3 are the copied nodes and there the order is reset back in accordance to their creation date.

Nodes_sort


Inspecting the code, it seems like the descendantCopy has the order correctly (ln.2719) but after we save it using _documentRepository.Save(descendantCopy) (ln.2739) the order is back to their creation order.

IContent descendantCopy = descendant.DeepCloneWithResetIdentities();
descendantCopy.ParentId = parentId;
if (scope.Notifications.PublishCancelable(
new ContentCopyingNotification(descendant, descendantCopy, parentId, eventMessages)))
{
continue;
}
// a copy is not published (but not really unpublishing either)
// update the create author and last edit author
if (descendantCopy.Published)
{
descendantCopy.Published = false;
}
descendantCopy.CreatorId = userId;
descendantCopy.WriterId = userId;
// save and flush (see above)
_documentRepository.Save(descendantCopy);


An idea could be to pass on the entity.SortOrder to nodeDto.SortOrder, instead of getting it from GetNewChildSortOrder() but we have to test out if other changes are necesary.

var sortOrder = GetNewChildSortOrder(entity.ParentId, 0);
// persist the node dto
NodeDto nodeDto = dto.ContentDto.NodeDto;
nodeDto.Path = parent.Path;
nodeDto.Level = Convert.ToInt16(level);
nodeDto.SortOrder = sortOrder;


🔴 Things to watch out for:

  • In the past, it has been pointed out that there is a more complicated scenario we have to test with - "If a child appears before the parent it will skip and not copy the child." (comment reference). We need to make sure that this is not the case after we fix the sorting. 😊

@elit0451 elit0451 changed the title Umbraco copy content with include descendants does not preserve sorting Umbraco copy content with descendants does not preserve sorting Nov 24, 2022
@elit0451 elit0451 changed the title Umbraco copy content with descendants does not preserve sorting Umbraco copy content with descendants does not preserve sorting order Nov 24, 2022
@ustadstar
Copy link
Contributor

I am working on this

@cpolish
Copy link

cpolish commented Apr 1, 2024

Hi there, thank you so much for solving this issue in Umbraco 10+.

My team at work are currently using Umbraco 8 for an application we have inherited from another client, and have currently encountered this issue as well.

I was hoping to see if our team could backport this fix to Umbraco 8, but it appears the fix in the linked PR relies on some features introduced in Umbraco 9.

If we wanted to backport this to Umbraco 8, are there any pointers or suggestions on what we should look to do that you guys could suggest? (even a slightly hacky solution will work).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants