umbraco / Umbraco-CMS

Umbraco is a free and open source .NET content management system helping you deliver delightful digital experiences.
https://umbraco.com
MIT License
4.4k stars 2.66k forks source link

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

Closed denhaandrei closed 1 year ago

denhaandrei commented 1 year ago

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

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

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

  4. Sort in TestSort:

    Nested item A Nested item B Nested item C

  5. Sort in Nested item B:

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

  6. 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 commented 1 year ago

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 (https://github.com/umbraco/Umbraco-CMS/issues/8033) and there were some attempts to fix it (https://github.com/umbraco/Umbraco-CMS/pull/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

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

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

  5. Sort the following content nodes, like:

    Nested item A Nested item B Nested item C

  6. Sort in Nested item B descendants:

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

  7. 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. https://github.com/umbraco/Umbraco-CMS/blob/4df012e8e9bc7bf3131791b15b4a41ebc63c4abe/src/Umbraco.Core/Services/ContentService.cs#L2719-L2739


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.

https://github.com/umbraco/Umbraco-CMS/blob/3a01176ae4a53a621c9fda6c371b2e85d2c14402/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/DocumentRepository.cs#L909-L915


πŸ”΄ Things to watch out for:

ustadstar commented 1 year ago

I am working on this

cpolish commented 5 months ago

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).