Skip to content
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

Update dropdown navigation menus to support setting priority in config #1387

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading