官术网_书友最值得收藏!

  • Drupal 9 Module Development
  • Daniel Sipos Antonio De Marco
  • 747字
  • 2021-06-11 18:36:06

Layouts

As part of the Drupal 8 release cycle, the Layouts API has been introduced in order to provide contributed modules with a unified approach for defining layouts. For example, modules such as Panels and Layout Builder make use of this API to define layouts that contain regions and that can render content and all sorts of things inside.

Layouts were introduced in version 8.3 of Drupal as an experimental module (called Layout Discovery) and marked stable in version 8.4. At the same time, a new experimental module has been introduced, called Layout Builder, which uses this API to provide site builders a way to build layouts for regular content. And since Drupal 8.8, the latter has also been marked as stable.

We won't be using layouts going forward in this book but it's important you know how to work with them in case you need them. So let's quickly talk about how you, as a module developer, can define and make use of layouts programmatically.

Defining layouts

Simply put, layouts are plugins. But unlike the plugins we've seen before, these are defined in YAML files instead of annotations above a class. One of the reasons for this is that layouts are more definition than functionality, so they don't necessarily require classes. They can be simply defined in a few lines inside a YAML file.

Although not necessarily, YAML-based plugins are typically defined inside a file named module_name.plugin_type_name.yml found in the root of the module defining the plugin. So in the case of layouts, this would be module_name.layouts.yml. But what does a definition contain?

Let's imagine we want to define a two-column layout with a left and right region. Our simplest definition could look like this:

two_column:

  label: 'Two column'

  category: 'My Layouts'

  template: templates/two-column

  regions:

    left:

      label: Left region

    right:

      label: Right region

So what do we learn from this definition?

  • First, we have a name and category, which are mandatory. These can be used in whatever UI to show information about the layout.
  • Second, we specify the template that should render this layout. The corresponding theme hook gets defined under the hood. In the case above, the template file would be in the templates folder and would be called two-column.html.twig.
  • Lastly, we define the regions of the layout with a label for each. The left and right keys are important as they are the machine names of the regions.
  • As a bonus, if we wanted to attach a library, we could add another line to this definition, like so:

library: my_module/my_library

Before the layout registration is complete, we'd also need to create the template file we referenced. And it could look like this:

<p class="two-column">

  <p class="left-region">

    {{ content.left }}

  </p>

  <p class="right-region">

    {{ content.right }}

  </p>

</p>

In the template we have access to the content variable on which we can access the values of the regions we can print.

And that's pretty much it. Clearing the cache (and enabling the Layout Discovery module) would register this layout with the system.

Rendering a layout

OK, but registering a layout doesn't help us with much. Unless, of course, we use Layout Builder or some contributed module that uses layouts for various things. In which case we'd already be providing great value. But what if we want to use this layout ourselves? In other words, render stuff with it.

The simplest way of rendering something with this layout could look like this:

$layoutPluginManager = \Drupal::service('plugin.manager.core.layout');

$layout = $layoutPluginManager->createInstance('two_column');

$regions = [

  'left' => [

    '#markup' => 'my left content',

  ],

  'right' => [

    '#markup' => 'my right content',

  ],

];

return $layout->build($regions);

Without going into too much detail about the plugin system (yet), but with the above we use the Layout plugin manager to create a new instance of the layout we defined (whose machine name is two_column). Then we prepare the data to print inside the layout in the $regions array. As you can see, the array construct mirrors the regions in the layout. Finally, we build the layout by passing the regions data. And that is it. The resulting render array would render the template with the content printed in the corresponding regions.

主站蜘蛛池模板: 曲周县| 武山县| 汾西县| 桐乡市| 伊通| 营山县| 胶州市| 灵寿县| 聂拉木县| 团风县| 襄垣县| 丽江市| 澄江县| 昌邑市| 旬阳县| 青田县| 曲松县| 新昌县| 洛川县| 枞阳县| 南丰县| 南安市| 大英县| 安顺市| 古浪县| 崇州市| 临武县| 湛江市| 青川县| 江城| 蚌埠市| 二连浩特市| 山东省| 武强县| 阿鲁科尔沁旗| 乐清市| 和田县| 江永县| 清流县| 和田市| 利川市|