Skip to content
This repository has been archived by the owner on Feb 22, 2019. It is now read-only.

Commit

Permalink
feat #2 add more configuration options for navigation (sstok)
Browse files Browse the repository at this point in the history
This PR was merged into the master branch.

Discussion
----------

|Q            |A  |
|---          |---|
|Bug Fix      |yes|
|New Feature  |yes|
|Deprecations |no |
|Fixed Tickets|   |
|License      |MIT|

 Sent using [Gush](https://github.com/gushphp/gush)

Commits
-------

92424e2 add more configuration options for navigation

- options (passed directly to the MenuItem)
- uri (as alternative to route) (sstok)
  • Loading branch information
sstok committed Nov 9, 2014
2 parents afa4d65 + 92424e2 commit da6e32b
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 31 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ rollerworks_navigation:
label: ~ # Label of the breadcrumb will be translated with the translator_domain
translator_domain: Menus # translator domain for the label
route: { name: ~, parameters: { } } # name can not be empty
uri: ~ # Alternatively you can use a URI instead of a route
items: [] # Sub-level items, same as this example (unlimited depth nesting)

# alternatively you can reference a service for getting the Menu object
Expand Down Expand Up @@ -124,6 +125,7 @@ rollerworks_navigation:
label: ~ # Label of the breadcrumb will be translated with the translator_domain
translator_domain: Breadcrumbs # translator domain for the label
route: { name: ~, parameters: { } } # name can not be empty
uri: ~ # Alternatively you can use a URI instead of a route
# alternatively you can reference a service for getting the Menu object
# The service must return a Knp\Menu\ItemInterface instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,16 @@ public function getConfigTreeBuilder()
->children()
->scalarNode('parent')->defaultNull()->end()
->scalarNode('label')->defaultNull()->end()
->arrayNode('options')
->useAttributeAsKey('name')
->prototype('variable')->end()
->end()
->scalarNode('translator_domain')->defaultValue('Breadcrumbs')->end()
->scalarNode('uri')->defaultNull()->end()
->arrayNode('route')
->children()
->scalarNode('name')->cannotBeEmpty()->end()
->booleanNode('absolute')->defaultFalse()->end()
->arrayNode('parameters')
->prototype('variable')->end()
->end()
Expand Down Expand Up @@ -94,17 +100,22 @@ final public function addItemConfig(ArrayNodeDefinition $rootNode)
->children()
->scalarNode('expression')->defaultNull()->end()
->scalarNode('label')->defaultNull()->end()
->arrayNode('options')
->useAttributeAsKey('name')
->prototype('variable')->end()
->end()
->scalarNode('translator_domain')->defaultValue('Menus')->end()
->scalarNode('uri')->defaultNull()->end()
->arrayNode('route')
->children()
->scalarNode('name')->cannotBeEmpty()->end()
->booleanNode('absolute')->defaultFalse()->end()
->arrayNode('parameters')
->useAttributeAsKey('name')
->prototype('variable')->end()
->end()
->end()
->end()

->arrayNode('service')
->children()
->scalarNode('id')->cannotBeEmpty()->end()
Expand All @@ -114,7 +125,6 @@ final public function addItemConfig(ArrayNodeDefinition $rootNode)
->end()
->end()
->end()

->arrayNode('items')
->useAttributeAsKey('name')
->prototype('variable')->end() // use variable as we can't nest to deep
Expand All @@ -124,6 +134,10 @@ final public function addItemConfig(ArrayNodeDefinition $rootNode)
->ifTrue(function ($v) { return !empty($v['service']) && (!empty($v['expression']) || null !== $v['label']); })
->thenInvalid('When a "service" or "expression" is set no other configurations should be set for this item.')
->end()
->validate()
->ifTrue(function ($v) { return !empty($v['route']) && !empty($v['uri']); })
->thenInvalid('An item can only have a route or uri, not both.')
->end()
->validate()
->ifTrue(function ($v) { return empty($v['service']) && empty($v['expression']) && null === $v['label']; })
->thenInvalid('Missing a value for either "service" or "label" for this item.')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private function registerBreadcrumbs(array $breadcrumbs, ContainerBuilder $conta
*
* @return Definition
*/
private function createMenuItem($name, array $options = array())
private function createMenuItem($name, $options = array())
{
unset($options['items']);

Expand All @@ -152,9 +152,16 @@ private function createMenuItem($name, array $options = array())
private function createMenuItemDefinition($name, array $item)
{
if (isset($item['route']['parameters'])) {
$item['route']['parameters'] = $this->resolveParameters($item['route']['parameters']);
$item['routeParameters'] = $this->resolveParameters($item['route']['parameters']);
$item['routeAbsolute'] = $this->resolveParameters($item['route']['absolute']);
$item['route'] = $this->resolveParameters($item['route']['name']);
}

$item['options'] = $this->resolveParameters($item['options']);
$item = array_merge($item['options'], $item);

unset($item['options']);

if (!empty($item['service'])) {
$definition = new Definition('stdClass');
$definition->setFactoryService($item['service']['id']);
Expand Down Expand Up @@ -234,7 +241,7 @@ private function validateMenuItemConfig(array $configs)
*
* @param string $value
*
* @return Reference
* @return mixed
*/
private function resolveParameters($value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public function testBreadcrumbsWithDefaulted()
'customers' => array(
'parent' => null,
'label' => null,
'options' => array(),
'translator_domain' => 'Breadcrumbs',
'uri' => null,
'expression' => null,
),
),
Expand Down Expand Up @@ -81,7 +83,9 @@ public function testBreadcrumbAcceptsService()
'customer' => array(
'parent' => null,
'label' => null,
'options' => array(),
'translator_domain' => 'Breadcrumbs',
'uri' => null,
'service' => array(
'id' => 'acme_customer.navigation',
'method' => 'getBreadcrumb',
Expand All @@ -92,7 +96,9 @@ public function testBreadcrumbAcceptsService()
'webhosting' => array(
'parent' => null,
'label' => null,
'options' => array(),
'translator_domain' => 'Breadcrumbs',
'uri' => null,
'service' => array(
'id' => 'acme_webhosting.navigation',
'method' => 'getBreadcrumb',
Expand Down
Loading

0 comments on commit da6e32b

Please sign in to comment.