Implement Widget Positioning Methods #5879
Open
+225
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Implement Widget Positioning Methods:
after()andbefore()WHY
BEFORE - What was wrong? What was happening before this PR?
The
Widgetclass had two empty placeholder methods (after()andbefore()) marked with TODO comments, indicating that the functionality to position widgets relative to other widgets was planned but not yet implemented. This limited developers' ability to precisely control widget ordering in their admin panels.Developers could only use
makeFirst()andmakeLast()methods to position widgets at the beginning or end of a section, but had no way to insert a widget immediately before or after a specific widget in the middle of the collection.AFTER - What is happening after this PR?
Developers can now use the
after($destination)andbefore($destination)methods to position widgets relative to other widgets by name. This provides fine-grained control over widget ordering, allowing for more flexible and intuitive widget management.Example usage:
HOW
How did you achieve that, in technical terms?
Implemented
after($destination)method: This method moves the current widget to appear immediately after the specified destination widget in the collection. It:Implemented
before($destination)method: This method moves the current widget to appear immediately before the specified destination widget. It follows the same pattern asafter()but inserts the widget before the destination instead.Added comprehensive unit tests: Created
WidgetTest.phpwith 10 test cases covering:Technical Implementation Details
Both methods use the following approach:
$thisif not, allowing for graceful failure)array_slice()to split the collection at the appropriate positionreplace()method to update the global collection$thisfor method chainingIs it a breaking change?
No, this is not a breaking change. The methods were previously empty placeholders that did nothing, so any existing code calling these methods will continue to work (though it will now actually perform the positioning operation). This is a purely additive feature that enhances existing functionality.
How can we test the before & after?
Manual Testing:
setup()method:Automated Testing:
Run the new unit tests:
Expected output should show all 10 tests passing:
Additional Notes
Code Quality
$thisif destination doesn't exist)makeFirst()andmakeLast()implementationsFuture Enhancements
This implementation resolves the TODO items in the Widget class. Similar positioning logic could potentially be applied to other areas of the codebase where ordering is important (e.g., the
moveColumn()method inColumnsProtectedMethods.phphas a similar TODO about refactoring).Related Issues
This PR addresses the TODO comments found at:
src/app/Library/Widget.php(after method)src/app/Library/Widget.php(before method)