Skip to content

Commit

Permalink
Merge pull request #1387 from hydephp/navigation-dropdown-priorities
Browse files Browse the repository at this point in the history
Update dropdown navigation menus to support setting priority in config
  • Loading branch information
caendesilva authored Oct 19, 2023
2 parents 888a907 + 6aa67b1 commit 284d56a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/digging-deeper/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Hyde\Framework\Features\Navigation;

use Hyde\Facades\Config;
use Illuminate\Support\Collection;

use function collect;
Expand All @@ -19,9 +20,9 @@ class DropdownNavItem extends NavItem
public array $items;

/** @param array<NavItem> $items */
public function __construct(string $label, array $items)
public function __construct(string $label, array $items, ?int $priority = null)
{
parent::__construct('', $label, 999);
parent::__construct('', $label, $priority ?? $this->searchForDropdownPriorityInNavigationConfig($label) ?? 999);
$this->items = $items;
}

Expand All @@ -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;
}
}
30 changes: 30 additions & 0 deletions packages/framework/tests/Unit/DropdownNavItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,6 +38,21 @@ 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(500, $item->priority);
}

public function testConstructWithNullPriority()
{
$item = new DropdownNavItem('foo', [], null);

$this->assertSame(999, $item->priority);
}

public function testFromArray()
Expand Down Expand Up @@ -72,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);
}
}

0 comments on commit 284d56a

Please sign in to comment.