@@ -19,7 +19,7 @@ A menu item should have a URL associated with it. The CMF provides the
19
19
``MenuFactory ``.
20
20
21
21
* The ``MenuFactory `` only supports using the ``uri `` option to specify the
22
- menu items link.
22
+ menu items link.
23
23
* The ``RouterAwareFactory `` adds support for for generating a URL from the
24
24
``route `` and ``routeParameters `` options.
25
25
* The CMF adds the ``ContentAwareFactory `` which supports generating the URL
@@ -44,7 +44,7 @@ of the three URL generation techniques to use.
44
44
The values for this options can be one of the following:
45
45
46
46
* ``null ``: If the value is ``null `` (or the options is not set) then the link
47
- type is determined automatically by looking at each of the ``uri ``, ``route `` and
47
+ type is determined automatically by looking at each of the ``uri ``, ``route `` and
48
48
``content `` options and using the first one which is non-null.
49
49
* **uri **: Use the URI provided by the ``uri `` option.
50
50
* **route **: Generate a URL using the route named by the ``route `` option
@@ -62,64 +62,61 @@ visible by use of the :doc:`publish workflow checker
62
62
63
63
.. versionadded :: 1.1
64
64
The ``MenuContentVoter `` was added in CmfMenuBundle 1.1.
65
-
65
+
66
66
The ``MenuContentVoter `` decides that a menu node is not published if the
67
67
content it is pointing to is not published.
68
68
69
69
Customizing Menus using Events
70
70
------------------------------
71
71
72
- The CMF menu factory dispatches a ``cmf_menu.create_menu_item_from_node `` event
73
- during the process of creating a ``MenuItem `` from a class implementing
74
- ``NodeInterface ``. You can use this event to control the ``MenuItem `` that is
75
- created. The ``CreateMenuItemFromNodeEvent `` provides access to the
72
+ The CMF menu factory dispatches a ``cmf_menu.create_menu_item_from_node `` event
73
+ during the process of creating a ``MenuItem `` from a class implementing
74
+ ``NodeInterface ``. You can use this event to control the ``MenuItem `` that is
75
+ created. The ``CreateMenuItemFromNodeEvent `` provides access to the
76
76
``NodeInterface `` and ``ContentAwareFactory ``, which can be used to create a
77
- custom ``MenuItem ``, or to tell the factory to ignore the current node or its
78
- children. For example, this event is used by the
79
- :doc: `publish workflow checker <../core/publish_workflow >` to skip
77
+ custom ``MenuItem ``, or to tell the factory to ignore the current node or its
78
+ children. For example, this event is used by the
79
+ :doc: `publish workflow checker <../core/publish_workflow >` to skip
80
80
``MenuItem `` generation for unpublished nodes.
81
81
82
82
The ``CreateMenuItemFromNodeEvent `` which is dispatched includes the following
83
- methods which can be used to customize the creation of the ``MenuItem `` for a
83
+ methods which can be used to customize the creation of the ``MenuItem `` for a
84
84
``NodeInterface ``.
85
85
86
- * ``CreateMenuItemFromNodeEvent::setSkipNode(true|false) ``: Setting skipNode to
87
- true will prevent creation of item from the node and skip any child nodes.
88
- **Note: ** If setSkipNode(true) is called for ``Menu `` the `` ContentAwareFactory ``
89
- will still create an empty item for the menu. This is to prevent the KnpMenuBundle
90
- code from throwing an Exception due to null being passed to a function to render a
91
- menu.
92
- * ``CreateMenuItemFromNodeEvent::setItem(ItemInterface $item|null) ``: A listener
93
- can call setItem to provide a custom item to use for the given node. If an
94
- item is set, the ``ContentAwareFactory `` will use it instead of creating one for
95
- the node. The children of the node will still be processed by the
96
- ``ContentAwareFactory `` and listeners will have an opportunity then to override
97
- their items using this method.
86
+ * ``CreateMenuItemFromNodeEvent::setSkipNode(true|false) ``: Setting skipNode
87
+ to true will prevent creation of item from the node and skip any child nodes.
88
+ **Note: ** If setSkipNode(true) is called for ``Menu `` the
89
+ `` ContentAwareFactory `` will still create an empty item for the menu. This is
90
+ to prevent the KnpMenuBundle code from throwing an exception due to `` null ``
91
+ being passed to a function to render a menu;
92
+ * ``CreateMenuItemFromNodeEvent::setItem(ItemInterface $item|null) ``: A
93
+ listener can call setItem to provide a custom item to use for the given node.
94
+ If an item is set, the ``ContentAwareFactory `` will use it instead of
95
+ creating one for the node. The children of the node will still be processed
96
+ by the ``ContentAwareFactory `` and listeners will have an opportunity then to
97
+ override their items using this method;
98
98
* ``CreateMenuItemFromNodeEvent::setSkipChildren(true|false) ``: Listeners can
99
99
set this to true and the ``ContentAwareFactory `` will skip processing of the
100
100
children of the current node.
101
101
102
- Example Listener
103
- ~~~~~~~~~~~~~~~~
104
-
105
- Example PHP code for a listener that would replace nodes that implement
106
- an interface ``MenuReferrerInterface `` with a menu generated by
107
- a menu provider service.
102
+ Example Menu Listener
103
+ ~~~~~~~~~~~~~~~~~~~~~
108
104
109
- .. code-block :: php
105
+ This listener handles menu nodes that point to a different menu by implementing
106
+ the ``MenuReferrerInterface ``::
110
107
111
- namespace Example\Menu ;
108
+ namespace Acme\DemoBundle ;
112
109
113
110
interface MenuReferrerInterface
114
111
{
115
112
public function getMenuName();
116
113
public function getMenuOptions();
117
114
}
118
115
119
- namespace Example\Event ;
116
+ namespace Acme\DemoBundle\EventListener ;
120
117
121
118
use Symfony\Cmf\Bundle\MenuBundle\Event\CreateMenuItemFromNodeEvent;
122
- use Example\Menu \MenuReferrerInterface;
119
+ use Acme\DemoBundle \MenuReferrerInterface;
123
120
use Knp\Menu\Provider\MenuProviderInterface;
124
121
125
122
class CreateMenuItemFromNodeListener
@@ -134,30 +131,66 @@ a menu provider service.
134
131
public function onCreateMenuItemFromNode(CreateMenuItemFromNodeEvent $event)
135
132
{
136
133
$node = $event->getNode();
137
-
134
+
138
135
if ($node implements MenuReferrerInterface) {
139
136
$menuName = $node->getMenuName();
140
137
$menuOptions = $node->getMenuOptions();
141
138
142
- if ( ! $this->provider->has($menuName)) {
139
+ if (! $this->provider->has($menuName)) {
143
140
return;
144
141
}
145
142
146
143
$menu = $this->provider->get($menuName, $menuOptions);
147
144
$event->setItem($menu);
145
+
146
+ // as this does not call $event->setSkipChildren(true),
147
+ // children of $node will be rendered as children items of $menu.
148
148
}
149
149
}
150
150
151
151
}
152
152
153
- Service definition
154
-
155
- .. code-block :: xml
156
-
157
- <service id =" cmf_menu.listener.menu_referrer_listener" class =" Example\Event\CreateMenuItemFromNodeListener" >
158
- <argument type =" service" id =" knp_menu.menu_provider" />
159
- <tag name =" kernel.event_listener" event =" cmf_menu.create_menu_item_from_node" method =" onCreateMenuItemFromNode" />
160
- </service >
161
-
162
- **Note ** In this case since the listener did not call ``$event->setSkipChildren(true) `` any nodes
163
- that are children of the node being processed will still be added to the menu that is created.
153
+ The service needs to be tagged as event listener:
154
+
155
+ .. configuration-block ::
156
+
157
+ .. code-block :: yaml
158
+
159
+ services :
160
+ acme_demo.listener.menu_referrer_listener :
161
+ class : Acme\DemoBundle\EventListener\CreateMenuItemFromNodeListener
162
+ arguments :
163
+ - @knp_menu.menu_provider
164
+ tags :
165
+ -
166
+ name : kernel.event_listener
167
+ event : cmf_menu.create_menu_item_from_node
168
+ method : onCreateMenuItemFromNode
169
+
170
+ .. code-block :: xml
171
+
172
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
173
+ <container xmlns =" http://symfony.com/schema/dic/services" >
174
+ <service id =" acme_demo.listener.menu_referrer_listener" class =" Acme\DemoBundle\EventListener\CreateMenuItemFromNodeListener" >
175
+ <argument type =" service" id =" knp_menu.menu_provider" />
176
+ <tag name =" kernel.event_listener"
177
+ event =" cmf_menu.create_menu_item_from_node"
178
+ method =" onCreateMenuItemFromNode"
179
+ />
180
+ </service >
181
+ </container >
182
+
183
+ .. code-block :: php
184
+
185
+ use Symfony\Component\DependencyInjection\Definition;
186
+ use Symfony\Component\DependencyInjection\Reference;
187
+
188
+ $definition = new Definition('Acme\DemoBundle\EventListener\CreateMenuItemFromNodeListener', array(
189
+ new Reference('knp_menu.menu_provider'),
190
+ ));
191
+ $definition->addTag('kernel.event_listener', array(
192
+ 'event' => 'cmf_menu.create_menu_item_from_node',
193
+ 'method' => 'onCreateMenuItemFromNode',
194
+ ));
195
+
196
+ $container->setDefinition('acme_demo.listener.menu_referrer_listener', $definition);
0 commit comments