Skip to content

Commit

Permalink
Adding support for "active" (#4)
Browse files Browse the repository at this point in the history
* Adding support for "active"

* cs
  • Loading branch information
Nyholm authored Jun 11, 2021
1 parent 801526e commit 7e8b096
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
}
],
"require": {
"php": "^7.2",
"symfony/config": "^4.4 || ^5.0",
"symfony/dependency-injection": "^4.4 || ^5.0",
"symfony/http-kernel": "^4.4 || ^5.0",
"symfony/http-foundation": "^4.4 || ^5.0.7",
"php": ">=7.2",
"symfony/config": "^4.4 || ^5.3 | ^6.0",
"symfony/dependency-injection": "^4.4 || ^5.3 | ^6.0",
"symfony/http-kernel": "^4.4 || ^5.3 | ^6.0",
"symfony/http-foundation": "^4.4 || ^5.3 | ^6.0",
"twig/twig": "^2.12.5 || ^3.0"
},
"require-dev": {
"symfony/phpunit-bridge": "^4.4 || ^5.0"
"symfony/phpunit-bridge": "^4.4 || ^5.3 | ^6.0"
},
"config": {
"sort-packages": true
Expand Down
22 changes: 22 additions & 0 deletions src/Model/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ final class MenuItem
*/
private $route;

/**
* Is this the current page?
*
* @var bool
*/
private $active = false;

/**
* @var array
*/
Expand Down Expand Up @@ -85,6 +92,11 @@ public function getChildren(): array
return $this->children;
}

public function hasChildren(): bool
{
return [] !== $this->children;
}

public function setChildren(array $children): void
{
$this->children = $children;
Expand All @@ -94,4 +106,14 @@ public function addChild(MenuItem $menuItem)
{
$this->children[] = $menuItem;
}

public function isActive(): bool
{
return $this->active;
}

public function setActive(bool $active): void
{
$this->active = $active;
}
}
26 changes: 24 additions & 2 deletions src/Twig/MenuRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Happyr\MenuBuilderBundle\Twig;

use Happyr\MenuBuilderBundle\MenuBuilder;
use Happyr\MenuBuilderBundle\Model\MenuItem;
use Symfony\Component\HttpFoundation\RequestStack;
use Twig\Environment;
use Twig\Extension\RuntimeExtensionInterface;
Expand All @@ -27,18 +28,39 @@ public function __construct(RequestStack $requestStack, MenuBuilder $builder, st

public function renderMenu(Environment $twig, string $name, array $options = []): string
{
$menuItems = $this->builder->getMenu($name, $options);

$request = $this->requestStack->getMasterRequest();
$route = null;
if (null !== $request) {
$route = $request->attributes->get('_route', null);
$this->markAsActive($menuItems, $route);
}

$menuItems = $this->builder->getMenu($name, $options);

return $twig->render($options['template'] ?? $this->defaultTemplate, [
'menuItems' => $menuItems,
'route' => $route,
'options' => $options,
]);
}

/**
* @param MenuItem[] $menuItems
*/
private function markAsActive(array $menuItems, ?string $route): bool
{
if (null === $route) {
return false;
}

$returnValue = false;
foreach ($menuItems as $item) {
if ($route === $item->getRoute() || ($item->hasChildren() && $this->markAsActive($item->getChildren(), $route))) {
$item->setActive(true);
$returnValue = true;
}
}

return $returnValue;
}
}

0 comments on commit 7e8b096

Please sign in to comment.