Print second level menu in Drupal 8 - drupal

Drupal 8 markup changed quite a lot from Drupal 7 using the Twig Engine. We are developing a site with it. We want to print the second level menu links in there.
{% if main_menu %}
<nav id ="main-menu" class="navigation" role="navigation">
{{ main_menu }}
</nav> <!-- /#main-menu -->
This is how we print the menu in Drupal 8. A syntax im not quite used to.
How can i print. Second level links on the menu. Or the Menu Tree?

According to template_preprocess_page(), you should have access to secondary_menu as well, so:
{% if secondary_menu %}
<nav id ="secondary-menu" class="navigation" role="navigation">
{{ secondary_menu }}
</nav>
{% endif %}
Generally speaking, you can use a preprocess hook to add variables. In this case that would be something like:
function MYTHEME_preprocess_page(&$vars) {
$vars['foo'] = 'bar'; // foo is available in the page template file
}

Depending on which theme you use, it may be that it is not a problem with the menu template. In my case, the (main) menu was configured to show only one level in the block settings.
I found out by chance, here is how to change it:
Go to https://<yourdrupalpage>/admin/structure/block
on the respective menu block item (e.g. main menu) click configure
adjust the number of visible menu levels to your needs
that should to the trick

I did what Stephan Richter suggest but I forgot to check "Always display open" equivalent option in the menu item level1
So If you want your level 2 to be displayed, do not forget this option in the parent.

Related

django cms menu: display page title alongside the page's menu title

I want to display a plain django-cms menu. I override the default menu/menu.html template, as I want to display the page's title, alongside the page's menu title. This is for a content navigation, where the additional info of the title is useful.
The default is (in the <a></a>):
{{ child.get_menu_title }}.
What I want is
{{ child.get_menu_title }}<span>{{ child.the_page_title }}</span>
But, somehow, I cant display the title alongside the menu_title. If the field menu_title is set, it overrides the title attribute of the NavigationNode, and it is returned when calling get_menu_title (obviously). Also, the title is not in the attr (NavigationNode attr).
I just ended using
{% load cms_tags %} {% page_attribute 'title' child.id %}
This might not be ideal concerning performance, but works very well. Open but for better solutions!

What would be the best way to create my menu in Symfony 2

I'm working on the admin of my portfolio and I'm having a dilemma about my menu. The admin side of my portfolio allows me to change some general info, add, modify or delete a project. Therefore, all pages of my admin have the same menu. The menu have the following buttons: a button to change the general info and a button to add a new project. if there are projects in the database, the menu have more things, a select with the names of my projects and the two buttons to modify or delete the project.
At first, I made a form for everything about the project and an other just for the change info button, but after asking some question, I got often told that I shoudn't make any form for that and that just making link should be fine.
I like that idea, but, if I do that, how can I pass the selected project when I click on modify for exemple?
<a href="{{ path("modify_project") }}">
Here is my current code:
<ul>
<li>
Ajouter un projet
</li>
</ul>
{% if projets | length > 0%}
<select>
{% for id,projet in projets %}
<option value={{ id }}>{{ projet }}</option>
{% endfor %}
</select>
<ul>
<li>
Modifier
</li>
<li>
Supprimer
</li>
</ul>
{% endif %}
If someone have a better idea, I will be very interested to ear it.
Have you had a look at this: https://github.com/KnpLabs/KnpMenuBundle?
Sorry, your are right, the answer is actually this one:
{{ entity.id }}
Twig will understand that, nothing more to do.
After really thinking about it, The best way would be a form, since you can't change the href of the link, except with javascript and I don't want to do that.

Silex - How to process template includes separately

I've got a Twig base template like so:
{% include "./partials/navigation.html" %}
<!-- Main Wrapper -->
<div id="wrapper">
{% block content %}{% endblock content %}
</div>
I also have a route controller which is outputting the response content to the page using twig:
return template->render('path/to/teplate', args());
where args[] array is all the data needed for this bit: (different on every page)
{% block content %}{% endblock content %}
However my sidebar is being built separately through a menu builder class, and now it needs to render the results of building the menu to my template and populating ./partials/navigation.html.
An easy solution for me is to just append the results of the Menu Builder to the returned response of every controller (I can use a base controller class to do this as this menu appears on every page). However something about this feels unclean as if I have to render the same header/footer every time as well I'll have to be append all 3 outputs to the response. (maybe that is okay?)
Is there a better way of rendering several includes worth of content which each need their own DB lookups and other server-side logic?
Yes. They are called sub requests. Read the documentation here.

Add a dynamic menu to a site?

I want to add a dynamic menu to the frontoffice of my site where can i easely add a child menu or a new panel menu from the page admin .
how can i do that using symfony
You should code that menu, it's a good training if you're new to symfony.
For a dynamic menu, I usually do :
a Menu entity
a MenuItem entity
a controller for each, with a CRUD.
an action in the MenuController do show the menu (as a HTML unordered list for example).
Then you can edit the ::base.html.twig in app/Resources/view and you add a block menu and render the MenuController :
{% block menu %}
{{ render(controller('AcmeBlogBundle:Menu:list')) }}
{% endblock %}
With knpmenubundle you can easily create a Menu with child.
First of all, look at https://github.com/KnpLabs/KnpMenuBundle and read http://symfony.com/doc/master/bundles/KnpMenuBundle/index.html
Secondly use Doctrine and make a relation OneToMany (or ManyToMany but it's not recommended).
Use KnpMenuBundle as Service
Good luck !

How to show Next & Previous page links in Django-CMS

We've a site built in Django-CMS and have developed a mobile version with alternative CSS to suit the smaller viewing area. As well as the usual navigation bar we want to include Next and Previous page links at the bottom of each page.
I know how to output the current page's siblings using this code:
{% show_menu current_page.level %}
What is the easiest way to output links to the next and previous page?
You can use {{ request.current_page.get_next_sibling }} and {{ request.current_page.get_previous_sibling }} in your templates to show the 'neighbor' pages (not that either or b
You can use the methods get_next_filtered_sibling and get_previous_filtered_sibling - but probably only for newer versions of the django cms.
Here are two template tags that return page objects which you might want to feed into the {% page_url ... %} template tag.
#register.assignment_tag(takes_context=True)
def get_next_page(context):
current_page = context['request'].current_page
return current_page.get_next_filtered_sibling(
publisher_is_draft=False
)
#register.assignment_tag(takes_context=True)
def get_prev_page(context):
current_page = context['request'].current_page
return current_page.get_previous_filtered_sibling(
publisher_is_draft=False
)

Resources