PageVariantAddWizard.php in Page Manager 8.4
File
page_manager_ui/src/Wizard/PageVariantAddWizard.php
View source
<?php
namespace Drupal\page_manager_ui\Wizard;
use Drupal\Core\Display\ContextAwareVariantInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\ctools\Plugin\PluginWizardInterface;
use Drupal\ctools\Wizard\EntityFormWizardBase;
use Drupal\page_manager_ui\Access\PageManagerPluginAccess;
use Drupal\page_manager_ui\Form\PageVariantAddForm;
use Drupal\page_manager_ui\Form\PageVariantConfigureForm;
use Drupal\page_manager_ui\Form\AddVariantContextsForm;
use Drupal\page_manager_ui\Form\AddVariantSelectionForm;
class PageVariantAddWizard extends EntityFormWizardBase {
public function getEntityType() {
return 'page_variant';
}
public function exists() {
return '\\Drupal\\page_manager\\Entity\\PageVariant::load';
}
public function getWizardLabel() {
return $this
->t('Page Variant');
}
public function getMachineLabel() {
return $this
->t('Label');
}
public function getRouteName() {
return 'entity.page_variant.add_step_form';
}
public function initValues() {
$cached_values = parent::initValues();
$cached_values['access'] = new PageManagerPluginAccess();
return $cached_values;
}
public function getOperations($cached_values) {
$operations = [];
$operations['type'] = [
'title' => $this
->t('Page variant type'),
'form' => PageVariantAddForm::class,
];
$operations['contexts'] = [
'title' => $this
->t('Contexts'),
'form' => AddVariantContextsForm::class,
];
$operations['selection'] = [
'title' => $this
->t('Selection criteria'),
'form' => AddVariantSelectionForm::class,
];
$operations['configure'] = [
'title' => $this
->t('Configure variant'),
'form' => PageVariantConfigureForm::class,
];
$optional_steps = [
'selection',
'contexts',
];
foreach ($optional_steps as $step_name) {
if (isset($cached_values['wizard_options']) && empty($cached_values['wizard_options'][$step_name])) {
unset($operations[$step_name]);
}
}
if (!empty($cached_values['page_variant']) && !empty($cached_values['variant_plugin_id'])) {
$page_variant = $cached_values['page_variant'];
$variant_plugin = $page_variant
->getVariantPlugin();
if ($variant_plugin instanceof PluginWizardInterface) {
if ($variant_plugin instanceof ContextAwareVariantInterface) {
$variant_plugin
->setContexts($page_variant
->getContexts());
}
$cached_values['plugin'] = $variant_plugin;
foreach ($variant_plugin
->getWizardOperations($cached_values) as $name => $operation) {
$operation['values']['plugin'] = $variant_plugin;
$operations[$name] = $operation;
}
}
}
return $operations;
}
protected function customizeForm(array $form, FormStateInterface $form_state) {
$form = parent::customizeForm($form, $form_state);
if ($this->step == 'type' && isset($form['name']['id'])) {
unset($form['name']['id']);
}
return $form;
}
public function buildForm(array $form, FormStateInterface $form_state, $page = NULL) {
$form = parent::buildForm($form, $form_state);
if (!isset($cached_values['page']) || !$cached_values['page']
->id()) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$page_tempstore = $this->tempstore
->get('page_manager.page')
->get($page);
$cached_values['page'] = $page_tempstore['page'];
$form_state
->setTemporaryValue('wizard', $cached_values);
}
if ($this->step == 'configure') {
$form['page_variant_label']['#type'] = 'value';
unset($form['delete']);
}
return $form;
}
public function getNextParameters($cached_values) {
$parameters = parent::getNextParameters($cached_values);
$parameters['page'] = $cached_values['page']
->id();
return $parameters;
}
public function getPreviousParameters($cached_values) {
$parameters = parent::getPreviousParameters($cached_values);
$parameters['page'] = $cached_values['page']
->id();
return $parameters;
}
public function finish(array &$form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$page_tempstore = $this->tempstore
->get('page_manager.page')
->get($cached_values['page']
->id());
$page_tempstore['page']
->addVariant($cached_values['page_variant']);
$this->tempstore
->get('page_manager.page')
->set($cached_values['page']
->id(), $page_tempstore);
$variant_plugin = $cached_values['page_variant']
->getVariantPlugin();
$this
->messenger()
->addMessage($this
->t('The %label @entity_type has been added to the page, but has not been saved. Please save the page to store changes.', [
'%label' => $cached_values['page_variant']
->label(),
'@entity_type' => $variant_plugin
->adminLabel(),
]));
$form_state
->setRedirectUrl(new Url('entity.page.edit_form', [
'machine_name' => $cached_values['page']
->id(),
'step' => 'general',
]));
}
}