From 1915c815a26e176435650a18ebb46d994fa2d084 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 19 Oct 2023 11:02:16 +0200 Subject: [PATCH 1/2] Update DropdownNavItem to support setting priority in constructor --- .../Framework/Features/Navigation/DropdownNavItem.php | 4 ++-- packages/framework/tests/Unit/DropdownNavItemTest.php | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Navigation/DropdownNavItem.php b/packages/framework/src/Framework/Features/Navigation/DropdownNavItem.php index 4d0a6c07f79..7aaa5fc52b2 100644 --- a/packages/framework/src/Framework/Features/Navigation/DropdownNavItem.php +++ b/packages/framework/src/Framework/Features/Navigation/DropdownNavItem.php @@ -19,9 +19,9 @@ class DropdownNavItem extends NavItem public array $items; /** @param array $items */ - public function __construct(string $label, array $items) + public function __construct(string $label, array $items, int $priority = 999) { - parent::__construct('', $label, 999); + parent::__construct('', $label, $priority); $this->items = $items; } diff --git a/packages/framework/tests/Unit/DropdownNavItemTest.php b/packages/framework/tests/Unit/DropdownNavItemTest.php index 8c2529bc5c2..1be55e0acb1 100644 --- a/packages/framework/tests/Unit/DropdownNavItemTest.php +++ b/packages/framework/tests/Unit/DropdownNavItemTest.php @@ -37,6 +37,16 @@ public function testConstruct() $this->assertSame('foo', $item->label); $this->assertSame([], $item->items); + $this->assertSame(999, $item->priority); + } + + public function testConstructWithCustomPriority() + { + $item = new DropdownNavItem('foo', [], 500); + + $this->assertSame('foo', $item->label); + $this->assertSame([], $item->items); + $this->assertSame(500, $item->priority); } public function testFromArray() From 6aa67b14548130749666cc60091e67ba9bfeb8d7 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 19 Oct 2023 11:21:44 +0200 Subject: [PATCH 2/2] Update dropdown navigation menus to support setting priority in config Fixes https://github.com/hydephp/hyde/issues/229 --- docs/digging-deeper/customization.md | 2 ++ .../Features/Navigation/DropdownNavItem.php | 14 +++++++++-- .../tests/Unit/DropdownNavItemTest.php | 24 +++++++++++++++++-- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/docs/digging-deeper/customization.md b/docs/digging-deeper/customization.md index 5721736d43b..daf4f24f7b8 100644 --- a/docs/digging-deeper/customization.md +++ b/docs/digging-deeper/customization.md @@ -267,6 +267,8 @@ navigation: --- ``` +>info Tip: If you are using automatic subdirectory dropdowns, you can also set their priority in the config. Just use the directory name instead of the page identifier. + #### Adding Custom Navigation Menu Links You can easily add custom navigation menu links similar how we add Authors. Simply add a `NavItem` model to the `navigation.custom` array. diff --git a/packages/framework/src/Framework/Features/Navigation/DropdownNavItem.php b/packages/framework/src/Framework/Features/Navigation/DropdownNavItem.php index 7aaa5fc52b2..f1adc1cc2d4 100644 --- a/packages/framework/src/Framework/Features/Navigation/DropdownNavItem.php +++ b/packages/framework/src/Framework/Features/Navigation/DropdownNavItem.php @@ -4,6 +4,7 @@ namespace Hyde\Framework\Features\Navigation; +use Hyde\Facades\Config; use Illuminate\Support\Collection; use function collect; @@ -19,9 +20,9 @@ class DropdownNavItem extends NavItem public array $items; /** @param array $items */ - public function __construct(string $label, array $items, int $priority = 999) + public function __construct(string $label, array $items, ?int $priority = null) { - parent::__construct('', $label, $priority); + parent::__construct('', $label, $priority ?? $this->searchForDropdownPriorityInNavigationConfig($label) ?? 999); $this->items = $items; } @@ -34,4 +35,13 @@ public function getItems(): Collection { return collect($this->items); } + + private function searchForDropdownPriorityInNavigationConfig(string $groupKey): ?int + { + return Config::getArray('hyde.navigation.order', [ + 'index' => 0, + 'posts' => 10, + 'docs/index' => 100, + ])[$groupKey] ?? null; + } } diff --git a/packages/framework/tests/Unit/DropdownNavItemTest.php b/packages/framework/tests/Unit/DropdownNavItemTest.php index 1be55e0acb1..884045acb46 100644 --- a/packages/framework/tests/Unit/DropdownNavItemTest.php +++ b/packages/framework/tests/Unit/DropdownNavItemTest.php @@ -4,6 +4,7 @@ namespace Hyde\Framework\Testing\Unit; +use Hyde\Facades\Config; use Hyde\Framework\Features\Navigation\DropdownNavItem; use Hyde\Framework\Features\Navigation\NavItem; use Hyde\Pages\MarkdownPage; @@ -44,11 +45,16 @@ public function testConstructWithCustomPriority() { $item = new DropdownNavItem('foo', [], 500); - $this->assertSame('foo', $item->label); - $this->assertSame([], $item->items); $this->assertSame(500, $item->priority); } + public function testConstructWithNullPriority() + { + $item = new DropdownNavItem('foo', [], null); + + $this->assertSame(999, $item->priority); + } + public function testFromArray() { $item = DropdownNavItem::fromArray('foo', []); @@ -82,4 +88,18 @@ public function testGetItems() $item = DropdownNavItem::fromArray('foo', $children); $this->assertSame($children, $item->getItems()->all()); } + + public function testCanSetPriorityInConfig() + { + $root = Config::getFacadeRoot(); + $mock = clone $root; + Config::swap($mock); + + Config::set('hyde.navigation.order.foo', 500); + $item = new DropdownNavItem('foo', []); + + $this->assertSame(500, $item->priority); + + Config::swap($root); + } }