-
Notifications
You must be signed in to change notification settings - Fork 194
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
Added a new sortOrder option to sort menu items when they are added t… #381
Open
B4rb4ross4
wants to merge
1
commit into
KnpLabs:master
Choose a base branch
from
B4rb4ross4:feature/add_sort_order_for_menu_items
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
Control the sort order of your menu items | ||
========================================= | ||
|
||
There are 2 ways to control the order of your menu items: | ||
|
||
1. Reorder items by giving a sorted list of item names | ||
2. Sort the items when adding them to the menu | ||
|
||
Reorder items by giving a sorted list of item names | ||
----------------------- | ||
|
||
```php | ||
$factory = new MenuFactory(); | ||
$menu = new MenuItem('root', $factory); | ||
$menu->addChild('c1'); | ||
$menu->addChild('c2'); | ||
$menu->addChild('c3'); | ||
$menu->addChild('c4'); | ||
|
||
$menu->reorderChildren(['c4', 'c3', 'c2', 'c1']); | ||
$menu->getChildren() //'c4', 'c3', 'c2', 'c1' | ||
``` | ||
|
||
Sort the items when adding them to the menu | ||
------------------------------------------- | ||
|
||
The items can be added in a sorted manner by using the `sortOrder` options. | ||
|
||
Caution: The order will be lost when using `ItemInterface->reorderChildren()`! | ||
|
||
```php | ||
$factory = new MenuFactory(); | ||
$menu = new MenuItem('root', $factory); | ||
$menu->addChild('c1', ['sortOrder' => 2]); | ||
$menu->addChild('c2', ['sortOrder' => 4]); | ||
$menu->addChild('c3', ['sortOrder' => 1]); | ||
$menu->addChild('c4', ['sortOrder' => 3]); | ||
|
||
$menu->getChildren() //'c1', 'c2', 'c3', 'c4' | ||
``` | ||
|
||
Items without the `sortOrder` option will be just appended after the items with `sortOrder` in the order they're added. |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Knp\Menu\Tests; | ||
|
||
use Knp\Menu\MenuFactory; | ||
use Knp\Menu\MenuItem; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class MenuItemSortOrderTest extends TestCase | ||
{ | ||
public function testItemsWithoutSortOrderShouldBeAppendedToKeepCurrentBehavior(): void | ||
{ | ||
$factory = new MenuFactory(); | ||
$menu = new MenuItem('root', $factory); | ||
$menu->addChild('c1', ['sortOrder' => 1]); | ||
$menu->addChild('c2'); | ||
$menu->addChild('c3', ['sortOrder' => 2]); | ||
$menu->addChild('c4'); | ||
$menu->addChild('c5'); | ||
$menu->addChild('c6', ['sortOrder' => 1]); | ||
$menu->addChild('c7', ['sortOrder' => 1]); | ||
|
||
$arr = \array_keys($menu->getChildren()); | ||
$this->assertEquals(['c1', 'c6', 'c7', 'c3', 'c2', 'c4', 'c5'], $arr); | ||
} | ||
|
||
public function testItemsAreAddedInTheCorrectOrder(): void | ||
{ | ||
$factory = new MenuFactory(); | ||
$menu = new MenuItem('root', $factory); | ||
$menu->addChild('c1', ['sortOrder' => 2]); | ||
$menu->addChild('c2', ['sortOrder' => 4]); | ||
$menu->addChild('c3', ['sortOrder' => 1]); | ||
$menu->addChild('c4', ['sortOrder' => 3]); | ||
|
||
$arr = \array_keys($menu->getChildren()); | ||
$this->assertEquals(['c3', 'c1', 'c4', 'c2'], $arr); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this line missing here?
$menu->reorderChildren();