View source
<?php
namespace Drupal\panels\Form;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\layout_plugin\Layout;
use Drupal\layout_plugin\Plugin\Layout\LayoutInterface;
use Drupal\layout_plugin\Plugin\Layout\LayoutPluginManagerInterface;
use Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant;
use Drupal\user\SharedTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LayoutChangeSettings extends FormBase {
protected $manager;
protected $tempstore;
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.layout_plugin'), $container
->get('user.shared_tempstore'));
}
public function __construct(LayoutPluginManagerInterface $manager, SharedTempStoreFactory $tempstore) {
$this->manager = $manager;
$this->tempstore = $tempstore;
}
public function getFormId() {
return 'panels_layout_settings_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$variant_plugin = $cached_values['plugin'];
$form['old_layout'] = [
'#title' => $this
->t('Old Layout'),
'#type' => 'select',
'#options' => Layout::getLayoutOptions([
'group_by_category' => TRUE,
]),
'#default_value' => !empty($cached_values['layout_change']['old_layout']) ? $cached_values['layout_change']['old_layout'] : '',
'#disabled' => TRUE,
'#access' => !empty($cached_values['layout_change']),
];
$form['new_layout'] = [
'#title' => $this
->t('New Layout'),
'#type' => 'select',
'#options' => Layout::getLayoutOptions([
'group_by_category' => TRUE,
]),
'#default_value' => !empty($cached_values['layout_change']['new_layout']) ? $cached_values['layout_change']['new_layout'] : '',
'#disabled' => TRUE,
'#access' => !empty($cached_values['layout_change']),
];
$form['layout_settings_wrapper'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Layout settings'),
'#tree' => TRUE,
];
$layout_settings = !empty($cached_values['layout_change']['layout_settings']) ? $cached_values['layout_change']['layout_settings'] : [];
if (!$layout_settings && $variant_plugin
->getLayout() instanceof ConfigurablePluginInterface) {
$layout_settings = $variant_plugin
->getLayout()
->getConfiguration();
}
$layout_id = !empty($cached_values['layout_change']['new_layout']) ? $cached_values['layout_change']['new_layout'] : $variant_plugin
->getConfiguration()['layout'];
$layout = Layout::layoutPluginManager()
->createInstance($layout_id, $layout_settings);
$form['layout_settings_wrapper']['layout_settings'] = $layout
->buildConfigurationForm([], $form_state);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$wizard = $form_state
->getFormObject();
$next_params = $wizard
->getNextParameters($cached_values);
$plugin = $cached_values['plugin'];
$layout_id = !empty($cached_values['layout_change']['new_layout']) ? $cached_values['layout_change']['new_layout'] : $plugin
->getConfiguration()['layout'];
$layout = Layout::layoutPluginManager()
->createInstance($layout_id, []);
if ($layout instanceof PluginFormInterface) {
$sub_form_state = new FormState();
$plugin_values = $form_state
->getValue([
'layout_settings_wrapper',
'layout_settings',
]);
if ($plugin_values) {
$sub_form_state
->setValues($plugin_values);
$layout
->submitConfigurationForm($form, $sub_form_state);
if ($layout instanceof ConfigurablePluginInterface) {
$cached_values = $this
->setCachedValues($next_params['step'], $plugin, $layout, $cached_values, $layout
->getConfiguration());
}
}
else {
$cached_values = $this
->setCachedValues($next_params['step'], $plugin, $layout, $cached_values, []);
}
}
else {
$cached_values = $this
->setCachedValues($next_params['step'], $plugin, $layout, $cached_values, []);
}
$form_state
->setTemporaryValue('wizard', $cached_values);
}
protected function setCachedValues($next_step, PanelsDisplayVariant $plugin, LayoutInterface $layout, $cached_values, $configuration) {
if (substr($next_step, 0 - 7) == 'regions') {
$cached_values['layout_change']['layout_settings'] = $configuration;
}
else {
$plugin
->setLayout($layout, $configuration);
$cached_values['plugin'] = $plugin;
unset($cached_values['layout_change']);
}
return $cached_values;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$plugin = $cached_values['plugin'];
$layout_id = !empty($cached_values['layout_change']['new_layout']) ? $cached_values['layout_change']['new_layout'] : $plugin
->getConfiguration()['layout'];
$layout = Layout::layoutPluginManager()
->createInstance($layout_id, []);
if ($layout instanceof PluginFormInterface) {
$sub_form_state = new FormState();
$plugin_values = $form_state
->getValue([
'layout_settings_wrapper',
'layout_settings',
]);
if ($plugin_values) {
$sub_form_state
->setValues($plugin_values);
$layout
->validateConfigurationForm($form, $sub_form_state);
}
}
}
}