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

Commit dd1c663

Browse files
committed
adjust menu event example
1 parent 542c05c commit dd1c663

File tree

1 file changed

+79
-46
lines changed

1 file changed

+79
-46
lines changed

bundles/menu/menu_factory.rst

Lines changed: 79 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ A menu item should have a URL associated with it. The CMF provides the
1919
``MenuFactory``.
2020

2121
* The ``MenuFactory`` only supports using the ``uri`` option to specify the
22-
menu items link.
22+
menu items link.
2323
* The ``RouterAwareFactory`` adds support for for generating a URL from the
2424
``route`` and ``routeParameters`` options.
2525
* The CMF adds the ``ContentAwareFactory`` which supports generating the URL
@@ -44,7 +44,7 @@ of the three URL generation techniques to use.
4444
The values for this options can be one of the following:
4545

4646
* ``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
4848
``content`` options and using the first one which is non-null.
4949
* **uri**: Use the URI provided by the ``uri`` option.
5050
* **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
6262

6363
.. versionadded:: 1.1
6464
The ``MenuContentVoter`` was added in CmfMenuBundle 1.1.
65-
65+
6666
The ``MenuContentVoter`` decides that a menu node is not published if the
6767
content it is pointing to is not published.
6868

6969
Customizing Menus using Events
7070
------------------------------
7171

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
7676
``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
8080
``MenuItem`` generation for unpublished nodes.
8181

8282
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
8484
``NodeInterface``.
8585

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;
9898
* ``CreateMenuItemFromNodeEvent::setSkipChildren(true|false)``: Listeners can
9999
set this to true and the ``ContentAwareFactory`` will skip processing of the
100100
children of the current node.
101101

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+
~~~~~~~~~~~~~~~~~~~~~
108104

109-
.. code-block:: php
105+
This listener handles menu nodes that point to a different menu by implementing
106+
the ``MenuReferrerInterface``::
110107

111-
namespace Example\Menu;
108+
namespace Acme\DemoBundle;
112109

113110
interface MenuReferrerInterface
114111
{
115112
public function getMenuName();
116113
public function getMenuOptions();
117114
}
118115

119-
namespace Example\Event;
116+
namespace Acme\DemoBundle\EventListener;
120117

121118
use Symfony\Cmf\Bundle\MenuBundle\Event\CreateMenuItemFromNodeEvent;
122-
use Example\Menu\MenuReferrerInterface;
119+
use Acme\DemoBundle\MenuReferrerInterface;
123120
use Knp\Menu\Provider\MenuProviderInterface;
124121

125122
class CreateMenuItemFromNodeListener
@@ -134,30 +131,66 @@ a menu provider service.
134131
public function onCreateMenuItemFromNode(CreateMenuItemFromNodeEvent $event)
135132
{
136133
$node = $event->getNode();
137-
134+
138135
if ($node implements MenuReferrerInterface) {
139136
$menuName = $node->getMenuName();
140137
$menuOptions = $node->getMenuOptions();
141138

142-
if ( ! $this->provider->has($menuName)) {
139+
if (!$this->provider->has($menuName)) {
143140
return;
144141
}
145142

146143
$menu = $this->provider->get($menuName, $menuOptions);
147144
$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.
148148
}
149149
}
150150

151151
}
152152

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

Comments
 (0)