Including layout description in Layout Builder ‘configure section’ for Drupal 9
Drupal 9 allows us to create custom layout ‘classes’ so that in Layout Builder, the ‘add/configure section’ can have custom options.
This tutorial covers how add the description
key in the layout definition in layouts.yml
to the custom options.
Here is an article from CodimTh on how to create those custom options.
Step 1: Add description
to the layout definition
Layouts are defined in my_theme.layouts.yml
One key is description
. I’m using this space to provide instructions to the content editor on how to use this kind of section.
isu_card_set:
label: Card Set
category: IASTATE22
class: '\Drupal\iastate22_theme_companion\Plugin\Layout\Iastate22Layout'
description: 'This section creates a container for a Card Set. Place 2 to 6 Card (default) custom blocks here, one per region.'
type: partial
template: templates/layouts/isu-card-set/isu-card-set
icon: templates/layouts/isu-card-set/isu-card-set.png
regions:
col_1:
label: Column One
col_2:
label: Column Two
col_3:
label: Column Three
Step 2: Add description to layout custom options
Inside public function buildConfigurationForm()
create a variable to get the definition
key.
definition = $this->getPluginDefinition();
This will give us access to:
- the label:
$definition->getLabel()
- the description:
$definition->getDescription()
You can then use these in a form element.
$form['feature_set_info'] = [
'#type' => 'item',
'#markup' => '<div class="form-item"><div class="isu-layout-form-title">' . $definition->getLabel() . '</div><p class="description isu-form-element-description">' . $definition->getDescription() . '</p></div></div>',
];
$form['feature_set_info'] = [
'#type' => 'item',
'#markup' => '<div class="form-item"><div class="isu-layout-form-title">' . $definition->getLabel() . '</div><p class="description isu-form-element-description">' . $definition->getDescription() . '</p></div></div>',
];
I like this because it lets me easily use the same layout class for my whole suite of custom layouts, and provide unique instructions for each one.