class MoveBlockForm in Drupal 8
Same name and namespace in other branches
- 9 core/modules/layout_builder/src/Form/MoveBlockForm.php \Drupal\layout_builder\Form\MoveBlockForm
Provides a form for moving a block.
@internal Form classes are internal.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\layout_builder\Form\MoveBlockForm uses AjaxFormHelperTrait, LayoutRebuildTrait, LayoutBuilderHighlightTrait
Expanded class hierarchy of MoveBlockForm
1 string reference to 'MoveBlockForm'
- layout_builder.routing.yml in core/
modules/ layout_builder/ layout_builder.routing.yml - core/modules/layout_builder/layout_builder.routing.yml
File
- core/
modules/ layout_builder/ src/ Form/ MoveBlockForm.php, line 20
Namespace
Drupal\layout_builder\FormView source
class MoveBlockForm extends FormBase {
use AjaxFormHelperTrait;
use LayoutBuilderHighlightTrait;
use LayoutRebuildTrait;
/**
* The section storage.
*
* @var \Drupal\layout_builder\SectionStorageInterface
*/
protected $sectionStorage;
/**
* The section delta.
*
* @var int
*/
protected $delta;
/**
* The region name.
*
* @var string
*/
protected $region;
/**
* The component uuid.
*
* @var string
*/
protected $uuid;
/**
* The Layout Tempstore.
*
* @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
*/
protected $layoutTempstore;
/**
* Constructs a new MoveBlockForm.
*
* @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
* The layout tempstore.
*/
public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository) {
$this->layoutTempstore = $layout_tempstore_repository;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('layout_builder.tempstore_repository'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'layout_builder_block_move';
}
/**
* Builds the move block form.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param \Drupal\layout_builder\SectionStorageInterface $section_storage
* The section storage being configured.
* @param int $delta
* The original delta of the section.
* @param string $region
* The original region of the block.
* @param string $uuid
* The UUID of the block being updated.
*
* @return array
* The form array.
*/
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;
}
/**
* {@inheritdoc}
*/
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) {
// Remove component from old section and add it to the new section.
$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);
}
/**
* Ajax callback for the region select element.
*
* @param array $form
* The form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*
* @return array
* The components wrapper render array.
*/
public function getComponentsWrapper(array $form, FormStateInterface $form_state) {
return $form['components_wrapper'];
}
/**
* {@inheritdoc}
*/
protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
return $this
->rebuildAndClose($this->sectionStorage);
}
/**
* Gets the selected region.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*
* @return string
* The current region name.
*/
protected function getSelectedRegion(FormStateInterface $form_state) {
if ($form_state
->hasValue('region')) {
return explode(':', $form_state
->getValue('region'), 2)[1];
}
return $this->region;
}
/**
* Gets the selected delta.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*
* @return int
* The section delta.
*/
protected function getSelectedDelta(FormStateInterface $form_state) {
if ($form_state
->hasValue('region')) {
return (int) explode(':', $form_state
->getValue('region'))[0];
}
return (int) $this->delta;
}
/**
* Provides a title callback.
*
* @param \Drupal\layout_builder\SectionStorageInterface $section_storage
* The section storage.
* @param int $delta
* The original delta of the section.
* @param string $uuid
* The UUID of the block being updated.
*
* @return string
* The title for the move block form.
*/
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,
]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AjaxFormHelperTrait:: |
public | function | Submit form dialog #ajax callback. | |
AjaxHelperTrait:: |
protected | function | Gets the wrapper format of the current request. | |
AjaxHelperTrait:: |
protected | function | Determines if the current request is via AJAX. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LayoutBuilderHighlightTrait:: |
protected | function | Provides the ID used to highlight the active Layout Builder UI element. | |
LayoutBuilderHighlightTrait:: |
protected | function | Provides the ID used to highlight the active Layout Builder UI element. | |
LayoutBuilderHighlightTrait:: |
protected | function | Provides the ID used to highlight the active Layout Builder UI element. | |
LayoutBuilderHighlightTrait:: |
protected | function | Provides the ID used to highlight the active Layout Builder UI element. | |
LayoutRebuildTrait:: |
protected | function | Rebuilds the layout. | |
LayoutRebuildTrait:: |
protected | function | Rebuilds the layout. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
MoveBlockForm:: |
protected | property | The section delta. | |
MoveBlockForm:: |
protected | property | The Layout Tempstore. | |
MoveBlockForm:: |
protected | property | The region name. | |
MoveBlockForm:: |
protected | property | The section storage. | |
MoveBlockForm:: |
protected | property | The component uuid. | |
MoveBlockForm:: |
public | function |
Builds the move block form. Overrides FormInterface:: |
|
MoveBlockForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
MoveBlockForm:: |
public | function | Ajax callback for the region select element. | |
MoveBlockForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
MoveBlockForm:: |
protected | function | Gets the selected delta. | |
MoveBlockForm:: |
protected | function | Gets the selected region. | |
MoveBlockForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
MoveBlockForm:: |
protected | function |
Allows the form to respond to a successful AJAX submission. Overrides AjaxFormHelperTrait:: |
|
MoveBlockForm:: |
public | function | Provides a title callback. | |
MoveBlockForm:: |
public | function | Constructs a new MoveBlockForm. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |