class MoveBlockForm

Same name in other branches
  1. 9 core/modules/layout_builder/src/Form/MoveBlockForm.php \Drupal\layout_builder\Form\MoveBlockForm
  2. 8.9.x core/modules/layout_builder/src/Form/MoveBlockForm.php \Drupal\layout_builder\Form\MoveBlockForm
  3. 11.x 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

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 22

Namespace

Drupal\layout_builder\Form
View source
class MoveBlockForm extends FormBase implements WorkspaceDynamicSafeFormInterface {
    use AjaxFormHelperTrait;
    use LayoutBuilderContextTrait;
    use LayoutBuilderHighlightTrait;
    use LayoutRebuildTrait;
    use WorkspaceSafeFormTrait;
    
    /**
     * 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();
        $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) {
                // 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);
        }
        $state_weight_delta = round(count($components) / 2);
        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',
                        ],
                    ],
                    '#delta' => $state_weight_delta,
                ],
            ];
        }
        $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

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
AjaxFormHelperTrait::ajaxSubmit public function Submit form dialog #ajax callback.
AjaxHelperTrait::getRequestWrapperFormat protected function Gets the wrapper format of the current request.
AjaxHelperTrait::isAjax protected function Determines if the current request is via AJAX.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user. 2
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 57
LayoutBuilderContextTrait::$contextRepository protected property The context repository.
LayoutBuilderContextTrait::contextRepository protected function Gets the context repository service.
LayoutBuilderContextTrait::getPopulatedContexts protected function Returns all populated contexts, both global and section-storage-specific.
LayoutBuilderHighlightTrait::blockAddHighlightId protected function Provides the ID used to highlight the active Layout Builder UI element.
LayoutBuilderHighlightTrait::blockUpdateHighlightId protected function Provides the ID used to highlight the active Layout Builder UI element.
LayoutBuilderHighlightTrait::sectionAddHighlightId protected function Provides the ID used to highlight the active Layout Builder UI element.
LayoutBuilderHighlightTrait::sectionUpdateHighlightId protected function Provides the ID used to highlight the active Layout Builder UI element.
LayoutRebuildTrait::rebuildAndClose protected function Rebuilds the layout.
LayoutRebuildTrait::rebuildLayout protected function Rebuilds the layout.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 16
MessengerTrait::messenger public function Gets the messenger. 16
MessengerTrait::setMessenger public function Sets the messenger.
MoveBlockForm::$delta protected property The section delta.
MoveBlockForm::$layoutTempstore protected property The Layout Tempstore.
MoveBlockForm::$region protected property The region name.
MoveBlockForm::$sectionStorage protected property The section storage.
MoveBlockForm::$uuid protected property The component uuid.
MoveBlockForm::buildForm public function Builds the move block form. Overrides FormInterface::buildForm
MoveBlockForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
MoveBlockForm::getComponentsWrapper public function Ajax callback for the region select element.
MoveBlockForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
MoveBlockForm::getSelectedDelta protected function Gets the selected delta.
MoveBlockForm::getSelectedRegion protected function Gets the selected region.
MoveBlockForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
MoveBlockForm::successfulAjaxSubmit protected function Allows the form to respond to a successful AJAX submission. Overrides AjaxFormHelperTrait::successfulAjaxSubmit
MoveBlockForm::title public function Provides a title callback.
MoveBlockForm::__construct public function Constructs a new MoveBlockForm.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 2
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WorkspaceSafeFormTrait::$workspaceInfo protected property The workspace information service.
WorkspaceSafeFormTrait::getSectionStorageFromFormState protected function Retrieves the section storage from a form state object, if it exists.
WorkspaceSafeFormTrait::getWorkspaceInfo protected function Retrieves the workspace information service.
WorkspaceSafeFormTrait::isWorkspaceSafeEntity protected function Determines whether an entity used in a form is workspace-safe.
WorkspaceSafeFormTrait::isWorkspaceSafeEntityType protected function Determines whether an entity type used in a form is workspace-safe.
WorkspaceSafeFormTrait::isWorkspaceSafeForm public function Determines whether the current form is safe to be submitted in a workspace.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.