View source
<?php
namespace Drupal\panels_ipe\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Layout\LayoutPluginManagerInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\RendererInterface;
use Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant;
use Drupal\Core\TempStore\SharedTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PanelsIPELayoutForm extends FormBase {
protected $renderer;
protected $tempStore;
protected $layoutManager;
protected $panelsDisplay;
protected $layout;
public function __construct(LayoutPluginManagerInterface $layout_manager, RendererInterface $renderer, SharedTempStoreFactory $temp_store_factory) {
$this->layoutManager = $layout_manager;
$this->renderer = $renderer;
$this->tempStore = $temp_store_factory
->get('panels_ipe');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.core.layout'), $container
->get('renderer'), $container
->get('tempstore.shared'));
}
public function getFormId() {
return 'panels_ipe_layout_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $layout_id = NULL, PanelsDisplayVariant $panels_display = NULL) {
if (!$layout_id || !$panels_display) {
return FALSE;
}
$this->panelsDisplay = $panels_display;
$layout = $this->panelsDisplay
->getLayout();
$current = $layout
->getPluginId() == $layout_id;
if (!$current) {
$layout = $this->layoutManager
->createInstance($layout_id, []);
}
$this->layout = $layout;
if ($layout instanceof PluginFormInterface) {
$form['settings'] = $layout
->buildConfigurationForm([], $form_state);
}
$form['settings']['#tree'] = TRUE;
if (empty(Element::getVisibleChildren($form['settings']))) {
if ($current) {
$form['settings'][] = [
'#markup' => $this
->t('<h5>This layout does not provide any configuration.</h5>'),
];
}
else {
$this
->submitForm($form, $form_state);
}
}
$form['submit'] = [
'#type' => 'button',
'#value' => $current ? $this
->t('Update') : $this
->t('Change Layout'),
'#ajax' => [
'callback' => '::submitForm',
'wrapper' => 'panels-ipe-layout-form-wrapper',
'method' => 'replace',
'progress' => [
'type' => 'throbber',
'message' => '',
],
],
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($this->layout instanceof PluginFormInterface) {
$layout_form_state = (new FormState())
->setValues($form_state
->getValue('settings', []));
$this->layout
->validateConfigurationForm($form, $layout_form_state);
$form_state
->setValue('settings', $layout_form_state
->getValues());
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->hasAnyErrors()) {
return $form;
}
$panels_display = $this->panelsDisplay;
if ($this->layout instanceof PluginFormInterface) {
$layout_form_state = (new FormState())
->setValues($form_state
->getValue('settings', []));
$this->layout
->submitConfigurationForm($form, $layout_form_state);
}
$layout_config = $this->layout
->getConfiguration();
$first_region = $this->layout
->getPluginDefinition()
->getDefaultRegion();
foreach ($panels_display
->getRegionAssignments() as $region => $region_assignment) {
foreach ($region_assignment as $block_id => $block) {
$block_config = $block
->getConfiguration();
if (!isset($region_definitions[$block_config['region']])) {
$block_config['region'] = $first_region;
$panels_display
->updateBlock($block_id, $block_config);
}
}
}
$this->panelsDisplay
->setLayout($this->layout, $layout_config);
$this->tempStore
->set($panels_display
->getTempStoreId(), $panels_display
->getConfiguration());
$region_data = [];
$region_content = [];
$regions = $panels_display
->getRegionAssignments();
foreach ($regions as $id => $label) {
$region_name = Html::getClass("block-region-{$id}");
$region_content[$id] = [
'#prefix' => '<div class="' . $region_name . '" data-region-name="' . $id . '">',
'#suffix' => '</div>',
];
$region_data[] = [
'name' => $id,
'label' => $label,
];
}
$build = $panels_display
->getLayout()
->build($region_content);
$form['build'] = $build;
$data = [
'id' => $this->layout
->getPluginId(),
'label' => $this->layout
->getPluginDefinition()
->getLabel(),
'current' => TRUE,
'html' => $this->renderer
->render($build),
'regions' => $region_data,
];
$form['#attached']['drupalSettings']['panels_ipe']['updated_layout'] = $data;
return $form;
}
}