diff --git a/Builder/ItemProcess.php b/Builder/ItemProcess.php index 3d521c4..ed71c18 100644 --- a/Builder/ItemProcess.php +++ b/Builder/ItemProcess.php @@ -132,7 +132,7 @@ protected function recursiveProcess(ItemInterface $menu, $options): bool // Sort Item usort($childs, static function ($a, $b) { - return $a->getOrder() > $b->getOrder(); + return $a->getOrder() > $b->getOrder() ? 1 : ($a->getOrder() === $b->getOrder() ? 0 : -1); }); // Set Childs diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php new file mode 100644 index 0000000..5c7938b --- /dev/null +++ b/DependencyInjection/Configuration.php @@ -0,0 +1,36 @@ + + * @link https://github.com/appaydin/pd-user + */ + +namespace Pd\MenuBundle\DependencyInjection; + +use Symfony\Component\Config\Definition\Builder\TreeBuilder; +use Symfony\Component\Config\Definition\ConfigurationInterface; + +class Configuration implements ConfigurationInterface +{ + public function getConfigTreeBuilder() + { + $treeBuilder = new TreeBuilder('pd_menu'); + $rootNode = $treeBuilder->getRootNode(); + + // Set Configuration + $rootNode + ->children() + ->scalarNode('template')->defaultValue('@PdMenu/Default/menu.html.twig')->end() + ->scalarNode('depth')->defaultValue(null)->end() + ->scalarNode('currentClass')->defaultValue('active')->end() + ->scalarNode('trans_domain')->defaultValue(null)->end() + ->scalarNode('iconTemplate')->defaultValue('&text')->end() + ->end(); + + return $treeBuilder; + } +} diff --git a/DependencyInjection/PdMenuExtension.php b/DependencyInjection/PdMenuExtension.php index d55f1cf..6940b0b 100644 --- a/DependencyInjection/PdMenuExtension.php +++ b/DependencyInjection/PdMenuExtension.php @@ -28,6 +28,13 @@ class PdMenuExtension extends Extension */ public function load(array $configs, ContainerBuilder $container) { + // Load Configuration + $configuration = new Configuration(); + $config = $this->processConfiguration($configuration, $configs); + + // Set Configuration + $container->setParameter('pd_menu', $config); + // Load Services $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('services.yml'); diff --git a/README.md b/README.md index 0603dbc..af54322 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle: ```console -$ composer require appaydin/pd-menu +composer require appaydin/pd-menu ``` This command requires you to have Composer installed globally, as explained @@ -202,6 +202,19 @@ You can change the default options. }) }} ``` +You can change the default options globally. + +```twig +config/packages/pd_menu.yaml + +pd_menu: + template: '@PdMenu/Default/menu.html.twig', + depth: null, + currentClass: active + trans_domain: admin, + iconTemplate: '' +``` + Create Menu Event & Event Listener --- #### Step 1: Create Menu Event diff --git a/Resources/views/Default/menu.html.twig b/Resources/views/Default/menu.html.twig index b755e7e..20abd94 100644 --- a/Resources/views/Default/menu.html.twig +++ b/Resources/views/Default/menu.html.twig @@ -12,12 +12,13 @@ {# Create Menu Item #} {% block menu_item %} -
  • + {% set dropdown = menu.child|length > 0 %} +
  • {# Create Link #} {% if menu.link %} {% if menu.extra('label_icon') %} - {{ options.iconTemplate|replace({ 'itext': menu.extra('label_icon') })|raw }} + {{ options.iconTemplate|replace({ '&text': menu.extra('label_icon') })|raw }} {% endif %} {{ menu.extra('label_translate', true) ? menu.label|trans([], options.trans_domain)|raw : menu.label|raw }} @@ -27,7 +28,7 @@ {{ menu.linkAfterHtml|raw }} {% else %} {% if menu.extra('label_icon') %} - {{ options.iconTemplate|replace({ 'itext': menu.extra('label_icon') })|raw }} + {{ options.iconTemplate|replace({ '&text': menu.extra('label_icon') })|raw }} {% endif %} {{ menu.extra('label_translate', true) ? menu.label|trans([], options.trans_domain)|raw : menu.label|raw }} diff --git a/Twig/MenuExtension.php b/Twig/MenuExtension.php index 882a7c7..188c1f6 100644 --- a/Twig/MenuExtension.php +++ b/Twig/MenuExtension.php @@ -16,6 +16,7 @@ use Pd\MenuBundle\Builder\MenuInterface; use Pd\MenuBundle\Render\RenderInterface; use Psr\Container\ContainerInterface; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Contracts\Translation\TranslatorInterface; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; @@ -31,35 +32,28 @@ class MenuExtension extends AbstractExtension * @var RenderInterface */ private $engine; - /** * @var ItemProcessInterface */ private $itemProcess; - /** * @var TranslatorInterface */ private $translator; - /** - * Default Menu Options. - * - * @var array + * @var ParameterBagInterface */ - private $defaultOptions = [ - 'template' => '@PdMenu/Default/menu.html.twig', - 'depth' => null, - 'currentClass' => 'active', - 'trans_domain' => null, - 'iconTemplate' => 'itext', - ]; - - public function __construct(RenderInterface $engine, ItemProcessInterface $itemProcess, TranslatorInterface $translator) + private ParameterBagInterface $parameterBag; + + public function __construct(RenderInterface $engine, ItemProcessInterface $itemProcess, + TranslatorInterface $translator, + ParameterBagInterface $parameterBag + ) { $this->engine = $engine; $this->itemProcess = $itemProcess; $this->translator = $translator; + $this->parameterBag = $parameterBag; } /** @@ -82,12 +76,12 @@ public function getFunctions() * @param string $menuClass * @param array $options * - * @return string + * @return string|null */ - public function renderMenu(string $menuClass = '', $options = []): string + public function renderMenu(string $menuClass = '', $options = []): ?string { // Merge Options - $options = array_merge($this->defaultOptions, $options); + $options = array_merge($this->parameterBag->get('pd_menu'), $options); // Get Menu $menu = new $menuClass(); @@ -100,7 +94,7 @@ public function renderMenu(string $menuClass = '', $options = []): string return $this->engine->render($menu, $options); } - return false; + return null; } /** @@ -109,12 +103,12 @@ public function renderMenu(string $menuClass = '', $options = []): string * @param string $menuClass * @param array $options * - * @return ItemInterface|bool + * @return ItemInterface|null */ - public function getMenu(string $menuClass, $options = []) + public function getMenu(string $menuClass, $options = []): ?ItemInterface { // Merge Options - $options = array_merge($this->defaultOptions, $options); + $options = array_merge($this->parameterBag->get('pd_menu'), $options); // Get Menu $menu = new $menuClass(); @@ -124,7 +118,7 @@ public function getMenu(string $menuClass, $options = []) return $this->itemProcess->processMenu($menu->createMenu($options), $options); } - return false; + return null; } /** @@ -148,7 +142,8 @@ public function arrayToAttr(array $array = [], array $append = [], array $option if ('title' === mb_strtolower($key)) { if (!isset($array['title_translate'])) { - $value = $this->translator->trans($value, [], $options['trans_domain'] ?? null); + $value = $this->translator->trans($value, [], + $options['trans_domain'] ?? $this->parameterBag->get('pd_menu')['trans_domain']); } } diff --git a/composer.json b/composer.json index 8c40918..14bfdaf 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ } ], "require": { - "php": "^7.3.0", + "php": "^7.4.0", "twig/twig": "*", "symfony/event-dispatcher": "*" },