View source
<?php
namespace Drupal\panelizer\Wizard;
use Drupal\Core\Form\FormStateInterface;
use Drupal\ctools\Wizard\FormWizardBase;
use Drupal\panelizer\Access\PanelizerUIAccess;
use Drupal\panelizer\Form\PanelizerWizardContentForm;
use Drupal\panelizer\Form\PanelizerWizardContextForm;
use Drupal\panelizer\Form\PanelizerWizardGeneralForm;
abstract class PanelizerWizardBase extends FormWizardBase {
protected function customizeForm(array $form, FormStateInterface $form_state) {
$form = parent::customizeForm($form, $form_state);
$cached_values = $form_state
->getTemporaryValue('wizard');
$operation = $this
->getOperation($cached_values);
$operations = $this
->getOperations($cached_values);
$default_operation = reset($operations);
$storage = $form_state
->getStorage();
$prefix = isset($storage['machine_name_prefix']) ? $storage['machine_name_prefix'] : $form_state
->getTemporaryValue('wizard')['id'];
if ($operation['form'] == $default_operation['form']) {
$form['name'] = [
'#type' => 'fieldset',
'#attributes' => [
'class' => [
'fieldset-no-legend',
],
],
'#title' => $this
->getWizardLabel(),
];
$form['name']['label'] = [
'#type' => 'textfield',
'#title' => $this
->getMachineLabel(),
'#required' => TRUE,
'#size' => 32,
'#default_value' => !empty($cached_values['label']) ? $cached_values['label'] : '',
'#maxlength' => 255,
'#disabled' => !empty($cached_values['label']),
];
$form['name']['id'] = [
'#type' => 'machine_name',
'#maxlength' => 128,
'#machine_name' => [
'source' => [
'name',
'label',
],
'exists' => $this
->exists(),
'prefix' => $prefix,
],
'#description' => $this
->t('A unique machine-readable name for this display. It must only contain lowercase letters, numbers, and underscores.'),
'#default_value' => !empty($cached_values['id']) ? $cached_values['id'] : '',
'#disabled' => !empty($cached_values['id']),
];
}
return $form;
}
public function getWizardLabel() {
return $this
->t('Wizard Information');
}
public function getMachineLabel() {
return $this
->t('Wizard name');
}
public function exists() {
return '\\Drupal\\panelizer\\Form\\PanelizerWizardGeneralForm::validateMachineName';
}
public function getOperations($cached_values) {
$operations = [
'general' => [
'form' => PanelizerWizardGeneralForm::class,
'title' => $this
->t('General settings'),
],
'contexts' => [
'form' => PanelizerWizardContextForm::class,
'title' => $this
->t('Contexts'),
],
];
foreach ($cached_values['plugin']
->getWizardOperations($cached_values) as $name => $operation) {
$operations[$name] = $operation;
}
if (isset($operations['content'])) {
}
return $operations;
}
public function initValues() {
$cached_values = parent::initValues();
$cached_values['access'] = new PanelizerUIAccess();
if (empty($cached_values['plugin'])) {
$plugin = \Drupal::service('plugin.manager.display_variant')
->createInstance('panels_variant');
$plugin
->setPattern('panelizer');
$plugin
->setBuilder('ipe');
$plugin
->setStorage('panelizer_default', 'TEMPORARY_STORAGE_ID');
$cached_values['plugin'] = $plugin;
}
if (empty($cached_values['contexts'])) {
$cached_values['contexts'] = [];
}
return $cached_values;
}
public function finish(array &$form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$panelizer = \Drupal::service('panelizer');
$invalidator = \Drupal::service('cache_tags.invalidator');
list($entity_type, $bundle, $view_mode, $display_id) = explode('__', $cached_values['id']);
$panelizer
->setDefaultPanelsDisplay($display_id, $entity_type, $bundle, $view_mode, $cached_values['plugin']);
$panelizer
->setDisplayStaticContexts($display_id, $entity_type, $bundle, $view_mode, $cached_values['contexts']);
parent::finish($form, $form_state);
$form_state
->setRedirect('panelizer.wizard.edit', [
'machine_name' => $cached_values['id'],
]);
$invalidator
->invalidateTags([
"panelizer_default:{$entity_type}:{$bundle}:{$view_mode}:{$display_id}",
]);
}
protected function getContextMapper() {
return \Drupal::service('ctools.context_mapper');
}
protected function getContexts($cached_values) {
return $this
->getContextMapper()
->getContextValues($cached_values['contexts']);
}
}