View source
<?php
namespace Drupal\panels_ipe\Form;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\RendererInterface;
use Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant;
use Drupal\panels_ipe\PanelsIPEBlockRendererTrait;
use Drupal\user\SharedTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PanelsIPEBlockPluginForm extends FormBase {
use ContextAwarePluginAssignmentTrait;
use PanelsIPEBlockRendererTrait;
protected $blockManager;
protected $renderer;
protected $tempStore;
protected $panelsDisplay;
public function __construct(PluginManagerInterface $block_manager, ContextHandlerInterface $context_handler, RendererInterface $renderer, SharedTempStoreFactory $temp_store_factory) {
$this->blockManager = $block_manager;
$this->contextHandler = $context_handler;
$this->renderer = $renderer;
$this->tempStore = $temp_store_factory
->get('panels_ipe');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.block'), $container
->get('context.handler'), $container
->get('renderer'), $container
->get('user.shared_tempstore'));
}
public function getFormId() {
return 'panels_ipe_block_plugin_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $plugin_id = NULL, PanelsDisplayVariant $panels_display = NULL, $uuid = NULL) {
if (!$plugin_id || !$panels_display) {
return FALSE;
}
$this->panelsDisplay = $panels_display;
$regions = $panels_display
->getRegionNames();
if ($uuid) {
$block_instance = $panels_display
->getBlock($uuid);
}
else {
$block_instance = $this->blockManager
->createInstance($plugin_id);
}
$block_config = $block_instance
->getConfiguration();
if (isset($block_config['region']) && isset($regions[$block_config['region']])) {
$region = $block_config['region'];
}
else {
$region = reset($regions);
}
$form_state
->set('block_theme', $this
->config('system.theme')
->get('default'));
$form['#prefix'] = '<div id="panels-ipe-block-plugin-form-wrapper">';
$form['#suffix'] = '</div>';
$form['flipper'] = [
'#type' => 'container',
'#attributes' => [
'class' => 'flipper',
],
];
$form['flipper']['front'] = [
'#type' => 'container',
'#attributes' => [
'class' => 'front',
],
];
$form['flipper']['back'] = [
'#type' => 'container',
'#attributes' => [
'class' => 'back',
],
];
$form['#attributes']['class'][] = 'flip-container';
$form['flipper']['front']['settings'] = $block_instance
->buildConfigurationForm([], $form_state);
$form['flipper']['front']['settings']['context_mapping'] = $this
->addContextAssignmentElement($block_instance, $this->panelsDisplay
->getContexts());
$form['flipper']['front']['settings']['#tree'] = TRUE;
$form['plugin_id'] = [
'#type' => 'value',
'#value' => $plugin_id,
];
$form['variant_id'] = [
'#type' => 'value',
'#value' => $panels_display
->id(),
];
$form['uuid'] = [
'#type' => 'value',
'#value' => $uuid,
];
$form['flipper']['front']['settings']['region'] = [
'#title' => $this
->t('Region'),
'#type' => 'select',
'#options' => $regions,
'#required' => TRUE,
'#default_value' => $region,
];
$form['submit'] = [
'#type' => 'button',
'#value' => $uuid ? $this
->t('Update') : $this
->t('Add'),
'#ajax' => [
'callback' => '::submitForm',
'wrapper' => 'panels-ipe-block-plugin-form-wrapper',
'method' => 'replace',
'progress' => [
'type' => 'throbber',
'message' => '',
],
],
];
$form['preview'] = [
'#type' => 'button',
'#value' => $this
->t('Toggle Preview'),
'#ajax' => [
'callback' => '::submitPreview',
'wrapper' => 'panels-ipe-block-plugin-form-wrapper',
'method' => 'replace',
'progress' => [
'type' => 'throbber',
'message' => '',
],
],
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$block_instance = $this
->getBlockInstance($form_state);
$block_form_state = (new FormState())
->setValues($form_state
->getValue('settings'));
$block_instance
->validateConfigurationForm($form, $block_form_state);
$form_state
->setValue('settings', $block_form_state
->getValues());
}
protected function submitBlock(BlockPluginInterface $block_instance, array $form, FormStateInterface $form_state) {
$block_form_state = (new FormState())
->setValues($form_state
->getValue('settings'));
$block_instance
->submitConfigurationForm($form['flipper']['front']['settings'], $block_form_state);
if ($block_instance instanceof ContextAwarePluginInterface) {
$block_instance
->setContextMapping($block_form_state
->getValue('context_mapping', []));
}
$form_state
->setValue('settings', $block_form_state
->getValues());
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->hasAnyErrors()) {
return $form;
}
$temp_store_key = $this->panelsDisplay
->id();
if ($variant_config = $this->tempStore
->get($temp_store_key)) {
$this->panelsDisplay
->setConfiguration($variant_config);
}
$block_instance = $this
->getBlockInstance($form_state);
$this
->submitBlock($block_instance, $form, $form_state);
$block_config = $block_instance
->getConfiguration();
$block_config['region'] = $form_state
->getValue(array(
'settings',
'region',
));
if ($uuid = $form_state
->getValue('uuid')) {
$this->panelsDisplay
->updateBlock($uuid, $block_config);
}
else {
$uuid = $this->panelsDisplay
->addBlock($block_config);
}
$this->tempStore
->set($this->panelsDisplay
->id(), $this->panelsDisplay
->getConfiguration());
$build = $this
->buildBlockInstance($block_instance, $this->panelsDisplay);
$this
->bubbleBlockAttributes($build);
$build['#attributes']['data-block-id'] = $uuid;
$plugin_definition = $block_instance
->getPluginDefinition();
$block_model = [
'uuid' => $uuid,
'label' => $block_instance
->label(),
'id' => $block_instance
->getPluginId(),
'region' => $block_config['region'],
'provider' => $block_config['provider'],
'plugin_id' => $plugin_definition['id'],
'html' => $this->renderer
->render($build),
];
$form['build'] = $build;
$form['#attached']['drupalSettings']['panels_ipe']['updated_block'] = $block_model;
return $form;
}
public function submitPreview(array &$form, FormStateInterface $form_state) {
if ($form_state
->hasAnyErrors()) {
return $form;
}
$block_instance = $this
->getBlockInstance($form_state);
$this
->submitBlock($block_instance, $form, $form_state);
$build = $this
->buildBlockInstance($block_instance, $this->panelsDisplay);
$build['content']['#post_render'][] = function ($html, array $elements) {
$search = [
'<form',
'</form>',
];
$replace = [
'<div',
'</div>',
];
return str_replace($search, $replace, $html);
};
$form['flipper']['back']['preview'] = $build;
$build['clearfix'] = [
'#markup' => '<div class="clearfix"></div>',
];
$form['#attached']['drupalSettings']['panels_ipe']['toggle_preview'] = TRUE;
return $form;
}
protected function getBlockInstance(FormStateInterface $form_state) {
if ($uuid = $form_state
->getValue('uuid')) {
$temp_store_key = $this->panelsDisplay
->id();
if ($variant_config = $this->tempStore
->get($temp_store_key)) {
$this->panelsDisplay
->setConfiguration($variant_config);
}
$block_instance = $this->panelsDisplay
->getBlock($uuid);
}
else {
$block_instance = $this->blockManager
->createInstance($form_state
->getValue('plugin_id'));
}
return $block_instance;
}
}