View source
<?php
namespace Drupal\ctools\Wizard;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\ctools\Ajax\OpenModalWizardCommand;
use Drupal\ctools\Event\WizardEvent;
use Drupal\Core\TempStore\SharedTempStoreFactory;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
abstract class FormWizardBase extends FormBase implements FormWizardInterface {
protected $tempstore;
protected $builder;
protected $classResolver;
protected $dispatcher;
protected $tempstore_id;
protected $machine_name;
protected $step;
public function __construct(SharedTempStoreFactory $tempstore, FormBuilderInterface $builder, ClassResolverInterface $class_resolver, EventDispatcherInterface $event_dispatcher, RouteMatchInterface $route_match, $tempstore_id, $machine_name = NULL, $step = NULL) {
$this->tempstore = $tempstore;
$this->builder = $builder;
$this->classResolver = $class_resolver;
$this->dispatcher = $event_dispatcher;
$this->routeMatch = $route_match;
$this->tempstore_id = $tempstore_id;
$this->machine_name = $machine_name;
$this->step = $step;
}
public static function getParameters() {
return [
'tempstore' => \Drupal::service('tempstore.shared'),
'builder' => \Drupal::service('form_builder'),
'class_resolver' => \Drupal::service('class_resolver'),
'event_dispatcher' => \Drupal::service('event_dispatcher'),
];
}
public function initValues() {
$values = [];
$event = new WizardEvent($this, $values);
$this->dispatcher
->dispatch(FormWizardInterface::LOAD_VALUES, $event);
return $event
->getValues();
}
public function getTempstoreId() {
return $this->tempstore_id;
}
public function getTempstore() {
$tempstore = $this->tempstore
->get($this
->getTempstoreId());
return $tempstore;
}
public function getMachineName() {
return $this->machine_name;
}
public function getStep($cached_values) {
if (!$this->step) {
$operations = $this
->getOperations($cached_values);
$steps = array_keys($operations);
$this->step = reset($steps);
}
return $this->step;
}
public function getOperation($cached_values) {
$operations = $this
->getOperations($cached_values);
$step = $this
->getStep($cached_values);
if (!empty($operations[$step])) {
return $operations[$step];
}
throw new NotFoundHttpException();
}
public function getNextOp() {
return $this
->t('Next');
}
public function getNextParameters($cached_values) {
$operations = $this
->getOperations($cached_values);
$steps = array_keys($operations);
$after = array_slice($operations, array_search($this
->getStep($cached_values), $steps) + 1);
$after_keys = array_keys($after);
$step = reset($after_keys);
if (!$step) {
$keys = array_keys($operations);
$step = end($keys);
}
return [
'machine_name' => $this
->getMachineName(),
'step' => $step,
'js' => 'nojs',
];
}
public function getPreviousParameters($cached_values) {
$operations = $this
->getOperations($cached_values);
$step = $this
->getStep($cached_values);
$steps = array_keys($operations);
$before = array_slice($operations, 0, array_search($step, $steps));
$before = array_keys($before);
$before_steps = array_reverse($before);
$step = reset($before_steps);
return [
'machine_name' => $this
->getMachineName(),
'step' => $step,
'js' => 'nojs',
];
}
public function getFormId() {
if (!$this
->getMachineName() || !$this
->getTempstore()
->get($this
->getMachineName())) {
$cached_values = $this
->initValues();
}
else {
$cached_values = $this
->getTempstore()
->get($this
->getMachineName());
}
$operation = $this
->getOperation($cached_values);
$operation = $this->classResolver
->getInstanceFromDefinition($operation['form']);
return $operation
->getFormId();
}
public function buildForm(array $form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$operation = $this
->getOperation($cached_values);
$form = $this
->customizeForm($form, $form_state);
$formClass = $this->classResolver
->getInstanceFromDefinition($operation['form']);
if (!empty($operation['values'])) {
$cached_values = array_merge($cached_values, $operation['values']);
$form_state
->setTemporaryValue('wizard', $cached_values);
}
$form = $formClass
->buildForm($form, $form_state);
if (isset($operation['title'])) {
$form['#title'] = $operation['title'];
}
$form['actions'] = $this
->actions($formClass, $form_state);
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ((string) $form_state
->getValue('op') == (string) $this
->getNextOp()) {
$cached_values = $form_state
->getTemporaryValue('wizard');
if ($form_state
->hasValue('label')) {
$cached_values['label'] = $form_state
->getValue('label');
}
if ($form_state
->hasValue('id')) {
$cached_values['id'] = $form_state
->getValue('id');
}
if (is_null($this->machine_name) && !empty($cached_values['id'])) {
$this->machine_name = $cached_values['id'];
}
$this
->getTempstore()
->set($this
->getMachineName(), $cached_values);
if (!$form_state
->get('ajax')) {
$form_state
->setRedirect($this
->getRouteName(), $this
->getNextParameters($cached_values));
}
}
}
public function populateCachedValues(array &$form, FormStateInterface $form_state) {
$cached_values = $this
->getTempstore()
->get($this
->getMachineName());
if (!$cached_values) {
$cached_values = $form_state
->getTemporaryValue('wizard');
if (!$cached_values) {
$cached_values = $this
->initValues();
$form_state
->setTemporaryValue('wizard', $cached_values);
}
}
}
public function previous(array &$form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$form_state
->setRedirect($this
->getRouteName(), $this
->getPreviousParameters($cached_values));
}
public function finish(array &$form, FormStateInterface $form_state) {
$this
->getTempstore()
->delete($this
->getMachineName());
}
protected function customizeForm(array $form, FormStateInterface $form_state) {
$prefix = [
'#theme' => [
'ctools_wizard_trail',
],
'#wizard' => $this,
'#cached_values' => $form_state
->getTemporaryValue('wizard'),
];
$form['#prefix'] = \Drupal::service('renderer')
->render($prefix);
return $form;
}
protected function actions(FormInterface $form_object, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$operations = $this
->getOperations($cached_values);
$step = $this
->getStep($cached_values);
$operation = $operations[$step];
$steps = array_keys($operations);
$before = array_slice($operations, 0, array_search($step, $steps));
$after = array_slice($operations, array_search($step, $steps) + 1);
$actions = [
'submit' => [
'#type' => 'submit',
'#value' => $this
->t('Next'),
'#button_type' => 'primary',
'#validate' => [
'::populateCachedValues',
[
$form_object,
'validateForm',
],
],
'#submit' => [
[
$form_object,
'submitForm',
],
],
],
];
if (isset($operation['validate'])) {
$actions['submit']['#validate'] = array_merge($actions['submit']['#validate'], $operation['validate']);
}
$actions['submit']['#validate'][] = '::validateForm';
if (isset($operation['submit'])) {
$actions['submit']['#submit'] = array_merge($actions['submit']['#submit'], $operation['submit']);
}
$actions['submit']['#submit'][] = '::submitForm';
if ($form_state
->get('ajax')) {
$parameters = $this
->getNextParameters($cached_values);
$parameters['step'] = $this
->getStep($cached_values);
$actions['submit']['#ajax'] = [
'callback' => '::ajaxSubmit',
'url' => Url::fromRoute($this
->getRouteName(), $parameters),
'options' => [
'query' => $this
->getRequest()->query
->all() + [
FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
],
],
];
}
if ($before) {
$actions['previous'] = [
'#type' => 'submit',
'#value' => $this
->t('Previous'),
'#validate' => [
[
$this,
'populateCachedValues',
],
],
'#submit' => [
[
$this,
'previous',
],
],
'#limit_validation_errors' => [],
'#weight' => -10,
];
if ($form_state
->get('ajax')) {
$parameters = $this
->getPreviousParameters($cached_values);
$parameters['step'] = $this
->getStep($cached_values);
$actions['previous']['#ajax'] = [
'callback' => '::ajaxPrevious',
'url' => Url::fromRoute($this
->getRouteName(), $parameters),
'options' => [
'query' => $this
->getRequest()->query
->all() + [
FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
],
],
];
}
}
if (!$after) {
$actions['submit']['#value'] = $this
->t('Finish');
$actions['submit']['#submit'][] = [
$this,
'finish',
];
if ($form_state
->get('ajax')) {
$actions['submit']['#ajax']['callback'] = [
$this,
'ajaxFinish',
];
}
}
return $actions;
}
public function ajaxSubmit(array $form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$response = new AjaxResponse();
$parameters = $this
->getNextParameters($cached_values);
$response
->addCommand(new OpenModalWizardCommand($this, $this
->getTempstoreId(), $parameters));
return $response;
}
public function ajaxPrevious(array $form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$response = new AjaxResponse();
$parameters = $this
->getPreviousParameters($cached_values);
$response
->addCommand(new OpenModalWizardCommand($this, $this
->getTempstoreId(), $parameters));
return $response;
}
public function ajaxFinish(array $form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$response
->addCommand(new CloseModalDialogCommand());
return $response;
}
public function getRouteName() {
return $this->routeMatch
->getRouteName();
}
}