View source
<?php
namespace Drupal\page_manager_ui\Form;
use Drupal\Core\Display\VariantManager;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\page_manager\Entity\PageVariant;
use Drupal\page_manager\PageInterface;
use Drupal\Core\TempStore\SharedTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PageVariantAddForm extends FormBase {
protected $variantManager;
protected $tempstore;
public function __construct(VariantManager $variant_manager, SharedTempStoreFactory $tempstore) {
$this->variantManager = $variant_manager;
$this->tempstore = $tempstore;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.display_variant'), $container
->get('tempstore.shared'));
}
public function getFormId() {
return 'page_manager_add_variant_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $machine_name = '') {
$cached_values = $form_state
->getTemporaryValue('wizard');
$form['name']['label']['#required'] = FALSE;
$form['name']['label']['#disabled'] = FALSE;
$variant_plugin_options = [];
foreach ($this->variantManager
->getDefinitions() as $plugin_id => $definition) {
if (in_array($plugin_id, [
'simple_page',
'block_page',
])) {
continue;
}
$variant_plugin_options[$plugin_id] = $definition['admin_label'];
}
$form['variant_plugin_id'] = [
'#title' => $this
->t('Type'),
'#type' => 'select',
'#options' => $variant_plugin_options,
'#default_value' => !empty($cached_values['variant_plugin_id']) ? $cached_values['variant_plugin_id'] : '',
];
$form['wizard_options'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Optional features'),
'#description' => $this
->t('Check any optional features you need to be presented with forms for configuring them. If you do not check them here you will still be able to utilize these features once the new variant is created.'),
'#options' => [
'selection' => $this
->t('Selection criteria'),
'contexts' => $this
->t('Contexts'),
],
'#default_value' => !empty($cached_values['wizard_options']) ? $cached_values['wizard_options'] : [],
];
return $form;
}
protected function variantExists(PageInterface $page, $variant_id) {
return isset($page
->getVariants()[$variant_id]) || PageVariant::load($variant_id);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if (!$form_state
->hasValue('label') || !$form_state
->getValue('label')) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$page_variant = $cached_values['page_variant'];
$plugin = $page_variant
->getVariantPlugin();
$admin_label = $plugin
->getPluginDefinition()['admin_label'];
$form_state
->setValue('label', (string) $admin_label);
}
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$page = $cached_values['page'];
$variant_plugin_id = $cached_values['variant_plugin_id'] = $form_state
->getValue('variant_plugin_id');
$page_variant = $cached_values['page_variant'];
$page_variant
->setVariantPluginId($variant_plugin_id);
$page_variant
->set('label', $form_state
->getValue('label'));
$page_variant
->set('page', $page
->id());
$variant_id_base = "{$page->id()}-{$variant_plugin_id}";
$key = 0;
while ($this
->variantExists($page, "{$variant_id_base}-{$key}")) {
$key++;
}
$cached_values['id'] = "{$variant_id_base}-{$key}";
$page_variant
->set('id', $cached_values['id']);
$cached_values['wizard_options'] = $form_state
->getValue('wizard_options');
$form_state
->setTemporaryValue('wizard', $cached_values);
}
}