Skip to content

Commit

Permalink
Added default_menu_options configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed Aug 27, 2015
1 parent 5c5b019 commit d9514b6
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Added DefaultOptionsExtension

## 2.0.0 (2014-08-01)

* Updated to KnpMenu 2 stable
Expand Down
6 changes: 6 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function getConfigTreeBuilder()

$rootNode
->fixXmlConfig('provider')
->fixXmlConfig('default_menu_option')
->children()
->arrayNode('providers')
->addDefaultsIfNotSet()
Expand All @@ -41,6 +42,11 @@ public function getConfigTreeBuilder()
->end()
->booleanNode('templating')->defaultFalse()->end()
->scalarNode('default_renderer')->cannotBeEmpty()->defaultValue('twig')->end()
->arrayNode('default_menu_options')
->useAttributeAsKey('name')
->normalizeKeys(false)
->prototype('scalar')->end()
->end()
->end();

return $treeBuilder;
Expand Down
5 changes: 5 additions & 0 deletions DependencyInjection/KnpMenuExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function load(array $configs, ContainerBuilder $container)
}

$container->setParameter('knp_menu.default_renderer', $config['default_renderer']);

if (isset($config['default_menu_options'])) {
$loader->load('options_extension.xml');
$container->setParameter('knp_menu.default_menu_options', $config['default_menu_options']);
}
}

public function getNamespace()
Expand Down
30 changes: 30 additions & 0 deletions Extension/DefaultOptionsExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Knp\Bundle\MenuBundle\Extension;

use Knp\Menu\Factory\ExtensionInterface;
use Knp\Menu\ItemInterface;

/**
* An extension to dynamically configure default menu options.
*
* @author Wouter J <wouter@wouterj.nl>
*/
class DefaultOptionsExtension implements ExtensionInterface
{
private $options;

public function __construct(array $options)
{
$this->options = $options;
}

public function buildOptions(array $options = array())
{
return array_merge($this->options, $options);
}

public function buildItem(ItemInterface $item, array $options)
{
}
}
18 changes: 18 additions & 0 deletions Resources/config/options_extension.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="knp_menu.extension.default_options.class">Knp\Bundle\MenuBundle\Extension\DefaultOptionsExtension</parameter>
</parameters>

<services>
<service id="knp_menu.extension.default_options" class="%knp_menu.extension.default_options.class%">
<tag name="knp_menu.factory_extension" />
<argument>%knp_menu.default_menu_options%</argument>
</service>
</services>

</container>
1 change: 1 addition & 0 deletions Resources/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ More Advanced Stuff
* :doc:`Custom Menu Provider <custom_provider>`
* :doc:`I18n for your menu labels <i18n>`
* :doc:`Using events to allow extending the menu <events>`
* :doc:`Setting Default Menu Options <menu_options>`

.. _`installation chapter`: https://getcomposer.org/doc/00-intro.md
.. _`KnpMenu documentation`: https://github.com/KnpLabs/KnpMenu/blob/master/doc/01-Basic-Menus.markdown
46 changes: 46 additions & 0 deletions Resources/doc/menu_options.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Setting Default Menu Options
============================

When creating a menu, menu options are set by passing them to ``addChild()``.
When all menu items need the same option, adding the same options to each child
can be cumbersome. In some cases, passing options isn't possible (e.g. when the
menu was created based on a database).

In these cases, you can configure the menu options in the configuration file:

.. configuration-block::

.. code-block:: yaml
# app/config/config.yml
knp_menu:
default_menu_options:
class: menu-item
.. code-block:: xml
<!-- app/config/config.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-Instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">
<config xmlns="http://knplabs.com/schema/dic/menu">
<default-menu-option name="class">menu-item</default-menu-option>
</config>
</container>
.. code-block:: php
// app/config/config.php
$container->loadFromExtension('knp_menu', array(
'default_menu_options' => array(
'class' => 'menu-item',
),
));
These options are overriden by options that are explicitely passed to the menu item::

// ...
$menu->addChild('Home', array('class' => 'menu-item menu-item-home'));
9 changes: 9 additions & 0 deletions Tests/DependencyInjection/KnpMenuExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,13 @@ public function testDisableContainerAwareProvider()
$this->assertTrue($container->getDefinition('knp_menu.menu_provider.builder_alias')->hasTag('knp_menu.provider'), 'The BuilderAliasProvider is enabled');
$this->assertFalse($container->getDefinition('knp_menu.menu_provider.container_aware')->hasTag('knp_menu.provider'), 'The ContainerAwareProvider is disabled');
}

public function testConfigureDefaultMenuOptions()
{
$container = new ContainerBuilder();
$loader = new KnpMenuExtension();
$loader->load(array(array('default_menu_options' => array('displayChildren' => false))), $container);
$this->assertEquals(array('displayChildren' => false), $container->getParameter('knp_menu.default_menu_options'));
$this->assertTrue($container->hasDefinition('knp_menu.extension.default_options'));
}
}
34 changes: 34 additions & 0 deletions Tests/Extension/DefaultOptionsExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Knp\Bundle\MenuBundle\Tests\Extension;

use Knp\Bundle\MenuBundle\Extension\DefaultOptionsExtension;

class DefaultOptionsExtensionTest extends \PHPUnit_Framework_TestCase
{
public function testSetsDefaultOptions()
{
$extension = new DefaultOptionsExtension(array(
'attributes' => array('class' => 'menu-item'),
'displayChildren' => false,
));

$this->assertEquals(array(
'attributes' => array('class' => 'menu-item'),
'displayChildren' => false,
), $extension->buildOptions());
}

public function testExplicitelyPassedOptionsOverwriteConfiguredDefaults()
{
$extension = new DefaultOptionsExtension(array(
'attributes' => array('class' => 'menu-item'),
'displayChildren' => false,
));

$this->assertEquals(array(
'attributes' => array('class' => 'menu-item'),
'displayChildren' => true,
), $extension->buildOptions(array('displayChildren' => true)));
}
}

0 comments on commit d9514b6

Please sign in to comment.