PreviousNext: Creating a cards section with Layout Builder

In this post, we explore building a cards section with Layout Builder.

 

by lee.rowlands / 9 May 2024

The component will look something like the 'Services' section on our homepage*. If you've built websites over the last decade, you've probably built a component like this many times.

Image removed.

The key aspects of the component are

  • A title
  • Some intro text
  • A series of cards. Each card has a URL, title, image and teaser text

If you've previously used paragraphs for modelling landing pages, you may immediately be thinking that your content model will be made up of paragraph type 'cards' with three fields as follows:

  • A title
  • A teaser field for the intro text
  • A multi-value cards field, which is itself another paragraph type 'card'.

Or rather, because this is Layout Builder, you might be thinking of a block-content type called cards with those fields on it — leaning on paragraphs for the multi-value cards field.

Whilst that approach works well for paragraphs, it isn't the best approach for Layout Builder:

  • There are some gnarly bugs with Layout Builder + Block Content + Paragraphs + Content moderation
  • It requires content-editors to fumble in the off-canvas editor to rearrange each card using drag-and-drop. We want a nice re-order experience like this:
Image removed.

Making the most of layout plugins

Let's instead pivot to a layout plugin for the cards component and think of it in terms of regions.

We have:

  • A layout title
  • An introduction region
  • A cards region

We probably already have a block-content type that consists of a WYSIWYG field — e.g., something like the Basic block content type in core. We can use that for the intro text.

So we need a card block content-type. But we probably want two. A lot of our cards will just point to other pages on the site — and it makes sense for the card to be built from fields on that page. If we put a teaser image and teaser text field on all our node-types, we can make use of them when creating a card for that page. We can also use these for meta-tags like the OpenGraph image. And all our node-types already have a title and URL. The second block-type is if we need to link to pages outside the site. So, the content models for those two block types are as follows:

  • For the internal card block-type, we need an entity-reference field to allow the content-editor to select the content to generate the card for
  • For the external card block-type, we need all the fields — title, image, teaser, URL

Defining layout plugins

Next, we need to define our layout plugin. We start with a layouts.yml file in our theme or module.

cards:   label: Card grid   category: Layouts   template: layouts/cards   icon_map:     - [intro, intro, intro]     - [c1, c2, c3]     - [c4, c5, c6]     - [c7, c8, c9]     - [c10, c11, c12]   regions:     content:       label: Cards     intro:       label: Introduction   library: 'your_theme/card'

Note: the icon_map isn't needed here, but it gives us a nice icon in the 'Add section' form.

With those pieces in place, we can use Layout builder restrictions to ensure only the right block-types can be placed in each region. The introduction region can be limited to the basic WYSIWYG block-type. The cards region can be limited to the internal and external card block-type.

This layout will use the default layout plugin, but we want a custom layout plugin with a title field in the configuration form. You can read more about creating a custom layout plugin from our previous post about creating a dynamic layout with flexible regions. This one will be much simpler. We just need a title field in the configuration form and a preprocess hook to expose that to our template. 

The first step of this is to add a 'class' entry to our layout definition.

cards: // ...   class: \Drupal\your_theme\Layouts\Cards   // ...

Then, we need to create that class. 

<?php namespace Drupal\your_theme\Layouts; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Layout\LayoutDefault; /** * Defines a class for a layout that has a title option. */ class LayoutWithTitle extends LayoutDefault { /** * {@inheritdoc} */ public function defaultConfiguration() { return parent::defaultConfiguration() + ['title' => '']; } /** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state): array { $build = parent::buildConfigurationForm($form, $form_state); $build['title'] = [ '#weight' => -10, '#type' => 'textfield', '#default_value' => $this->configuration['title'], '#title' => $this->t('Title'), '#description' => $this->t('Provide an optional title for this section'), ]; return $build; } /** * {@inheritdoc} */ public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void { parent::submitConfigurationForm($form, $form_state); $this->configuration['title'] = $form_state->getValue('title'); } }

Then, in your theme you can preprocess to make this variable available to the template.

/** * Implements hook_preprocess_HOOK(). */ function your_theme_preprocess_layout(array &$variables): void { // Add the title from LayoutWithTitle. $variables['title'] = $variables['content']['#settings']['title'] ?? NULL; }

For optimum UX, we'd probably want to make Layout Builder Browser blocks, too. You can read more in our previous post about our approach to Layout Builder UX.

All that remains then is to theme the block-content types to match the design. For more on that, see our previous post on theming block-content types with Layout Builder.

As an extra enhancement, you could add integration with Layout Section Classes module to give content editors additional options like the number of cards shown across the page in the card grid.

Note: this component on our website is automated and built with Views, but there is often a need to build out curated equivalents.

Tagged

Layout Builder

The Drop Times: Drupal Iberia 2024 Unites Portuguese and Spanish Communities

Drupal beria 2024 brings together Spanish and Portuguese Drupal communities for the first time. Set in Évora on May 10th, this event, organized by Associação Drupal Portugal and Asociación Española de Drupal, aims to foster collaboration and unity. With a biennial format, it promises engaging sessions and keynotes, supporting both seasoned professionals and newcomers.

Tag1 Consulting: Migrating Your Data from Drupal 7 to Drupal 10: Assumptions of the API

As pointed out in the first article, the Migrate API scope is to move over content and configuration. Porting modules and themes are out of scope. With that in mind, let’s take a closer look at some of the assumptions and limitations. In addition to what will be presented in today’s article, I highly recommend reviewing the official documentation for upgrading from Drupal 7. Drupal is ever-evolving and the Migrate API documentation gets updated regularly. Of particular importance, review the page on known issues when upgrading from Drupal 7, as things get added and removed from that list as issues are discovered and fixed.

mauricio Wed, 05/08/2024 - 17:13

The Drop Times: Which CMS Powers Your Country's Official Website? A Global CMS Usage Analysis

A growing trend is seen in governments favouring free and open-source solutions (FOSS) for their official websites. The potential benefits of FOSS include enhanced security, flexibility, and cost savings. But how widespread is this trend, and which specific systems are gaining traction? To understand the scope of this trend, we examined the official websites of 195 countries globally. This analysis digs deeper into the trend of governments favouring free and open-source solutions for their online presence, uncovering which CMS systems—including Drupal, WordPress, and others—are most preferred for official government communication.

Promet Source: DrupalCon 2024: Sessions to Watch for Government

Note: This blog was first published on May 24, 2023, and has been updated to reflect new information and insights. Takeaway: DrupalCon Portland 2024 offers valuable sessions, summits, and exhibitors useful to State and local government agencies. As an attendee, you'll have the opportunity to learn from industry experts, discover powerful tools and solutions, and network with peers facing similar challenges in the government space.

Dries Buytaert: State of Drupal presentation (May 2024)

This week, approximately 1,400 Drupal enthusiasts came together for DrupalCon North America in Portland, Oregon. As a matter of tradition, I delivered my State of Drupal keynote, often referred to as "DriesNote". In case you missed it, you can watch the video or download my slides (385 MB).

This year's keynote was inspired by President John F. Kennedy's famous "Moonshot" speech. After being global leaders, the U.S. had fallen behind in the Space Race. Challenged by the Soviet cosmonaut program, President Kennedy rallied Americans around the ambitious goal of landing on the moon before the decade was out.

Drupal Starshot, a new version of Drupal

Drupal has always been known for its low-code capabilities. However, many competitors now offer similar features, and in some areas, they even surpass what Drupal provides. While Drupal is celebrated for its robustness, it can be challenging for newcomers, especially those with limited technical expertise. So in my keynote, I was excited to introduce Drupal Starshot, our "Moonshot" to make Drupal more accessible and easier to use.

Twenty-three years after Drupal's inception, we are preparing to launch a second official version of Drupal. For the time being, we're calling this second version "Drupal CMS". It will be built on top of Drupal Core and common contributed modules, and available as a separate download alongside Drupal Core. Image removed.Wireframe of the Drupal.org download page featuring two options: Drupal CMS (Drupal Starshot) and Drupal Core, with Drupal CMS being promoted as the preferred starting point for most.

Drupal Starshot will be designed to have a great out-of-the-box experience. It will enable Ambitious Site Builders without Drupal experience to easily create a new Drupal site and extend it with pre-packaged recipes, all using their browser.

The vision for Drupal Starshot is the outcome of highly productive brainstorming sessions with Drupal Core Committers, the Drupal Association, colleagues at Acquia, various Drupal agencies, and others.

From an implementation standpoint, it will primarily rely on the Project Browser and Recipes initiatives, while also incorporating the Experience Builder initiative. We actually started prototyping Drupal Starshot a few weeks ago and showcased our progress at DrupalCon. Our goal is to launch a first version of Drupal Starshot within 8 months.

At DrupalCon, hundreds of people pledged to get involved, and we had two "super BoFs" with over 50 people each. If you're interested in getting involved with Drupal Starshot, you can register your interest at https://drupal.org/starshot. Additionally, join the conversation in the #Starshot channel on Drupal Slack.

Image removed.

Drupal's brand refresh and marketing strategy

Alongside our technical efforts, we've launched a bold marketing strategy. At DrupalCon Lille, I discussed the need for a fresh marketing approach. Since then, we've made tremendous progress.

I introduced a Drupal brand refresh, which includes updated brand guidelines to ensure a consistent and modern visual identity. This refresh aims to invigorate our brand – making it more vibrant and appealing to newcomers, while still honoring our history and representing our diverse, global community.

I couldn't cover all the details around the brand refresh in my keynote, so I'm expanding on them in this blog post. For a detailed explanation of Drupal's refreshed brand, check out this video by Shawn Perritt: Drupal 2024 brand refresh explained.

Our efforts went beyond just a brand refresh. The Marketing Committee has also guided the community in developing a comprehensive marketing toolkit, which includes messaging guides, pitch decks, and more. All these resources will be available at https://www.drupal.org/marketing.

Let's reach for the stars

Image removed.

As we advance with Drupal Starshot, I recall President Kennedy's famous words:

We choose to go to the moon in this decade and do the other things, not because they are easy, but because they are hard, because that goal will serve to organize and measure the best of our energies and skills, because that challenge is one that we are willing to accept, one we are unwilling to postpone, and one which we intend to win.

Embracing the Drupal Starshot initiative reflects a similar ethos; we're not choosing the easy path, but the one that tests our strength, creativity, and ability to do hard things. The success of Drupal Starshot will be a testament to the incredible collective power of the Drupal community. It's a challenge we are also unwilling to postpone, and intend to win.

Drupal Starshot is more than a technological leap; it represents a shift in how we think, innovate, and collaborate. It's about reaching for the stars and making the Open Web accessible to everyone.

Thank you for being part of this journey. I can't wait to see where it takes us together. The feedback from the last two days at DrupalCon has been overwhelmingly positive. I am more committed than ever and excited to pledge a significant amount of my time to this mission, and to the future of Drupal.