View source
<?php
namespace Drupal\layout_builder\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface;
use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
use Drupal\layout_builder\SectionStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DefaultsEntityForm extends EntityForm {
use PreviewToggleTrait;
protected $layoutTempstoreRepository;
protected $entityTypeBundleInfo;
protected $sectionStorage;
public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
$this->layoutTempstoreRepository = $layout_tempstore_repository;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('layout_builder.tempstore_repository'), $container
->get('entity_type.bundle.info'));
}
public function getBaseFormId() {
return $this
->getEntity()
->getEntityTypeId() . '_layout_builder_form';
}
public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL) {
$form['#attributes']['class'][] = 'layout-builder-form';
$form['layout_builder'] = [
'#type' => 'layout_builder',
'#section_storage' => $section_storage,
'#process' => [
[
static::class,
'layoutBuilderElementGetKeys',
],
],
];
$form['layout_builder_message'] = $this
->buildMessage($section_storage
->getContextValue('display'));
$this->sectionStorage = $section_storage;
return parent::buildForm($form, $form_state);
}
public static function layoutBuilderElementGetKeys(array $element, FormStateInterface $form_state, &$form) {
$form['#layout_builder_element_keys'] = $element['#array_parents'];
return $element;
}
protected function buildMessage(LayoutEntityDisplayInterface $entity) {
$entity_type_id = $entity
->getTargetEntityTypeId();
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
$bundle_info = $this->entityTypeBundleInfo
->getBundleInfo($entity_type_id);
$args = [
'@bundle' => $bundle_info[$entity
->getTargetBundle()]['label'],
'@plural_label' => $entity_type
->getPluralLabel(),
];
if ($entity_type
->hasKey('bundle')) {
$message = $this
->t('You are editing the layout template for all @bundle @plural_label.', $args);
}
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,
];
}
public function buildEntity(array $form, FormStateInterface $form_state) {
$this
->setEntity($this->sectionStorage
->getContextValue('display'));
$entity = parent::buildEntity($form, $form_state);
$this->sectionStorage
->setContextValue('display', $entity);
return $entity;
}
public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id) {
$route_parameters = $route_match
->getParameters()
->all();
return $this->entityTypeManager
->getStorage('entity_view_display')
->load($route_parameters['entity_type_id'] . '.' . $route_parameters['bundle'] . '.' . $route_parameters['view_mode_name']);
}
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;
}
public function redirectOnSubmit(array $form, FormStateInterface $form_state) {
$form_state
->setRedirectUrl($this->sectionStorage
->getLayoutBuilderUrl($form_state
->getTriggeringElement()['#redirect']));
}
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());
return $return;
}
public function getSectionStorage() {
return $this->sectionStorage;
}
}