mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 13:54:23 +00:00
93 lines
2.6 KiB
PHP
93 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Chill\MainBundle\Routing;
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
use Twig\Environment;
|
|
use Twig\Extension\AbstractExtension;
|
|
use Twig\TwigFunction;
|
|
|
|
/**
|
|
* Add the filter 'chill_menu'.
|
|
*/
|
|
class MenuTwig extends AbstractExtension implements ContainerAwareInterface
|
|
{
|
|
private ?\Symfony\Component\DependencyInjection\ContainerInterface $container = null;
|
|
|
|
/**
|
|
* the default parameters for chillMenu.
|
|
*
|
|
* @var mixed[]
|
|
*/
|
|
private array $defaultParams = [
|
|
'layout' => '@ChillMain/Menu/defaultMenu.html.twig',
|
|
'args' => [],
|
|
'activeRouteKey' => null,
|
|
];
|
|
|
|
public function __construct(private readonly MenuComposer $menuComposer) {}
|
|
|
|
/**
|
|
* Render a Menu corresponding to $menuId.
|
|
*
|
|
* Expected params :
|
|
* - args: the arguments to build the path (i.e: if pattern is /something/{bar}, args must contain {'bar': 'foo'}
|
|
* - layout: the layout. Absolute path needed (i.e.: @ChillXyz/section/foo.html.twig)
|
|
* - activeRouteKey : the key active, will render the menu differently.
|
|
*
|
|
* @deprecated link: see https://redmine.champs-libres.coop/issues/179 for more informations
|
|
*
|
|
* @param string $menuId
|
|
* @param mixed[] $params
|
|
*/
|
|
public function chillMenu(Environment $env, $menuId, array $params = [])
|
|
{
|
|
$resolvedParams = array_merge($this->defaultParams, $params);
|
|
|
|
$layout = $resolvedParams['layout'];
|
|
unset($resolvedParams['layout']);
|
|
|
|
if ($this->menuComposer->hasLocalMenuBuilder($menuId) === false) {
|
|
$resolvedParams['routes'] = $this->menuComposer->getRoutesFor($menuId, $resolvedParams);
|
|
|
|
return $env->render($layout, $resolvedParams);
|
|
}
|
|
$resolvedParams['menus'] = $this->menuComposer->getMenuFor($menuId, $resolvedParams);
|
|
|
|
return $env->render($layout, $resolvedParams);
|
|
}
|
|
|
|
public function getFunctions()
|
|
{
|
|
return [new TwigFunction(
|
|
'chill_menu',
|
|
$this->chillMenu(...),
|
|
[
|
|
'is_safe' => ['html'],
|
|
'needs_environment' => true,
|
|
]
|
|
),
|
|
];
|
|
}
|
|
|
|
public function getName()
|
|
{
|
|
return 'chill_menu';
|
|
}
|
|
|
|
public function setContainer(?ContainerInterface $container = null)
|
|
{
|
|
$this->container = $container;
|
|
}
|
|
}
|