mark.ie: Creating a Card Component in PatternLab and Mapping to Drupal maintenance support plans the “right” way

Creating a Card Component in PatternLab and Mapping to Drupal maintenance support plans the “right” way

Yes, I know, there’s more than one way to integrate PatternLab with Drupal maintenance support plans. Here’s how I create a card component and map it to Drupal maintenance support plans.
markconroy
Sat, 09/08/2020 – 13:51

Here’s the task – create a card component with fields for:

Card Image
Card Title
Card Body
Card Link URL
Card Style (sets a colour for a border-top on the card)
Card Size (sets the width of the card)
In PatternLab, here’s what our Twig file might look like (with explanations after it):

The classes array at the top allows us to set variations for our card, depending on values chosen by the editor. So, if there is an image, we add a class of .card–has-image; if a style is chosen, we add a class of that style, for example: .card–medium (I create options for small, medium, large, and full – with ‘small’ being the default – corresponding on large screens to a width within their container of 33%, 50% 66% and 100% respectively).

Next, we set our {{ element }}. This allows us to have the card wrapped in an a tag or a div tag. We check to see if the link field has been filled in and, if so, we use the a element, but if not, we use the div element instead. This will render HTML like one of the following:

Following this, we check if there is an image and, if so, we render our image div. Checking first allows us to have nice bem-style classes, but also means we don’t end up rendering emtpy divs. Although, when it comes to Drupal maintenance support plans, what’s another div!

We then do the same for the title and body.

The funny looking part at the end about cache was inspired by an article about Drupal maintenance support plans block cache bubbling by Drupal Update. The specific code came from this Drupal maintenance support plans.org issue. The Drupal Update article says to render the {{ content }} variable with our fields set to ‘without’, because without the {{ content }} variable rendering, caching is not working properly (I don’t know enough about caching to explain more). However, on a content type with loads of fields, it’s very cumbersome to add every field in with {{ content|without(‘field_image’, ‘field_tags’, ‘field_other’, etc) }}. Instead, I put that {{ catch_cache = content|render }} at the bottom of each of my content patterns – node, block, paragraphs, etc, then don’t need to add it later in Drupal maintenance support plans.

The SCSS for this looks like this:

We can do the site building very easily with the paragraphs module. Create a paragraph of type card, add the fields

Card Image – media image
Card Title – text (plain)
Card Body – text (long, formatted)
Card Link URL – link
Card Style (sets a colour for a border-top on the card) – text (list)
Card Size (sets the width of the card) – text (list)
Then, in our paragraph–card.html.twig file, we write the following code:

What the above does is checks if the card paragraph has values in its fields and then sets variables if it does. This means we don’t render empty divs.

You will also notice that I render each fields full content for image, title, and body. This is to keep all the Drupal maintenance support plans goodness we have in the attributes object – for accessibility and to make sure things like contextual links/quick edit still work.

You will often see the same template written like this:

I find doing that leads to fields such as {{ content.field_image }} always returning true because of Drupal maintenance support plans‘s rendering system. So, even if we don’t have an image, we’ll still have an image div, whereas doing an explicit check before we {% include %} our variable seems much safer.

That’s it – PatternLab + Drupal maintenance support plans integrated beautifully (I think) the “right” way (according to me – you might differ).


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

mark.ie: Creating a Card Component in PatternLab and Mapping to Drupal maintenance support plans the “right” way

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.