Skip to content

Conversation

@duncanmcclean
Copy link
Member

This pull request attempts to fix an issue where moving a custom section to the 1st position would cause it to be moved to the last position instead.

Note

I don't really know how the nav builder works, so I asked Claude to write me a failing test and come up with a solution. Feel free to tweak as necessary.

Fixes #12419


Root Cause

The issue was in the NavTransformer::calculateMinimumItemsForReorder() method. When the new navigation list contained custom sections (items not in the original list), the algorithm that calculated which items to include in the reorder array didn't account for custom items inserted in the middle of the list.

The original algorithm used zip() to pair items from the reversed original and new lists, but when the lists had different lengths (due to custom sections), the pairing was incorrect, leading to the wrong minimum count being calculated.

The Fix

I made two key changes:

  1. Modified calculateMinimumItemsForReorder() method (src/CP/Navigation/NavTransformer.php):
    • Added logic to detect custom items (items not in the original list)
    • Check if custom items have any original items after them (to distinguish between custom items in the middle vs. at the end)
    • For custom items in the middle, include enough items in the reorder array to establish their position
    • Custom items at the end are automatically appended and don't need to be in the reorder array
  2. Changed the empty array check in minify() method (src/CP/Navigation/NavTransformer.php):
    • Changed from $reorder ? ... to ! empty($reorder) ? ... for better clarity
    • This ensures that empty arrays are correctly treated as falsy

@duncanmcclean duncanmcclean changed the title [5.x] Nav preferences first section [5.x] Fix moving custom section to 1st position in CP Nav Nov 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to place as 1st section within CP Nav

2 participants