diff --git a/core/modules/layout_builder/src/Form/DefaultsEntityForm.php b/core/modules/layout_builder/src/Form/DefaultsEntityForm.php index b254e3424d5643b9d8239c4c9025619464334013..4e8cee0c0891d1344aa97dfd245ad90113daeed6 100644 --- a/core/modules/layout_builder/src/Form/DefaultsEntityForm.php +++ b/core/modules/layout_builder/src/Form/DefaultsEntityForm.php @@ -20,6 +20,7 @@ class DefaultsEntityForm extends EntityForm { use PreviewToggleTrait; + use LayoutBuilderEntityFormTrait; /** * Layout tempstore repository. @@ -65,13 +66,6 @@ public static function create(ContainerInterface $container) { ); } - /** - * {@inheritdoc} - */ - public function getBaseFormId() { - return $this->getEntity()->getEntityTypeId() . '_layout_builder_form'; - } - /** * {@inheritdoc} */ @@ -126,23 +120,7 @@ protected function buildMessage(LayoutEntityDisplayInterface $entity) { else { $message = $this->t('You are editing the layout template for all @plural_label.', $args); } - return [ - '#type' => 'container', - '#attributes' => [ - 'class' => [ - 'layout-builder__message', - 'layout-builder__message--defaults', - ], - ], - 'message' => [ - '#theme' => 'status_messages', - '#message_list' => ['status' => [$message]], - '#status_headings' => [ - 'status' => $this->t('Status message'), - ], - ], - '#weight' => -900, - ]; + return $this->buildMessageContainer($message, 'defaults'); } /** @@ -171,26 +149,7 @@ public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entit */ protected function actions(array $form, FormStateInterface $form_state) { $actions = parent::actions($form, $form_state); - $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; - } - - /** - * Form submission handler. - */ - public function redirectOnSubmit(array $form, FormStateInterface $form_state) { - $form_state->setRedirectUrl($this->sectionStorage->getLayoutBuilderUrl($form_state->getTriggeringElement()['#redirect'])); + return $this->buildActions($actions); } /** @@ -198,20 +157,8 @@ public function redirectOnSubmit(array $form, FormStateInterface $form_state) { */ public function save(array $form, FormStateInterface $form_state) { $return = $this->sectionStorage->save(); - $this->layoutTempstoreRepository->delete($this->sectionStorage); - $this->messenger()->addMessage($this->t('The layout has been saved.')); - $form_state->setRedirectUrl($this->sectionStorage->getRedirectUrl()); + $this->saveTasks($form_state, $this->t('The layout has been saved.')); return $return; } - /** - * Retrieves the section storage object. - * - * @return \Drupal\layout_builder\SectionStorageInterface - * The section storage for the current form. - */ - public function getSectionStorage() { - return $this->sectionStorage; - } - } diff --git a/core/modules/layout_builder/src/Form/LayoutBuilderEntityFormTrait.php b/core/modules/layout_builder/src/Form/LayoutBuilderEntityFormTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..c7e89af2c44a12b626f970c26169122d7d40f047 --- /dev/null +++ b/core/modules/layout_builder/src/Form/LayoutBuilderEntityFormTrait.php @@ -0,0 +1,109 @@ +<?php + +namespace Drupal\layout_builder\Form; + +use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; +use Drupal\layout_builder\SectionStorageInterface; + +/** + * Provides a trait for common methods used in Layout Builder entity forms. + */ +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()); + } + +} diff --git a/core/modules/layout_builder/src/Form/OverridesEntityForm.php b/core/modules/layout_builder/src/Form/OverridesEntityForm.php index 789778fc8f8e26a743a576bcca31e7e48abd61e2..87f647e31a4ec25ce421ca31d4211a9f2160e200 100644 --- a/core/modules/layout_builder/src/Form/OverridesEntityForm.php +++ b/core/modules/layout_builder/src/Form/OverridesEntityForm.php @@ -24,6 +24,7 @@ class OverridesEntityForm extends ContentEntityForm { use PreviewToggleTrait; + use LayoutBuilderEntityFormTrait; /** * Layout tempstore repository. @@ -68,13 +69,6 @@ public static function create(ContainerInterface $container) { ); } - /** - * {@inheritdoc} - */ - public function getBaseFormId() { - return $this->getEntity()->getEntityTypeId() . '_layout_builder_form'; - } - /** * {@inheritdoc} */ @@ -150,24 +144,7 @@ protected function buildMessage(EntityInterface $entity, OverridesSectionStorage $message = $this->t('You are editing the layout for this @singular_label.', $variables); } } - - return [ - '#type' => 'container', - '#attributes' => [ - 'class' => [ - 'layout-builder__message', - 'layout-builder__message--overrides', - ], - ], - 'message' => [ - '#theme' => 'status_messages', - '#message_list' => ['status' => [$message]], - '#status_headings' => [ - 'status' => $this->t('Status message'), - ], - ], - '#weight' => -900, - ]; + return $this->buildMessageContainer($message, 'overrides'); } /** @@ -175,10 +152,7 @@ protected function buildMessage(EntityInterface $entity, OverridesSectionStorage */ public function save(array $form, FormStateInterface $form_state) { $return = parent::save($form, $form_state); - - $this->layoutTempstoreRepository->delete($this->sectionStorage); - $this->messenger()->addStatus($this->t('The layout override has been saved.')); - $form_state->setRedirectUrl($this->sectionStorage->getRedirectUrl()); + $this->saveTasks($form_state, $this->t('The layout override has been saved.')); return $return; } @@ -187,20 +161,10 @@ public function save(array $form, FormStateInterface $form_state) { */ protected function actions(array $form, FormStateInterface $form_state) { $actions = parent::actions($form, $form_state); - $actions['#attributes']['role'] = 'region'; - $actions['#attributes']['aria-label'] = $this->t('Layout Builder tools'); - $actions['submit']['#value'] = $this->t('Save layout'); + $actions = $this->buildActions($actions); $actions['delete']['#access'] = FALSE; - $actions['#weight'] = -1000; - $actions['discard_changes'] = [ - '#type' => 'submit', - '#value' => $this->t('Discard changes'), - '#submit' => ['::redirectOnSubmit'], - '#redirect' => 'discard_changes', - // Discard is not dependent on form input. - '#limit_validation_errors' => [], - ]; + $actions['discard_changes']['#limit_validation_errors'] = []; // @todo This button should be conditionally displayed, see // https://www.drupal.org/node/2917777. $actions['revert'] = [ @@ -209,25 +173,7 @@ protected function actions(array $form, FormStateInterface $form_state) { '#submit' => ['::redirectOnSubmit'], '#redirect' => 'revert', ]; - $actions['preview_toggle'] = $this->buildContentPreviewToggle(); return $actions; } - /** - * 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() { - return $this->sectionStorage; - } - }