LayoutBuilderForm.php in Page Manager 8.4
File
src/Form/LayoutBuilderForm.php
View source
<?php
namespace Drupal\page_manager\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\Core\TempStore\SharedTempStoreFactory;
use Drupal\layout_builder\Form\PreviewToggleTrait;
use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
use Drupal\layout_builder\SectionStorage\SectionStorageManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LayoutBuilderForm extends FormBase {
use PreviewToggleTrait;
protected $layoutTempstoreRepository;
protected $sectionStorageManager;
protected $sectionStorage;
protected $tempstore;
public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, SectionStorageManager $section_storage_manager, SharedTempStoreFactory $tempstore) {
$this->layoutTempstoreRepository = $layout_tempstore_repository;
$this->sectionStorageManager = $section_storage_manager;
$this->tempstore = $tempstore;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('layout_builder.tempstore_repository'), $container
->get('plugin.manager.layout_builder.section_storage'), $container
->get('tempstore.shared'));
}
public function getFormId() {
return 'page_manager_layout_builder_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$page_variant = $cached_values['page_variant'];
if ($page_variant
->isNew()) {
$this->tempstore
->get('page_manager.layout_builder')
->set($page_variant
->id(), $page_variant);
}
$section_storage = $this->sectionStorageManager
->load('page_manager', [
'entity' => EntityContext::fromEntity($page_variant),
]);
$this->sectionStorage = $this->layoutTempstoreRepository
->get($section_storage);
$form['preview_toggle'] = $this
->buildContentPreviewToggle();
$form['layout_builder'] = [
'#type' => 'layout_builder',
'#section_storage' => $this->sectionStorage,
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$page_variant = $this->sectionStorage
->getContextValue('entity');
$variant_plugin = $cached_values['plugin'];
$variant_plugin
->setConfiguration($page_variant
->get('variant_settings'));
$this->layoutTempstoreRepository
->delete($this->sectionStorage);
$this
->messenger()
->addMessage($this
->t('The layout has been saved.'));
$this->tempstore
->get('page_manager.layout_builder')
->delete($page_variant
->id());
$this->tempstore
->get('page_manager.page_variant')
->delete($page_variant
->id());
}
}