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

Commit 31d6177

Browse files
committed
cleaning up the menu documentation
1 parent c6479f8 commit 31d6177

File tree

1 file changed

+82
-66
lines changed

1 file changed

+82
-66
lines changed

bundles/menu.rst

Lines changed: 82 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,31 @@
55
MenuBundle
66
==========
77

8-
The `MenuBundle`_ provides menus from a doctrine object manager with the help
9-
of `KnpMenuBundle`_.
8+
The CmfMenuBundle extends the `KnpMenuBundle`_ by adding a menu provider
9+
that loads menus from a doctrine object manager and generating menu URLs
10+
from content.
11+
12+
.. caution::
13+
14+
Make sure you understand how the `KnpMenuBundle`_ works when you want to
15+
customize the CmfMenuBundle. The CMF menu bundle is just adding
16+
functionality on top of the Knp menu bundle and this documentation is
17+
focused on the additional functionality.
1018

1119
.. index:: MenuBundle
1220

1321
Dependencies
1422
------------
1523

16-
This bundle is extending the `KnpMenuBundle`_.
24+
This bundle is extending the `KnpMenuBundle`_. Unless you change defaults and
25+
provide your own implementations, this bundle also depends on:
1726

18-
Unless you change defaults and provide your own implementations, this bundle
19-
also depends on
20-
21-
* ``SymfonyRoutingBundle`` for the router service
22-
``cmf_routing.dynamic_router``. Note that you need to explicitly
23-
enable the dynamic router as per default it is not loaded. See the
24-
:doc:`documentation of the cmf routing bundle <routing>` for how to do this.
25-
* :doc:`PHPCR-ODM <phpcr_odm>` to load route documents from the content repository
27+
* **CmfRoutingBundle** - if you want to generate routes for content objects.
28+
Note that you need to explicitly enable the dynamic router as per default it
29+
is not loaded. See the
30+
:doc:`documentation of the cmf routing bundle <routing>` for more information.
31+
* :doc:`PHPCR-ODM <phpcr_odm>` - to load route documents from the content
32+
repository when using the ``PHPCRMenuProvider``.
2633

2734
Configuration
2835
-------------
@@ -57,34 +64,80 @@ The values are:
5764
If you want to render the menu from twig, make sure you have not disabled twig
5865
in the ``knp_menu`` configuration section.
5966

60-
If ``sonata-project/doctrine-phpcr-admin-bundle`` is added to the
61-
composer.json require section, the menu documents are exposed in the
62-
SonataDoctrinePhpcrAdminBundle. For instructions on how to configure this
67+
If you have ``sonata-project/doctrine-phpcr-admin-bundle`` in your
68+
``composer.json`` require section, the menu documents are exposed in the
69+
SonataDoctrinePhpcrAdminBundle. For instructions on how to configure this
6370
Bundle see :doc:`doctrine_phpcr_admin`.
6471

6572
By default, ``use_sonata_admin`` is automatically set based on whether
66-
SonataDoctrinePhpcrAdminBundle is available but you can explicitly disable it
73+
SonataDoctrinePhpcrAdminBundle is available, but you can explicitly disable it
6774
to not have it even if sonata is enabled, or explicitly enable to get an error
6875
if Sonata becomes unavailable.
6976

70-
By default, menu nodes that have neither the uri nor the route field set and no
71-
route can be generated from the content they link to are skipped by the
77+
By default, menu nodes that have neither the URI nor the routeName field set
78+
and no route can be generated from the linked content are skipped by the
7279
``ContentAwareFactory``. This also leads to their descendants not showing up.
7380
If you want to generate menu items without a link instead, set the
7481
``allow_empty_items`` parameter to true to make the menu items show up as
75-
static text instead.
82+
plain text instead.
7683

7784
Menu Entries
7885
------------
7986

80-
A ``MenuItem`` document defines menu entries. You can build menu items based
81-
on symfony routes, absolute or relative urls or referenceable PHPCR-ODM
82-
content documents.
87+
A ``MenuNode`` document defines a menu entry. You can build entries based
88+
on symfony route names, absolute or relative URLs or referenceable PHPCR-ODM
89+
content documents. Route names and URLs are provided directly by `KnpMenu`_.
90+
Generating routes from content objects is a feature of the CmfRoutingBundle.
91+
92+
Menu Provider
93+
~~~~~~~~~~~~~
94+
95+
A menu provider is responsible to load a menu when it is requested. KnpMenu
96+
supports having several providers. The CmfMenuBundle provides the
97+
``PHPCRMenuProvider`` to load menu items from PHPCR-ODM.
98+
99+
Every menu has a name and is loaded by that name. The ``PHPCRMenuProvider``
100+
locates menus by looking at ``persistence.phpcr.menu_basepath``/``<menuname>``.
101+
You can use custom document classes for menu nodes if needed, as long as they
102+
implement ``Knp\Menu\NodeInterface`` to integrate with KnpMenuBundle. The
103+
default ``MenuNode`` class discards children that do not implement the
104+
``Knp\Menu\NodeInterface``.
105+
106+
.. note::
107+
108+
There is currently no support for Doctrine ORM or other persistence
109+
managers. This is not by design, but only because nobody built that yet.
110+
We would be glad for a pull request refactoring ``PHPCRMenuProvider`` into
111+
a base class suitable for all doctrine implementations, and storage
112+
specific providers.
113+
114+
You can also write your completely custom provider and register it with the
115+
KnpMenu as explained in the `KnpMenuBundle custom provider documentation`_.
116+
117+
Menu Factory
118+
~~~~~~~~~~~~
119+
120+
The menu nodes need to be converted into menu items. This is the job of the
121+
menu factory. A menu item should have a URL associated with it. KnpMenu can
122+
either take the ``uri`` field from the options, or generate a URL from the
123+
``route`` and ``routeParameters`` options. The CmfMenuBundle provides the
124+
``ContentAwareFactory`` that supports to generate the URL from the ``content``
125+
option that contains an object the ``DynamicRouter`` can generate a URL for, plus
126+
eventual ``routeParameters``. Thus a menu node can link to a content object or
127+
a route object in the database and put that object into the options to have the
128+
URL generated.
129+
130+
URL generation is absolute or relative, depending on ``routeAbsolute``.
131+
If you specify the ``linkType`` option, you can control how the URL is
132+
generated. If this parameter is missing, it is determined automatically,
133+
tacking in order ``uri``,``route`` or ``content``.
134+
135+
.. note::
83136

84-
The menu tree is built from documents under [menu_basepath]/[menuname]. You
85-
can use different document classes for menu items as long as they implement
86-
``Knp\Menu\NodeInterface`` to integrate with KnpMenuBundle. The default
87-
``MenuNode`` document discards children that do not implement this interface.
137+
If you just want to generate normal Symfony routes with a menu that is in
138+
the database, simply make sure to never provide a ``content`` option and
139+
either provide the ``route`` and eventual ``routeParameters`` or the
140+
``uri``.
88141

89142
Examples::
90143

@@ -130,20 +183,8 @@ Examples::
130183

131184
$dm->flush();
132185

133-
By default content documents are created using a **weak** reference (this
134-
means you will be able to delete the referenced content). You can specify a
135-
strong reference by using ``setWeak(false)``::
136-
137-
<?php
138-
139-
$node = new MenuNode;
140-
// ...
141-
$node->setWeak(false);
142-
143-
.. note::
144-
145-
When content is referenced weakly and subsequently deleted the rendered
146-
menu will not provide a link to the content.
186+
The default PHPCR-ODM mapping links content documents by a **weak** reference,
187+
which means you are able to delete the referenced content.
147188

148189
Current Menu Item
149190
-----------------
@@ -342,38 +383,13 @@ The menu name is the name of the node under ``menu_basepath``. For example if
342383
your repository stores the menu nodes under ``/cms/menu`` , rendering "main"
343384
would mean to render the menu that is at ``/cms/menu/main``
344385

345-
How to use Non-Default Other Components
346-
---------------------------------------
347-
348-
If you use the cmf menu with PHPCR-ODM, you just need to store Route documents
349-
under ``menu_basepath``. If you use a different object manager, you need to
350-
make sure that the menu root document is found with::
351-
352-
$dm->find($menu_document_class, $menu_basepath . $menu_name)
353-
354-
The route document must implement ``Knp\Menu\NodeInterface`` - see
355-
``MenuNode`` document for an example. You probably need to specify
356-
menu_document_class too, as only PHPCR-ODM can determine the document from the
357-
database content.
358-
359-
If you use the cmf menu with the DynamicRouter, you need no route name as the
360-
menu document just needs to provide a field content_key in the options. If
361-
you want to use a different service to generate URLs, you need to make sure
362-
your menu entries provide information in your selected content_key that the
363-
url generator can use to generate the url. Depending on your generator, you
364-
might need to specify a route_name too.
365-
366-
Note that if you just want to generate normal symfony routes with a menu that
367-
is in the database, you can pass the core router service as
368-
content_url_generator, make sure the content_key never matches and make your
369-
menu documents provide the route name and eventual routeParameters.
370-
371386
Publish Workflow Interface
372387
--------------------------
373388

374389
Menu nodes implement the write interfaces for publishable and publish time
375390
period, see the :ref:`publish workflow documentation <bundle-core-publish_workflow>`
376391
for more information.
377392

378-
.. _`MenuBundle`: https://github.com/symfony-cmf/MenuBundle#readme
393+
.. _`KnpMenu`: https://github.com/knplabs/KnpMenu
379394
.. _`KnpMenuBundle`: https://github.com/knplabs/KnpMenuBundle
395+
.. _`KnpMenuBundle custom provider documentation`: https://github.com/KnpLabs/KnpMenuBundle/blob/master/Resources/doc/custom_provider.md

0 commit comments

Comments
 (0)