From d1475018b8dbc8604c834ed15ca376763fb6ea29 Mon Sep 17 00:00:00 2001 From: kemor Date: Sun, 22 May 2016 19:48:21 +0200 Subject: [PATCH] allow "extras" params in config --- Config/Definition/Builder/MenuNodeDefinition.php | 4 ++++ DependencyInjection/JbConfigKnpMenuExtension.php | 1 - Provider/ConfigurationMenuProvider.php | 5 +++++ README.md | 1 + .../Fixtures/Bundle2/Resources/config/navigation.yml | 5 ++++- Tests/DependencyInjection/NavigationConfigurationTest.php | 4 +++- Tests/Provider/ConfigurationMenuProviderTest.php | 5 +++++ 7 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Config/Definition/Builder/MenuNodeDefinition.php b/Config/Definition/Builder/MenuNodeDefinition.php index ab19f9b..d9bbd0a 100644 --- a/Config/Definition/Builder/MenuNodeDefinition.php +++ b/Config/Definition/Builder/MenuNodeDefinition.php @@ -66,6 +66,10 @@ public function menuNodeHierarchy($depth = 10) ->prototype('scalar') ->end() ->end() + ->arrayNode('extras') + ->prototype('scalar') + ->end() + ->end() ->menuNode('children')->menuNodeHierarchy($depth - 1) ->end() ->end(); diff --git a/DependencyInjection/JbConfigKnpMenuExtension.php b/DependencyInjection/JbConfigKnpMenuExtension.php index 7208a12..20659b7 100644 --- a/DependencyInjection/JbConfigKnpMenuExtension.php +++ b/DependencyInjection/JbConfigKnpMenuExtension.php @@ -66,7 +66,6 @@ public function load(array $configs, ContainerBuilder $container) $container ->getDefinition('jb_config.menu.provider') ->addArgument($configuredMenus); - } /** diff --git a/Provider/ConfigurationMenuProvider.php b/Provider/ConfigurationMenuProvider.php index 643d7fd..c993bb6 100644 --- a/Provider/ConfigurationMenuProvider.php +++ b/Provider/ConfigurationMenuProvider.php @@ -182,6 +182,11 @@ protected function createItem($parentItem, $name, $configuration) if (!empty($configuration['labelAttributes'])) { $item->setLabelAttributes($configuration['labelAttributes']); } + + // set extras + if (!empty($configuration['extras'])) { + $item->setExtras($configuration['extras']); + } // set display if (isset($configuration['display'])) { diff --git a/README.md b/README.md index 15a88a5..f853ce9 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ my_mega_menu: display: boolean to hide the item displayChildren: boolean to hide the children roles: array of item (string roles) passed to isGranted securityContext method to check if user has rights in menu items + extras: An array of extra parameters (for example icon img, additional content etc.) children: # An array of subitems second_level_item: label: My second level diff --git a/Tests/DependencyInjection/Fixtures/Bundle2/Resources/config/navigation.yml b/Tests/DependencyInjection/Fixtures/Bundle2/Resources/config/navigation.yml index 30c1db2..2a6a68d 100644 --- a/Tests/DependencyInjection/Fixtures/Bundle2/Resources/config/navigation.yml +++ b/Tests/DependencyInjection/Fixtures/Bundle2/Resources/config/navigation.yml @@ -24,9 +24,12 @@ main: test: test4 display: false displayChildren: false + extras: + key1: value1 + key2: value2 four_item: label: "Four Item Label" order: 40 five_item: label: "Five Item Label" - order: 40 \ No newline at end of file + order: 40 diff --git a/Tests/DependencyInjection/NavigationConfigurationTest.php b/Tests/DependencyInjection/NavigationConfigurationTest.php index a0d253e..3ea57bb 100644 --- a/Tests/DependencyInjection/NavigationConfigurationTest.php +++ b/Tests/DependencyInjection/NavigationConfigurationTest.php @@ -72,7 +72,8 @@ public static function buildRandomMenuItem($number = 1) 'display' => true, 'displayChildren' => true, 'children' => array(), - 'roles' => array() + 'roles' => array(), + 'extras' => array() ); } @@ -121,6 +122,7 @@ public function getInvalidTypeData() array( array('tree' => array('item1' => array('displayChildren' => array()))) ), array( array('tree' => array('item1' => array('children' => ''))) ), array( array('tree' => array('item1' => array('labelAttributes' => ''))) ), + array( array('tree' => array('item1' => array('extras' => ''))) ), ); } diff --git a/Tests/Provider/ConfigurationMenuProviderTest.php b/Tests/Provider/ConfigurationMenuProviderTest.php index c40b1f9..572a774 100644 --- a/Tests/Provider/ConfigurationMenuProviderTest.php +++ b/Tests/Provider/ConfigurationMenuProviderTest.php @@ -111,6 +111,11 @@ public function testGet() $menu->getChild('third_item')->getDisplayChildren(), 'Third item display children' ); + $this->assertEquals( + array('key1' => 'value1', 'key2' => 'value2'), + $menu->getChild('third_item')->getExtras(), + 'Third item extras' + ); $position = 0; foreach ($menu->getChildren() as $key => $item) {