Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 783c6da

Browse files
author
mkoosej
committed
Add MenuOptionsExtension
Allow editing the menu options in sonata admin
1 parent f7bd490 commit 783c6da

File tree

9 files changed

+330
-2
lines changed

9 files changed

+330
-2
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2014 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Bundle\MenuBundle\Admin\Extension;
13+
14+
use Sonata\AdminBundle\Admin\AdminExtension;
15+
use Sonata\AdminBundle\Form\FormMapper;
16+
17+
/**
18+
* Admin extension for editing menu options
19+
* implementing MenuOptionsInterface.
20+
*
21+
* @author Mojtaba Koosej <[email protected]>
22+
*/
23+
class MenuOptionsExtension extends AdminExtension
24+
{
25+
/**
26+
* @var string
27+
*/
28+
protected $formGroup;
29+
30+
/**
31+
* @var bool
32+
*/
33+
protected $advanced;
34+
35+
/**
36+
* @param string $formGroup - group to use for form mapper
37+
* @param bool $advanced - activates editing all fields of the node
38+
*/
39+
public function __construct($formGroup = 'form.group_menu_options', $advanced = false)
40+
{
41+
$this->formGroup = $formGroup;
42+
$this->advanced = $advanced;
43+
}
44+
45+
/**
46+
* {@inheritDoc}
47+
*/
48+
public function configureFormFields(FormMapper $formMapper)
49+
{
50+
$formMapper->with($this->formGroup, array(
51+
'translation_domain' => 'CmfMenuBundle',
52+
))
53+
->add(
54+
'display',
55+
'checkbox',
56+
array('required' => false)
57+
)
58+
->add(
59+
'displayChildren',
60+
'checkbox',
61+
array('required' => false)
62+
)
63+
->end();
64+
65+
if (! $this->advanced) {
66+
return;
67+
}
68+
69+
$child_options = array(
70+
'value_type' => 'text',
71+
'label' => false,
72+
'attr'=> array('style' => 'clear:both')
73+
);
74+
75+
$formMapper->with($this->formGroup, array(
76+
'translation_domain' => 'CmfMenuBundle',
77+
))
78+
->add(
79+
'attributes',
80+
'burgov_key_value',
81+
array(
82+
'value_type' => 'text',
83+
'required' => false,
84+
'options' => $child_options
85+
)
86+
)
87+
->add(
88+
'labelAttributes',
89+
'burgov_key_value',
90+
array(
91+
'value_type' => 'text',
92+
'required' => false,
93+
'options' => $child_options
94+
)
95+
)
96+
->add(
97+
'childrenAttributes',
98+
'burgov_key_value',
99+
array(
100+
'value_type' => 'text',
101+
'required' => false,
102+
'options' => $child_options
103+
)
104+
)
105+
->add(
106+
'linkAttributes',
107+
'burgov_key_value',
108+
array(
109+
'value_type' => 'text',
110+
'required' => false,
111+
'options' => $child_options
112+
)
113+
)
114+
->end();
115+
}
116+
}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Changelog
22
=========
33

4+
* **2014-07-11**: Added MenuOptionsExtension that adds the editing feature of menu options in Sonata Admin
45
* **2014-06-06**: Updated to PSR-4 autoloading
56

67
1.2.x

DependencyInjection/CmfMenuExtension.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Cmf\Bundle\MenuBundle\DependencyInjection;
1313

1414
use Symfony\Component\Config\FileLocator;
15+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1516
use Symfony\Component\DependencyInjection\ContainerBuilder;
1617
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
1718
use Symfony\Component\DependencyInjection\Reference;
@@ -23,6 +24,7 @@ class CmfMenuExtension extends Extension
2324
public function load(array $configs, ContainerBuilder $container)
2425
{
2526
$config = $this->processConfiguration(new Configuration(), $configs);
27+
$bundles = $container->getParameter('kernel.bundles');
2628

2729
$loader = new XmlFileLoader(
2830
$container,
@@ -40,6 +42,20 @@ public function load(array $configs, ContainerBuilder $container)
4042
$this->loadPhpcr($config['persistence']['phpcr'], $loader, $container);
4143
}
4244

45+
if ($config['admin_extensions']['menu_options']['enabled']) {
46+
if(!isset($bundles['SonataAdminBundle'])) {
47+
throw new InvalidConfigurationException('To use menu options extionsion, you need sonata-project/SonataAdminBundle in your project.');
48+
}
49+
50+
if ($config['admin_extensions']['menu_options']['advanced'] && !isset($bundles['BurgovKeyValueFormBundle'])) {
51+
throw new InvalidConfigurationException('To use advanced menu options, you need the burgov/key-value-bundle in your project.');
52+
}
53+
54+
$container->setParameter($this->getAlias() . '.admin_extensions.menu_options.advanced', $config['admin_extensions']['menu_options']['advanced']);
55+
56+
$loader->load('admin-extension.xml');
57+
}
58+
4359
if ($config['publish_workflow']['enabled']) {
4460
$loader->load('publish-workflow.xml');
4561
}
@@ -111,7 +127,6 @@ public function loadSonataAdmin($config, XmlFileLoader $loader, ContainerBuilder
111127
$config[$key.'_admin_class']
112128
);
113129
}
114-
115130
}
116131

117132
$loader->load('admin.xml');

DependencyInjection/Configuration.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ public function getConfigTreeBuilder()
6363
->end()
6464
->end()
6565

66+
->arrayNode('admin_extensions')
67+
->addDefaultsIfNotSet()
68+
->children()
69+
->arrayNode('menu_options')
70+
->addDefaultsIfNotSet()
71+
->canBeDisabled()
72+
->children()
73+
->booleanNode('advanced')->defaultValue(false)->end()
74+
->end()
75+
->end()
76+
->end()
77+
->end()
78+
6679
->arrayNode('publish_workflow')
6780
->addDefaultsIfNotSet()
6881
->canBeDisabled()

Model/MenuNode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Cmf\Bundle\CoreBundle\Translatable\TranslatableInterface;
1616
use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishTimePeriodInterface;
1717
use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishableInterface;
18+
use Symfony\Cmf\Bundle\MenuBundle\Model\MenuOptionsInterface;
1819

1920
/**
2021
* This is the standard CMF MenuNode implementation
@@ -34,6 +35,7 @@ class MenuNode extends MenuNodeBase implements
3435
TranslatableInterface,
3536
PublishTimePeriodInterface,
3637
PublishableInterface,
38+
MenuOptionsInterface,
3739
ChildInterface
3840
{
3941
/**

Model/MenuOptionsInterface.php

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\MenuBundle\Model;
4+
5+
use Knp\Menu\NodeInterface;
6+
7+
/**
8+
* Provide access to read and write the menu options.
9+
*
10+
* @author Mojtaba Koosej <[email protected]>
11+
*/
12+
13+
interface MenuOptionsInterface extends NodeInterface
14+
{
15+
/**
16+
* Whether or not to display this menu
17+
*
18+
* @return boolean
19+
*/
20+
function getDisplay();
21+
22+
/**
23+
* Set whether or not this menu should be displayed
24+
*
25+
* @param boolean $bool
26+
*
27+
* @return MenuOptionsInterface
28+
*/
29+
function setDisplay($bool);
30+
31+
/**
32+
* Whether or not this menu should show its children.
33+
*
34+
* @return boolean
35+
*/
36+
function getDisplayChildren();
37+
38+
/**
39+
* Set whether or not this menu should show its children
40+
*
41+
* @param boolean $bool
42+
*
43+
* @return MenuOptionsInterface
44+
*/
45+
function setDisplayChildren($bool);
46+
47+
/**
48+
* Return the attributes associated with this menu node
49+
*
50+
* @return array
51+
*/
52+
function getAttributes();
53+
54+
/**
55+
* Set the attributes associated with this menu node
56+
*
57+
* @param $attributes array
58+
*
59+
* @return Page The current Page instance
60+
*/
61+
function setAttributes(array $attributes);
62+
63+
/**
64+
* Return the given attribute, optionally specifying a default value
65+
*
66+
* @param string $name The name of the attribute to return
67+
* @param string $default The value to return if the attribute doesn't exist
68+
*
69+
* @return string
70+
*/
71+
function getAttribute($name, $default = null);
72+
73+
/**
74+
* Set the named attribute
75+
*
76+
* @param string $name attribute name
77+
* @param string $value attribute value
78+
*
79+
* @return Page The current Page instance
80+
*/
81+
function setAttribute($name, $value);
82+
83+
/**
84+
* Get the link HTML attributes.
85+
*
86+
* @return array
87+
*/
88+
function getLinkAttributes();
89+
90+
/**
91+
* Set the link HTML attributes as associative array.
92+
*
93+
* @param array $linkAttributes
94+
*
95+
* @return Page The current Page instance
96+
*/
97+
function setLinkAttributes($linkAttributes);
98+
99+
/**
100+
* Return the children attributes
101+
*
102+
* @return array
103+
*/
104+
function getChildrenAttributes();
105+
106+
/**
107+
* Set the children attributes
108+
*
109+
* @param array $attributes
110+
*
111+
* @return Page The current Page instance
112+
*/
113+
function setChildrenAttributes(array $childrenAttributes);
114+
115+
/**
116+
* Get the label HTML attributes.
117+
*
118+
* @return array
119+
*/
120+
function getLabelAttributes();
121+
122+
/**
123+
* Set the label HTML attributes as associative array.
124+
*
125+
* @param array $labelAttributes
126+
*
127+
* @return Page The current Page instance
128+
*/
129+
function setLabelAttributes($labelAttributes);
130+
131+
}

Resources/config/admin-extension.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<parameters>
8+
<parameter key="cmf_menu.admin_extension.menu_node_options.class">Symfony\Cmf\Bundle\MenuBundle\Admin\Extension\MenuOptionsExtension</parameter>
9+
</parameters>
10+
11+
<services>
12+
13+
<service id="cmf_menu.admin_extension.menu_options" class="%cmf_menu.admin_extension.menu_node_options.class%">
14+
<argument>form.group_menu_options</argument>
15+
<argument>%cmf_menu.admin_extensions.menu_options.advanced%</argument>
16+
<tag name="sonata.admin.extension"/>
17+
</service>
18+
19+
</services>
20+
21+
</container>

Resources/translations/CmfMenuBundle.en.xliff

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,34 @@
126126
<source>form.label_menu_nodes</source>
127127
<target>Menu</target>
128128
</trans-unit>
129+
<trans-unit id="form.group_menu_options">
130+
<source>form.group_menu_options</source>
131+
<target>Menu Options</target>
132+
</trans-unit>
133+
<trans-unit id="form.label_display">
134+
<source>form.label_display</source>
135+
<target>Display</target>
136+
</trans-unit>
137+
<trans-unit id="form.label_display_children">
138+
<source>form.label_display_children</source>
139+
<target>Display Children</target>
140+
</trans-unit>
141+
<trans-unit id="form.label_attributes">
142+
<source>form.label_attributes</source>
143+
<target>Menu attributes</target>
144+
</trans-unit>
145+
<trans-unit id="form.label_label_attributes">
146+
<source>form.label_label_attributes</source>
147+
<target>Label attributes</target>
148+
</trans-unit>
149+
<trans-unit id="form.label_children_attributes">
150+
<source>form.label_children_attributes</source>
151+
<target>Children attributes</target>
152+
</trans-unit>
153+
<trans-unit id="form.label_link_attributes">
154+
<source>form.label_link_attributes</source>
155+
<target>Link attributes</target>
156+
</trans-unit>
129157
</body>
130158
</file>
131159
</xliff>

0 commit comments

Comments
 (0)