Daniel Veza

PreviousNext

Using Drupal since 2014

Layout Builder

But Layout Builder is scary!

Layout Builder modules

Layout Builder Claro
Layout Builder Lock
Layout Builder Restrictions
Layout Builder Browser

Adding fields with Layout Builder


abstract class FieldBlockBase extends BlockBase {

  abstract protected function getFieldItemList();

  abstract protected function getFormatterSettings();

  public function build(): array {
    return $this->getFieldItemList()?->view(
      $this->getFormatterSettings() + ['label' => 'hidden']
    ) ?: [
      '#cache' => [
        'tags' => [
          'node_list',
        ],
      ],
    ];
  }
}

final class AuthoredByBlock extends FieldBlockBase {

  protected function getFieldItemList(): ?FieldItemListInterface {
    $node = $this->getContextValue('node');
    return $node->get('uid');
  }

  protected function getFormatterSettings(): array {
    return [
      'type' => 'author',
    ];
  }
}

Summary

  • Layout Builder Claro
  • Layout Builder Restictions
    Layout Builder Lock
    Layout Builder Browser
  • Custom block plugins for fields.

Questions