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

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!

Related

Theming Drupal 8 - Loop through all posted articles and print specific section

I am trying to create my first Drupal 8 theme. On the frontpage, I want to list all articles with the title, summary, date posted, etc.
I more or less achieved this using views. Basically {{ page.content }} in twig, but found it to be really inflexible and I didn't really get the results that I wanted. What I want to do is just to iterate through all the articles and print each section of that article "manually". For instance:
{% for page in pages %}
{{ page.content.title }}
{{ page.content.datePosted }}
{{ page.content.body }}
{% endfor %}
So that I can have more control of what is happening and not making a lot of configuration in the views module when deploying. What is the best soltuion to achieve this? Thanks!
I recommend to use Views. Configure you view (filter by content type, etc.), you have a lot of parameters to achieve what you need.
Set the view to show "Content" and give it a view mode (you can use the default teaser, full, or create your own view mode). Then you can create a custom template for this :
node--xxxx--teaser.html.twig
In order to know how to name your custom template file, enable Twig Debugging in sites/default/services.yml. Set the debug variable to true. And clear cache. Then, you will see in source code the template name suggestion like this :
<!-- FILE NAME SUGGESTIONS:
* node--1--full.html.twig
* node--1.html.twig
x node--oeuvre--full.html.twig
* node--oeuvre.html.twig
* node--full.html.twig
* node.html.twig
-->
In your twig, you can do that kind of templating :
{{ content.my_field_image[0] }}
<h2>{{ node.title.value }}</h2>
<p>{{ content.body }}</p>

Using alternative (translated) titles in dynamic menus in Lektor

I'm new to Lektor and I've been using this snippet in my layout to dynamically generate menus
{% for content in site.get('/').children %}
<li>{{ content.title }}</li>
{% endfor %}
It works fine to produce the alt url accordingly to the alternative (language) selected, but the title is still shown only in the primary language of the content instead of the translated one.
That means that when a say french alt is selected (and present in the url), menus are still i.e. Main, Contact instead of the french translated version.
I know I can work around this using a databag to hold a language mapping for the menus, but that would require to duplicate the title information in the databag and then make sure it stays in sync with whatever is in content.title.
Ideally what I'm missing is a filter for title where you can specify the alt you want to use, or some other method that I'm not aware of?
Naive as it may be, I didn't realize that you can pass the alt parameter in site.get until after I looked at the source code. So the way to get the defined child pages for a certain language is:
{% for content in site.get('/', alt=alt).children %}
<li>{{ content.title }}</li>
{% endfor %}
And since we already specified the alt parameter in the get, we don't need to filter the url anymore for the current alt.

CollectionType custom prototype via form_theme

I want to customize the way an entry of a CollectionType in a form will be rendered, that is the way the prototype is generated, I need my own HTML. So I need to do it with the Twig form_theme, but it doesn't work.
I am currently using Symfony 4.1.2.
Here is what I have for now:
In the template that renders the whole form:
{% form_theme form.additionalEmails.vars.prototype 'form/additionalEmail.html.twig' %}
In the form/additionalEmail.html.twig file:
{% block form_row %}
<div style="margin: 10px 0">
{{ form_label(form) }}
{{ form_widget(form.children.value) }}
<div>Recevoir les mails de type : {{ form_row(form.children.isUsedForAdminCommChannel) }}{{ form_row(form.children.isUsedForInfosPratiquesCommChannel) }}{{ form_row(form.children.isUsedForAgendaCommChannel) }}</div>
</div>
{% endblock %}
If I do that I get this error:
Key "value" does not exist as the array is empty.
What is weird is that if I replace the above code by this (by commenting the old one and adding the new line):
{{ dump(form.children) }}
I see that form.children is an array with 4 items and one named "value" so it is clearly broken.
Your help would be much appreciated, as I'm just an amateur dev trying to make a website for a youth movement ^^
Best Regards,
[EDIT]
Here is the form that includes the CollectionType:
https://gist.github.com/MrPOC/a4af94cc5e577a1244dac59de5ea3506
The field is named "additionalEmails"
Just so I understand you correctly. You have a Form which contains a CollectionType and you want to change the prototype of that CollectionType?
If yes could you please post the Form with the CollectionType?
EDIT: Ok so here's how to overwrite the prototype template for a CollectionType.
First we have to find out what your forms unique_block_prefix is. You can find this by going to the page where this form is rendered, you then click in the symfony debug bar on the form icon. Then you have to expand the View variables block and look for unique_block_prefix.
I assume your block prefix could look like this _user_type the name of the block you would overwrite would then be _user_type_additionalEmails_entry_widget (for more details please read the link provided below)
You can replace the widget part with row or label depending on what you want to overwrite (I would suggest trying to overwrite them all to see what exactly changes because I don't know of the top of my head)
In twig you would then take your block prefix and overwrite the prototype field in the same way you would overwrite any other field
{% block '_user_type_additionalEmails_entry_widget' %}
//Content
{% endblock %}
What I wrote down here is what I found out after reading https://github.com/symfony/symfony-docs/issues/6056 (read HeahDude' comment specifically)
I hope this helps!

Creating 'News' section with cmsplugin_blog Django CMS application

How do I create a generic 'News' section, that would use cmsplugin_blog and be displayed on all pages. Is it possible to tell Django CMS in base.html to include the app on all pages? The content of each news entry should be displayed in the main block of the page.
My base.html looks something like this:
...
{% block base_content %}
{% endblock %}
...
{% block right-column %}
{% endblock %}
Each page that should have a news short list in the right column with links to individual news entries that should be displayed in the base content block when clicked on the link.
One way you could do this would be to create a page which is published but not in the navigation and add an instance of the blog plugin to that page and then in your "master" template you can display the contents of that placeholder (which will be the blog plugin instance) using the following template tag:
http://django-cms.readthedocs.org/en/latest/advanced/templatetags.html#show-placeholder

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