View source
<?php
namespace Drupal\layout_builder\Form;
use Drupal\Core\Ajax\AjaxFormHelperTrait;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\layout_builder\Context\LayoutBuilderContextTrait;
use Drupal\layout_builder\Controller\LayoutRebuildTrait;
use Drupal\layout_builder\LayoutBuilderHighlightTrait;
use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
use Drupal\layout_builder\SectionStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MoveBlockForm extends FormBase {
use AjaxFormHelperTrait;
use LayoutBuilderContextTrait;
use LayoutBuilderHighlightTrait;
use LayoutRebuildTrait;
protected $sectionStorage;
protected $delta;
protected $region;
protected $uuid;
protected $layoutTempstore;
public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository) {
$this->layoutTempstore = $layout_tempstore_repository;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('layout_builder.tempstore_repository'));
}
public function getFormId() {
return 'layout_builder_block_move';
}
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();
$contexts = $this
->getPopulatedContexts($section_storage);
$region_options = [];
foreach ($sections as $section_delta => $section) {
$layout = $section
->getLayout($contexts);
$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) {
$region_options[$section_label]["{$section_delta}:{$region_name}"] = $this
->t('@section, Region: @region', [
'@section' => $section_label,
'@region' => $region_info['label'],
]);
}
}
$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',
],
],
'#theme_wrappers' => [
'container' => [
'#attributes' => [
'id' => 'layout-builder-components-table',
'class' => [
'layout-builder-components-table',
],
'aria-label' => $aria_label,
],
],
],
];
$components = $current_section
->getComponentsByRegion($selected_region);
if (!isset($components[$uuid])) {
$components[$uuid] = $sections[$delta]
->getComponent($uuid);
}
foreach ($components as $component_uuid => $component) {
$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) {
$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;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$region = $this
->getSelectedRegion($form_state);
$delta = $this
->getSelectedDelta($form_state);
$original_section = $this->sectionStorage
->getSection($this->delta);
$component = $original_section
->getComponent($this->uuid);
$section = $this->sectionStorage
->getSection($delta);
if ($delta !== $this->delta) {
$original_section
->removeComponent($this->uuid);
$section
->insertComponent(0, $component);
}
$component
->setRegion($region);
foreach ($form_state
->getValue('components') as $uuid => $component_info) {
$section
->getComponent($uuid)
->setWeight($component_info['weight']);
}
$this->layoutTempstore
->set($this->sectionStorage);
}
public function getComponentsWrapper(array $form, FormStateInterface $form_state) {
return $form['components_wrapper'];
}
protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
return $this
->rebuildAndClose($this->sectionStorage);
}
protected function getSelectedRegion(FormStateInterface $form_state) {
if ($form_state
->hasValue('region')) {
return explode(':', $form_state
->getValue('region'), 2)[1];
}
return $this->region;
}
protected function getSelectedDelta(FormStateInterface $form_state) {
if ($form_state
->hasValue('region')) {
return (int) explode(':', $form_state
->getValue('region'))[0];
}
return (int) $this->delta;
}
public function title(SectionStorageInterface $section_storage, $delta, $uuid) {
$block_label = $section_storage
->getSection($delta)
->getComponent($uuid)
->getPlugin()
->label();
return $this
->t('Move the @block_label block', [
'@block_label' => $block_label,
]);
}
}