View source
<?php
namespace Drupal\page_manager_ui\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\page_manager\PageVariantInterface;
use Drupal\user\SharedTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class VariantPluginConfigureBlockFormBase extends FormBase {
use ContextAwarePluginAssignmentTrait;
protected $tempstore;
protected $variantPlugin;
protected $block;
public function __construct(SharedTempStoreFactory $tempstore) {
$this->tempstore = $tempstore;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('user.shared_tempstore'));
}
protected function getTempstoreId() {
return 'page_manager.block_display';
}
protected function getTempstore() {
return $this->tempstore
->get($this
->getTempstoreId());
}
protected abstract function prepareBlock($block_id);
protected abstract function submitText();
public function buildForm(array $form, FormStateInterface $form_state, $block_display = NULL, $block_id = NULL) {
$cached_values = $this->tempstore
->get('page_manager.block_display')
->get($block_display);
$this->variantPlugin = $cached_values['plugin'];
$contexts = [];
foreach ($cached_values['contexts'] as $context_name => $context_definition) {
$contexts[$context_name] = new Context($context_definition);
}
$this->variantPlugin
->setContexts($contexts);
$this->block = $this
->prepareBlock($block_id);
$form_state
->set('variant_id', $this
->getVariantPlugin()
->id());
$form_state
->set('block_id', $this->block
->getConfiguration()['uuid']);
$form['#tree'] = TRUE;
$form['settings'] = $this->block
->buildConfigurationForm([], $form_state);
$form['settings']['id'] = [
'#type' => 'value',
'#value' => $this->block
->getPluginId(),
];
$form['region'] = [
'#title' => $this
->t('Region'),
'#type' => 'select',
'#options' => $this
->getVariantPlugin()
->getRegionNames(),
'#default_value' => $this
->getVariantPlugin()
->getRegionAssignment($this->block
->getConfiguration()['uuid']),
'#required' => TRUE,
];
if ($this->block instanceof ContextAwarePluginInterface) {
$form['context_mapping'] = $this
->addContextAssignmentElement($this->block, $this
->getVariantPlugin()
->getContexts());
}
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->submitText(),
'#button_type' => 'primary',
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$this->block = $this
->getVariantPlugin()
->getBlock($form_state
->get('block_id'));
$settings = (new FormState())
->setValues($form_state
->getValue('settings'));
$this->block
->validateConfigurationForm($form, $settings);
$form_state
->setValue('settings', $settings
->getValues());
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$settings = (new FormState())
->setValues($form_state
->getValue('settings'));
$this->block
->submitConfigurationForm($form, $settings);
$form_state
->setValue('settings', $settings
->getValues());
if ($this->block instanceof ContextAwarePluginInterface) {
$this->block
->setContextMapping($form_state
->getValue('context_mapping', []));
}
$this
->getVariantPlugin()
->updateBlock($this->block
->getConfiguration()['uuid'], [
'region' => $form_state
->getValue('region'),
]);
$cached_values = $this
->getTempstore()
->get($form_state
->get('variant_id'));
$cached_values['plugin'] = $this
->getVariantPlugin();
$this
->getTempstore()
->set($form_state
->get('variant_id'), $cached_values);
}
protected function getVariantPlugin() {
return $this->variantPlugin;
}
}