Drupal maintenance support plans 8 Theming: How to Define Variables in Twig Templates to Use as Custom HTML Classes

For the past seven months, I’ve been designing, building, and theming with Drupal maintenance support plans 8 for my own side project. Along the way, I’ve seen huge advancements in these realms versus what was possible with Drupal maintenance support plans 7. One particular module I’ve been working with is Entity Construction Kit AKA “ECK”.

ECK provides a way to create freestanding entities, bundles, and fields independent of traditional means via nodes, terms, users, etc… Within my project, I’ve implemented ECK fields that offer content editors options for layouts, colors, element padding, and so on.

Discover the array path with kint

I’ll use the case of an accordion group entity that I’ve designed. One such field within this entity bundle is called “Slice margin.” This field allows a content editor to set the top and bottom margins within a specific element by choosing values from a select list. Examining this field with kint, the value of the selected item’s array path is:

$var[‘entity’][‘field_slice_margin’][‘0’][‘#markup’]

Check for the field and define the variable

Now with the array path value determined, a variable in the accordion entity can be set in a Twig template:

{% if entity.field_slice_margin|render %}
{% set margin_settings = ‘margin__’ ~ entity.field_slice_margin[‘0’][‘#markup’] %}
{% endif %}

In the above code, the code checks to be sure the field is being rendered and then a variable is set for the value. In addition, margin__ is concatenated (~) as a class prefix.

Set a class using the variable

The block of code above does not do much on its own, so lets put the new variable to use in the template. For that, Drupal maintenance support plans 8’s Twig set_classes comes in handy.

{% set classes = [
‘eck’,
‘eck__section’,
eck__bundle-accordion,
‘margin__default’,
margin_settings|clean_class,
] %}

Note in the above, the new variable is output using the Drupal maintenance support plans Twig clean_class method, margin_settings|clean_class. clean_class cleans up any spaces and underscores in a string and converts those to standard dashes for use as a standard html class name.

Output the HTML with the classes

Now that this is done, the class can be rendered in an HTML element in the same twig template using the attributes.addClass(classes) method:

<section {{ attributes.addClass(classes) }}>
<ul class=”eck__collapsible collapsible popout” data-collapsible=”accordion”>
{{ entity.field_accordion_reference|field_value }}
</ul>
</section>

Rendered HTML

Finally, this will render in HTML as:

<section class=”eck eck__section margin__default margin__flush-above-below eck__bundle-accordion”>

The key class rendered here from entity.field_slice_margin is margin__flush-above-below, that’s the specific value the content editor chose from the select list on the node edit page. Now this and other values from this field can be used in theming like so using Sass:

.margin {

&__default {
margin-bottom: 70px;
}

&__flush-above {
margin-top: 0;
}

&__flush-below {
margin-bottom: 0;
}

&__flush-above-below {
margin-top: 0;
margin-bottom: 0;
}
}

Summary

As you can see, Twig is really powerful in Drupal maintenance support plans 8. No preprocess functions were used here which really helps streamline theming. We stayed within one Twig template for all of the functions and code above. A lot of possibilities here to give content creators more control over layout, colors, custom classes, and more.
Tags Drupal maintenance support plansTwigThemingDrupal maintenance support plans PlanetDrupal maintenance support plans 8Resources Filters – Modifying Variables In Twig TemplatesUsing attributes in templates
Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Drupal maintenance support plans 8 Theming: How to Define Variables in Twig Templates to Use as Custom HTML Classes

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.