Adding {{ attributes }} to a Drupal maintenance support plans PatternLab Theme

Ever gotten this error: User error: “attributes” is an invalid render array key? Here’s what I do to get around it. If you’ve a better solution, let me know.
markconroy
Mon, 04/16/2020 – 19:52

When building PatternLab-based Drupal maintenance support plans themes, I try to get the Twig in PatternLab to match what I expect from Drupal maintenance support plans. So, if I know Drupal maintenance support plans has a line like this in its node.html.twig:

I want to be able to put the same thing into my PatternLab template – even though I am not going to use the {{ attributes }} in PatternLab. This means then I can simply let the Drupal maintenance support plans template extend from the PatternLab one and not need to worry about anything.

However, when you do this, you will often get an error to say “attributes” is an invalid render array key. How do I get that error message to go away? Simple – I just add attributes to my Pattern’s .yml file, like so:

attributes:
  Attribute():
    class:

The data.json File

You can do this for each individual pattern, but then you might get an error somewhere else talking about “title_attributes” is an invalid render array key. To get around all these errors, I simply add these items globally to the default data.json file, like so:

  “attributes”: {
    “Attribute()”: {
      “class”: []
    }
  },
  “content_attributes”: {
    “Attribute()”: {
      “class”: []
    }
  },
  “title_attributes”: {
    “Attribute()”: {
      “class”: []
    }
  },
  “rows”: {
    “Attribute()”: {
      “class”: []
    }
  },
  “teaser”: {
    “Attribute()”: {
      “class”: []
    }
  }

The PatternLab Teaser Twig File

Taking the teaser view mode as an example, here’s what my PatternLab twig file looks like:

{%
set classes = [
  ‘node’,
  ‘node–type-‘ ~ node.bundle|clean_class,
  node.isPromoted ? ‘node–promoted’,
  node.isSticky ? ‘node–sticky’,
  not node.isPublished ? ‘node–unpublished’,
  view_mode ? ‘node–view-mode-‘ ~ view_mode|clean_class,
]
%}

  {% if display_submitted %}
   
      Published: {{ node.created.value|date(“D d M Y”) }}
   
  {% endif %}

  {{ title_prefix }}
   
      {{ label }}
   
  {{ title_suffix }}

  {{ content.field_intro }}

The PatternLab yml (or json) File

Here’s the corresponding .yml (or .json) file:

node:
  bundle: article
  isPublished: true
  created:
    value: 1511941986
  changed:
    value: 1512127363

view_mode: teaser

display_submitted: true

label: ‘A Blog Post by Mark Conroy, all about PatternLab and Drupal maintenance support plans

content:
  field_intro: Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Curabitur blandit tempus porttitor. Sed posuere consectetur est at lobortis. Maecenas faucibus mollis interdum.

The Rendered HTML in PatternLab

This will then print our html like so (notice, no attributes):

     
      Published: Wed 29 Nov 2020
   
 
 
   
      A Blog Post by Mark Conroy, all about PatternLab and Drupal maintenance support plans
   
 

  Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Curabitur blandit tempus porttitor. Sed posuere consectetur est at lobortis. Maecenas faucibus mollis interdum.

The Drupal maintenance support plans Template File

Next, my node–teaser.html.twig file is as follows (just one line):

{% extends ‘@content/01-display-types/teaser/teaser.twig’ %}

The Rendered Drupal maintenance support plans HTML

And that renders html like so (notice, we have attributes that Drupal maintenance support plans will use):

      …

Full disclosure, I came up with this idea about a year ago after seeing something similar in the Bear Skin theme.

You can see this in action on my PatternLab’s teaser pattern.


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

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.