-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added default_menu_options configuration
- Loading branch information
Showing
9 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |