New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to configure default menu options #278
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Knp\Bundle\MenuBundle\Extension; | ||
|
||
use Knp\Menu\Factory\ExtensionInterface; | ||
use Knp\Menu\ItemInterface; | ||
|
||
/** | ||
* An extension to configure the default menu options. | ||
* | ||
* @author Wouter J <wouter@wouterj.nl> | ||
*/ | ||
class DefaultOptionsExtension implements ExtensionInterface | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $options; | ||
|
||
public function __construct(array $options) | ||
{ | ||
$this->options = $options; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildOptions(array $options = array()) | ||
{ | ||
return array_merge($this->options, $options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. many options of the library are arrays. This makes them hard to use with your extension There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So you suggest using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know what to use here. |
||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildItem(ItemInterface $item, array $options) | ||
{ | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?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"> | ||
|
||
<services> | ||
<service id="knp_menu.extension.default_options" class="Knp\Bundle\MenuBundle\Extension\DefaultOptionsExtension"> | ||
<tag name="knp_menu.factory_extension" /> | ||
<argument>%knp_menu.default_menu_options%</argument> | ||
</service> | ||
</services> | ||
|
||
</container> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is no |
||
|
||
.. 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')); |
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))); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is always set (prototyped array nodes have a default value of empty array)