You are here

protected function LayoutBuilder::buildAdministrativeSection in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/layout_builder/src/Element/LayoutBuilder.php \Drupal\layout_builder\Element\LayoutBuilder::buildAdministrativeSection()

Builds the render array for the layout section while editing.

Parameters

\Drupal\layout_builder\SectionStorageInterface $section_storage: The section storage.

int $delta: The delta of the section.

Return value

array The render array for a given section.

1 call to LayoutBuilder::buildAdministrativeSection()
LayoutBuilder::layout in core/modules/layout_builder/src/Element/LayoutBuilder.php
Renders the Layout UI.

File

core/modules/layout_builder/src/Element/LayoutBuilder.php, line 241

Class

LayoutBuilder
Defines a render element for building the Layout Builder UI.

Namespace

Drupal\layout_builder\Element

Code

protected function buildAdministrativeSection(SectionStorageInterface $section_storage, $delta) {
  $storage_type = $section_storage
    ->getStorageType();
  $storage_id = $section_storage
    ->getStorageId();
  $section = $section_storage
    ->getSection($delta);
  $layout = $section
    ->getLayout();
  $layout_settings = $section
    ->getLayoutSettings();
  $section_label = !empty($layout_settings['label']) ? $layout_settings['label'] : $this
    ->t('Section @section', [
    '@section' => $delta + 1,
  ]);
  $build = $section
    ->toRenderArray($this
    ->getAvailableContexts($section_storage), TRUE);
  $layout_definition = $layout
    ->getPluginDefinition();
  $region_labels = $layout_definition
    ->getRegionLabels();
  foreach ($layout_definition
    ->getRegions() as $region => $info) {
    if (!empty($build[$region])) {
      foreach (Element::children($build[$region]) as $uuid) {
        $build[$region][$uuid]['#attributes']['class'][] = 'js-layout-builder-block';
        $build[$region][$uuid]['#attributes']['class'][] = 'layout-builder-block';
        $build[$region][$uuid]['#attributes']['data-layout-block-uuid'] = $uuid;
        $build[$region][$uuid]['#attributes']['data-layout-builder-highlight-id'] = $this
          ->blockUpdateHighlightId($uuid);
        $build[$region][$uuid]['#contextual_links'] = [
          'layout_builder_block' => [
            'route_parameters' => [
              'section_storage_type' => $storage_type,
              'section_storage' => $storage_id,
              'delta' => $delta,
              'region' => $region,
              'uuid' => $uuid,
            ],
            // Add metadata about the current operations available in
            // contextual links. This will invalidate the client-side cache of
            // links that were cached before the 'move' link was added.
            // @see layout_builder.links.contextual.yml
            'metadata' => [
              'operations' => 'move:update:remove',
            ],
          ],
        ];
      }
    }
    $build[$region]['layout_builder_add_block']['link'] = [
      '#type' => 'link',
      // Add one to the current delta since it is zero-indexed.
      '#title' => $this
        ->t('Add block <span class="visually-hidden">in @section, @region region</span>', [
        '@section' => $section_label,
        '@region' => $region_labels[$region],
      ]),
      '#url' => Url::fromRoute('layout_builder.choose_block', [
        'section_storage_type' => $storage_type,
        'section_storage' => $storage_id,
        'delta' => $delta,
        'region' => $region,
      ], [
        'attributes' => [
          'class' => [
            'use-ajax',
            'layout-builder__link',
            'layout-builder__link--add',
          ],
          'data-dialog-type' => 'dialog',
          'data-dialog-renderer' => 'off_canvas',
        ],
      ]),
    ];
    $build[$region]['layout_builder_add_block']['#type'] = 'container';
    $build[$region]['layout_builder_add_block']['#attributes'] = [
      'class' => [
        'layout-builder__add-block',
      ],
      'data-layout-builder-highlight-id' => $this
        ->blockAddHighlightId($delta, $region),
    ];
    $build[$region]['layout_builder_add_block']['#weight'] = 1000;
    $build[$region]['#attributes']['data-region'] = $region;
    $build[$region]['#attributes']['class'][] = 'layout-builder__region';
    $build[$region]['#attributes']['class'][] = 'js-layout-builder-region';
    $build[$region]['#attributes']['role'] = 'group';
    $build[$region]['#attributes']['aria-label'] = $this
      ->t('@region region in @section', [
      '@region' => $info['label'],
      '@section' => $section_label,
    ]);

    // Get weights of all children for use by the region label.
    $weights = array_map(function ($a) {
      return isset($a['#weight']) ? $a['#weight'] : 0;
    }, $build[$region]);

    // The region label is made visible when the move block dialog is open.
    $build[$region]['region_label'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'layout__region-info',
          'layout-builder__region-label',
        ],
        // A more detailed version of this information is already read by
        // screen readers, so this label can be hidden from them.
        'aria-hidden' => TRUE,
      ],
      '#markup' => $this
        ->t('Region: @region', [
        '@region' => $info['label'],
      ]),
      // Ensures the region label is displayed first.
      '#weight' => min($weights) - 1,
    ];
  }
  $build['#attributes']['data-layout-update-url'] = Url::fromRoute('layout_builder.move_block', [
    'section_storage_type' => $storage_type,
    'section_storage' => $storage_id,
  ])
    ->toString();
  $build['#attributes']['data-layout-delta'] = $delta;
  $build['#attributes']['class'][] = 'layout-builder__layout';
  $build['#attributes']['data-layout-builder-highlight-id'] = $this
    ->sectionUpdateHighlightId($delta);
  return [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'layout-builder__section',
      ],
      'role' => 'group',
      'aria-label' => $section_label,
    ],
    'remove' => [
      '#type' => 'link',
      '#title' => $this
        ->t('Remove @section', [
        '@section' => $section_label,
      ]),
      '#url' => Url::fromRoute('layout_builder.remove_section', [
        'section_storage_type' => $storage_type,
        'section_storage' => $storage_id,
        'delta' => $delta,
      ]),
      '#attributes' => [
        'class' => [
          'use-ajax',
          'layout-builder__link',
          'layout-builder__link--remove',
        ],
        'data-dialog-type' => 'dialog',
        'data-dialog-renderer' => 'off_canvas',
      ],
    ],
    // The section label is added to sections without a "Configure section"
    // link, and is only visible when the move block dialog is open.
    'section_label' => [
      '#markup' => $this
        ->t('<span class="layout-builder__section-label" aria-hidden="true">@section</span>', [
        '@section' => $section_label,
      ]),
      '#access' => !$layout instanceof PluginFormInterface,
    ],
    'configure' => [
      '#type' => 'link',
      '#title' => $this
        ->t('Configure @section', [
        '@section' => $section_label,
      ]),
      '#access' => $layout instanceof PluginFormInterface,
      '#url' => Url::fromRoute('layout_builder.configure_section', [
        'section_storage_type' => $storage_type,
        'section_storage' => $storage_id,
        'delta' => $delta,
      ]),
      '#attributes' => [
        'class' => [
          'use-ajax',
          'layout-builder__link',
          'layout-builder__link--configure',
        ],
        'data-dialog-type' => 'dialog',
        'data-dialog-renderer' => 'off_canvas',
      ],
    ],
    'layout-builder__section' => $build,
  ];
}