Add menu class in Drupal8 - drupal-8

I installed Link attribute module to add classes in menu items but its not showing attributes for already added menus i.e. the menus which are coming by default in Drupal8 menus.
How to add class in Drupal default menus i.e My account, Login and Logout menus.

You could copy the template for the menus into your theme and add classes that way?
You can find the nav templates here core/themes/classy/templates/navigation the one for the logged in user is links.html.twig. If you enable twig debug you will be able to see which templates are being used there's a guide on how to do that here. Then all you have to do is copy that template to your theme and modify it to how you want it. Example below:-
{% if links -%}
{%- if heading -%}
{%- if heading.level -%}
<{{ heading.level }}{{ heading.attributes }}>{{ heading.text }}</{{ heading.level }}>
{%- else -%}
<h2{{ heading.attributes }}>{{ heading.text }}</h2>
{%- endif -%}
{%- endif -%}
<ul{{ attributes.addClass('my-class') }}>
{%- for item in links -%}
<li{{ item.attributes }}>
{%- if item.link -%}
{{ item.link }}
{%- elseif item.text_attributes -%}
<span{{ item.text_attributes }}>{{ item.text }}</span>
{%- else -%}
{{ item.text }}
{%- endif -%}
</li>
{%- endfor -%}
</ul>
{%- endif %}

Related

Shopify Liquid If Statement for Shop Name

I have my brand name or shop name added to all my URL's via the following statement which is found in the theme.liquid file. I want to exclude all pages that are blogs or articles. This would mean no shop name on those URL's, which is this pipe and code.
| {{ shop.name }}
Original Code
{%- capture seo_title -%}
{%- if template == 'search' and search.performed == true -%}
{{ 'general.search.heading' | t: count: search.results_count }}: {{ 'general.search.results_with_count' | t: terms: search.terms, count: search.results_count }}
{%- else -%}
{{ page_title }}
{%- endif -%}
{%- if current_tags -%}
{%- assign meta_tags = current_tags | join: ', ' -%} – {{ 'general.meta.tags' | t: tags: meta_tags -}}
{%- endif -%}
{%- if current_page != 1 -%}
– {{ 'general.meta.page' | t: page: current_page }}
{%- endif -%}
{%- assign escaped_page_title = page_title | escape -%}
{%- unless escaped_page_title contains shop.name -%}
| {{ shop.name }}
{%- endunless -%}
{%- endcapture -%}
<title>{{ seo_title | strip }}</title>
I have been trying to place another if statement around the following but I have had no luck yet.
{%- assign escaped_page_title = page_title | escape -%}
{%- unless escaped_page_title contains shop.name -%}
| {{ shop.name }}
{%- endunless -%}
This is the code that I have tried, is there a better way to do this as I can't quite get it to work.
Attempted Code
{%- capture seo_title -%}
{%- if template == 'search' and search.performed == true -%}
{{ 'general.search.heading' | t: count: search.results_count }}: {{ 'general.search.results_with_count' | t: terms: search.terms, count: search.results_count }}
{%- else -%}
{{ page_title }}
{%- endif -%}
{%- if current_tags -%}
{%- assign meta_tags = current_tags | join: ', ' -%} – {{ 'general.meta.tags' | t: tags: meta_tags -}}
{%- endif -%}
{%- if current_page != 1 -%}
– {{ 'general.meta.page' | t: page: current_page }}
{%- endif -%}
{%- if (template == "blog" or template == "article") and current_tags contains '_NOINDEX' -%}
{%- assign escaped_page_title = page_title | escape -%}
{%- unless escaped_page_title contains shop.name -%}
{%- endunless -%}
{%- else -%}
{%- assign escaped_page_title = page_title | escape -%}
{%- unless escaped_page_title contains shop.name -%}
| {{ shop.name }}
{%- endunless -%}
{%- endif -%}
{%- endcapture -%}
<title>{{ seo_title | strip }}</title>
You can just use the following:
{%- unless escaped_page_title contains shop.name or template == 'blog' or template == 'article' -%}
| {{ shop.name }}
{%- endunless -%}
You were close, but you cannot use parentheses to group Liquid condition operators.

How to display text if the field is empty?

How to display a text if the field is empty ?
I tried the following code but it does not work :
{% if content.field_description is not empty %}
{{ content.field_description }}
{% else %}
test
{% endif %}
If you have Twig Tweak installed, you can do the following:
{% if content['field_description'] | field_value != '' %}
{{ content['field_description'].value | striptags }}
{% else %}
<p class="des-empty">test</p>
{% endif %}
When field is empty it is not coming with content variable
so you can simply check by isset
{% if content.field_description %}
{{ content.field_description }}
{% else %}
<p class="des-empty">test</p>
{% endif %}
The below worked for me:
{% if content['field_description'] IS NOT EMPTY %}
{{ content['field_description'].value | striptags }}
{% else %}
<p class="des-empty">test</p>
{% endif %}
Please note that it may vary based on the content type and fields you are dealing with.

List of list in Jinja2

I want to loop over lists using a list containing the lists name, eg :
{% set album = ['one','two'] %}
{% set one = ['a','b','c'] %}
{% set two = ['d','e','f'] %}
{% for alb in album %}
{% for songs in alb %}
{{ songs }}
{%- endfor %}
{%- endfor %}
but it doesn't work...how can I achieve something like that ? (I want to split the lists because they are long)
Thank's
Create album from previously set lists one and two and it will work
{% set one = ['a','b','c'] %}
{% set two = ['d','e','f'] %}
{% set album = [one, two] %}
{% for alb in album %}
{% for songs in alb %}
{{ songs }}
{%- endfor %}
{%- endfor %}

Sum a value inside a loop in jinja

I have a for loop in a jinja template:
<ul>
{%- for t in tree recursive %}
<li><span class="li_wrap">
<span><i class="glyphicon {{ 'glyphicon-folder-close' if t.type == 'folder' else 'glyphicon-file'}}"></i> {{ t.name }}</span>
{% if t.type != 'folder' %}
<span class="pull-right">Size: {{ t.size/1000 }} kb </span>
{% endif %}
</span>
{%- if t.children -%}
<ul>{{ loop(t.children) }}</ul>
{%- endif %}
</li>
{%- endfor %}
</ul>
what I want is to sum the value of t.size and use it as a total size number, but above this block.
What is the best way to do that?
You must use the jinja function SUM => http://jinja.pocoo.org/docs/
This code should work
Total: {{ t|sum(attribute='size') }}

Navigation Nodes in django-cms example

Can you give me an example how can i use Navigation Nodes?
Cannot find examples in documentation.
There is this {{ node }} but where is it coming from?
Particalarly i am intereisted in {{ node.is_leaf_node }}.
Each navigation node is simply a link/entry in your menu tree so they are generated from your page layout, for example:
- Home
- About
- Projects
- Project A
- Project B
- Contact
creates a menu with each page representing a node in the menu tree.
There's an example of them working in the default menu.html template (where child is a node in the menu):
{% load menu_tags %}
{% for child in children %}
<li class="{% if child.selected %}selected{% endif %}{% if child.ancestor %}ancestor{% endif %}{% if child.sibling %}sibling{% endif %}{% if child.descendant %}descendant{% endif %}">
{{ child.get_menu_title }}
{% if child.children %}
<ul>
{% show_menu from_level to_level extra_inactive extra_active template "" "" child %}
</ul>
{% endif %}
</li>
{% endfor %}