LayoutBuilderForm.php in Layout builder library 8
File
src/Form/LayoutBuilderForm.php
View source
<?php
namespace Drupal\layout_library\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
use Drupal\layout_builder\SectionStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LayoutBuilderForm extends EntityForm {
protected $layoutTempstoreRepository;
protected $sectionStorage;
public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository) {
$this->layoutTempstoreRepository = $layout_tempstore_repository;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('layout_builder.tempstore_repository'));
}
public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL) {
$form['layout_builder'] = [
'#type' => 'layout_builder',
'#section_storage' => $section_storage,
];
$this->sectionStorage = $section_storage;
return parent::buildForm($form, $form_state);
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this
->t('Save layout');
$actions['delete']['#access'] = FALSE;
$actions['#weight'] = -1000;
$actions['discard_changes'] = [
'#type' => 'link',
'#title' => $this
->t('Discard changes'),
'#attributes' => [
'class' => [
'button',
],
],
'#url' => $this->sectionStorage
->getLayoutBuilderUrl('discard_changes'),
];
return $actions;
}
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;
}
}