You are here

public function MoveBlockForm::buildForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/layout_builder/src/Form/MoveBlockForm.php \Drupal\layout_builder\Form\MoveBlockForm::buildForm()

Builds the move block form.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

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

int $delta: The original delta of the section.

string $region: The original region of the block.

string $uuid: The UUID of the block being updated.

Return value

array The form array.

Overrides FormInterface::buildForm

File

core/modules/layout_builder/src/Form/MoveBlockForm.php, line 106

Class

MoveBlockForm
Provides a form for moving a block.

Namespace

Drupal\layout_builder\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL, $region = NULL, $uuid = NULL) {
  $parameters = array_slice(func_get_args(), 2);
  foreach ($parameters as $parameter) {
    if (is_null($parameter)) {
      throw new \InvalidArgumentException('MoveBlockForm requires all parameters.');
    }
  }
  $this->sectionStorage = $section_storage;
  $this->delta = $delta;
  $this->uuid = $uuid;
  $this->region = $region;
  $form['#attributes']['data-layout-builder-target-highlight-id'] = $this
    ->blockUpdateHighlightId($uuid);
  $sections = $section_storage
    ->getSections();
  $region_options = [];
  foreach ($sections as $section_delta => $section) {
    $layout = $section
      ->getLayout();
    $layout_definition = $layout
      ->getPluginDefinition();
    if (!($section_label = $section
      ->getLayoutSettings()['label'])) {
      $section_label = $this
        ->t('Section: @delta', [
        '@delta' => $section_delta + 1,
      ])
        ->render();
    }
    foreach ($layout_definition
      ->getRegions() as $region_name => $region_info) {

      // Group regions by section.
      $region_options[$section_label]["{$section_delta}:{$region_name}"] = $this
        ->t('@section, Region: @region', [
        '@section' => $section_label,
        '@region' => $region_info['label'],
      ]);
    }
  }

  // $this->region and $this->delta are where the block is currently placed.
  // $selected_region and $selected_delta are the values from this form
  // specifying where the block should be moved to.
  $selected_region = $this
    ->getSelectedRegion($form_state);
  $selected_delta = $this
    ->getSelectedDelta($form_state);
  $form['region'] = [
    '#type' => 'select',
    '#options' => $region_options,
    '#title' => $this
      ->t('Region'),
    '#default_value' => "{$selected_delta}:{$selected_region}",
    '#ajax' => [
      'wrapper' => 'layout-builder-components-table',
      'callback' => '::getComponentsWrapper',
    ],
  ];
  $current_section = $sections[$selected_delta];
  $aria_label = $this
    ->t('Blocks in Section: @section, Region: @region', [
    '@section' => $selected_delta + 1,
    '@region' => $selected_region,
  ]);
  $form['components_wrapper']['components'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Block label'),
      $this
        ->t('Weight'),
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'table-sort-weight',
      ],
    ],
    // Create a wrapping element so that the Ajax update also replaces the
    // 'Show block weights' link.
    '#theme_wrappers' => [
      'container' => [
        '#attributes' => [
          'id' => 'layout-builder-components-table',
          'class' => [
            'layout-builder-components-table',
          ],
          'aria-label' => $aria_label,
        ],
      ],
    ],
  ];

  /** @var \Drupal\layout_builder\SectionComponent[] $components */
  $components = $current_section
    ->getComponentsByRegion($selected_region);

  // If the component is not in this region, add it to the listed components.
  if (!isset($components[$uuid])) {
    $components[$uuid] = $sections[$delta]
      ->getComponent($uuid);
  }
  foreach ($components as $component_uuid => $component) {

    /** @var \Drupal\Core\Block\BlockPluginInterface $plugin */
    $plugin = $component
      ->getPlugin();
    $is_current_block = $component_uuid === $uuid;
    $row_classes = [
      'draggable',
      'layout-builder-components-table__row',
    ];
    $label['#wrapper_attributes']['class'] = [
      'layout-builder-components-table__block-label',
    ];
    if ($is_current_block) {

      // Highlight the current block.
      $label['#markup'] = $this
        ->t('@label (current)', [
        '@label' => $plugin
          ->label(),
      ]);
      $label['#wrapper_attributes']['class'][] = 'layout-builder-components-table__block-label--current';
      $row_classes[] = 'layout-builder-components-table__row--current';
    }
    else {
      $label['#markup'] = $plugin
        ->label();
    }
    $form['components_wrapper']['components'][$component_uuid] = [
      '#attributes' => [
        'class' => $row_classes,
      ],
      'label' => $label,
      'weight' => [
        '#type' => 'weight',
        '#default_value' => $component
          ->getWeight(),
        '#title' => $this
          ->t('Weight for @block block', [
          '@block' => $plugin
            ->label(),
        ]),
        '#title_display' => 'invisible',
        '#attributes' => [
          'class' => [
            'table-sort-weight',
          ],
        ],
      ],
    ];
  }
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Move'),
    '#button_type' => 'primary',
  ];
  $form['#attributes']['data-add-layout-builder-wrapper'] = 'layout-builder--move-blocks-active';
  if ($this
    ->isAjax()) {
    $form['actions']['submit']['#ajax']['callback'] = '::ajaxSubmit';
  }
  return $form;
}