Add navigation menu¶
To add a navigation menu to your website, prepare a general layout template in a templates/themes/<theme_name>/pagelayout.html.twig
file.
This template can contain things such as header, menu, footer, as well as assets for the whole site, and all other templates extend it.
To select items that should be rendered in the menu, you can use one of the following ways:
- create a query
- create a MenuBuilder
Render menu using a query¶
To create a menu that contains a specific set of Content items, for example all content under the root Location, use a Query Type.
First, in src/QueryType
, create a custom MenuQueryType.php
file that queries for all items that you want in the menu:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
In this case, it queries for all visible children of Location 2
, the root Location, (lines 15-16)
and renders them in order according to their Location priority.
The Query Type has the name Menu
(line 28).
You can use it in the template to render the menu.
Add the following ez_render_content_query
function to the pagelayout_html.twig
template:
1 2 3 4 5 6 7 |
|
Next, add the templates/themes/<theme_name>/pagelayout_menu.html.twig
template,
which renders the individual items of the menu:
1 2 3 4 5 |
|
Create a MenuBuilder¶
To make a more configurable menu, where you select the specific items to render, use the KNPMenuBundle that is installed together with the product.
To use it, first create a MenuBuilder.php
file in src/Menu
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
In the builder, you can define items that you want in the menu.
For example, lines 20-22 add a specific Location by using the ez_urlalias
route.
Line 26 adds a defined system route that leads to the search form.
Next, register the menu builder as a service:
1 2 3 |
|
Finally, you can render the menu in pagelayout.html.twig
.
Identify it by the name that you provided in the Menu Builder's buildMenu()
method:
1 |
|