trait LayoutBuilderEntityFormTrait
Same name in other branches
- 11.x core/modules/layout_builder/src/Form/LayoutBuilderEntityFormTrait.php \Drupal\layout_builder\Form\LayoutBuilderEntityFormTrait
Provides a trait for common methods used in Layout Builder entity forms.
Hierarchy
- trait \Drupal\layout_builder\Form\LayoutBuilderEntityFormTrait uses \Drupal\layout_builder\Form\PreviewToggleTrait
1 file declares its use of LayoutBuilderEntityFormTrait
- LayoutForm.php in core/
modules/ navigation/ src/ Form/ LayoutForm.php
File
-
core/
modules/ layout_builder/ src/ Form/ LayoutBuilderEntityFormTrait.php, line 12
Namespace
Drupal\layout_builder\FormView source
trait LayoutBuilderEntityFormTrait {
use PreviewToggleTrait;
/**
* {@inheritdoc}
*/
public function getBaseFormId() : string {
return $this->getEntity()
->getEntityTypeId() . '_layout_builder_form';
}
/**
* Build the message container.
*
* @param \Drupal\Core\StringTranslation\TranslatableMarkup $message
* The message to display.
* @param string $type
* The form type this is being attached to.
*
* @return array
* The render array.
*/
protected function buildMessageContainer(TranslatableMarkup $message, string $type) : array {
return [
'#type' => 'container',
'#attributes' => [
'class' => [
'layout-builder__message',
sprintf('layout-builder__message--%s', $type),
],
],
'message' => [
'#theme' => 'status_messages',
'#message_list' => [
'status' => [
$message,
],
],
'#status_headings' => [
'status' => $this->t('Status message'),
],
],
'#weight' => -900,
];
}
/**
* Form submission handler.
*/
public function redirectOnSubmit(array $form, FormStateInterface $form_state) {
$form_state->setRedirectUrl($this->sectionStorage
->getLayoutBuilderUrl($form_state->getTriggeringElement()['#redirect']));
}
/**
* Retrieves the section storage object.
*
* @return \Drupal\layout_builder\SectionStorageInterface
* The section storage for the current form.
*/
public function getSectionStorage() : SectionStorageInterface {
return $this->sectionStorage;
}
/**
* Builds the actions for the form.
*
* @param array $actions
* The actions array to modify.
*
* @return array
* The modified actions array.
*/
protected function buildActions(array $actions) : array {
$actions['#attributes']['role'] = 'region';
$actions['#attributes']['aria-label'] = $this->t('Layout Builder tools');
$actions['submit']['#value'] = $this->t('Save layout');
$actions['#weight'] = -1000;
$actions['discard_changes'] = [
'#type' => 'submit',
'#value' => $this->t('Discard changes'),
'#submit' => [
'::redirectOnSubmit',
],
'#redirect' => 'discard_changes',
];
$actions['preview_toggle'] = $this->buildContentPreviewToggle();
return $actions;
}
/**
* Performs tasks that are needed during the save process.
*
* @param \Drupal\Core\Form\FormStateInterface $formState
* The form state.
* @param \Drupal\Core\StringTranslation\TranslatableMarkup $message
* The message to display.
*/
protected function saveTasks(FormStateInterface $formState, TranslatableMarkup $message) : void {
$this->layoutTempstoreRepository
->delete($this->getSectionStorage());
$this->messenger()
->addStatus($message);
$formState->setRedirectUrl($this->getSectionStorage()
->getRedirectUrl());
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overrides |
---|---|---|---|---|
LayoutBuilderEntityFormTrait::buildActions | protected | function | Builds the actions for the form. | |
LayoutBuilderEntityFormTrait::buildMessageContainer | protected | function | Build the message container. | |
LayoutBuilderEntityFormTrait::getBaseFormId | public | function | 1 | |
LayoutBuilderEntityFormTrait::getSectionStorage | public | function | Retrieves the section storage object. | |
LayoutBuilderEntityFormTrait::redirectOnSubmit | public | function | Form submission handler. | |
LayoutBuilderEntityFormTrait::saveTasks | protected | function | Performs tasks that are needed during the save process. | |
PreviewToggleTrait::buildContentPreviewToggle | protected | function | Builds the content preview toggle input. | |
PreviewToggleTrait::currentUser | abstract protected | function | Gets the current user. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.