diff --git a/core/lib/Drupal/Core/Wizard/WizardFormController.php b/core/lib/Drupal/Core/Wizard/WizardFormController.php index 65c8f54..060cbe1 100644 --- a/core/lib/Drupal/Core/Wizard/WizardFormController.php +++ b/core/lib/Drupal/Core/Wizard/WizardFormController.php @@ -15,11 +15,38 @@ */ class WizardFormController extends EntityFormController { - protected $entity_info; + /** + * The entity type definition. + * + * @var array + */ + protected $entityInfo; + + /** + * The entity type definition key that will specify the relevant steps. + * + * Subclasses may change this to support multiple entity form wizards. + * + * @var string + */ + protected $stepKey = 'steps'; + + /** + * The entity type definition key that will specify the relevant destination. + * + * Subclasses may change this to support multiple entity form wizards + * destinations. + * + * @var string + */ + protected $destinationKey = 'destination'; + /** + * Overrides Drupal\Core\Entity\EntityFormController::init(). + */ protected function init(array &$form_state, EntityInterface $entity) { parent::init($form_state, $entity); - $this->entity_info = $entity->entityInfo(); + $this->entityInfo = $entity->entityInfo(); } /** @@ -32,13 +59,12 @@ protected function init(array &$form_state, EntityInterface $entity) { protected function actions(array $form, array &$form_state) { $actions = parent::actions($form, $form_state); $entity = $this->buildEntity($form, $form_state += array('values' => array())); - $this->entity_info = $entity->entityInfo(); // Get the steps of the wizard from the entity definition. - $steps = array_keys($this->entity_info['steps']); + $steps = array_keys($this->entityInfo[$this->stepKey]); // Slice to find the operations that occur before the current operation. - $before = array_slice($this->entity_info['steps'], 0, array_search($this->getOperation(), $steps)); + $before = array_slice($this->entityInfo[$this->stepKey], 0, array_search($this->getOperation(), $steps)); // Slice to find the operations that occur after the current operation. - $after = array_slice($this->entity_info['steps'], array_search($this->getOperation(), $steps) + 1); + $after = array_slice($this->entityInfo[$this->stepKey], array_search($this->getOperation(), $steps) + 1); // If there are steps after this one, label the button "Next" otherwise // label it "Finish". @@ -77,13 +103,13 @@ public function submit(array $form, array &$form_state) { $entity = $this->buildEntity($form, $form_state); drupal_container()->get('user.tempstore')->get($entity->entityType())->set($entity->id(), $entity); // Get the steps by key. - $steps = array_keys($this->entity_info['steps']); + $steps = array_keys($this->entityInfo[$this->stepKey]); // Get the steps after the current step. - $after = array_slice($this->entity_info['steps'], array_search($this->getOperation(), $steps) + 1); + $after = array_slice($this->entityInfo[$this->stepKey], array_search($this->getOperation(), $steps) + 1); // Get the steps after the current step by key. $after = array_keys($after); - $form_state['redirect'] = 'admin/config/system/conditions/' . $entity->id() . '/' . $after[0]; + $form_state['redirect'] = $this->redirect($entity, $after[0]); } } @@ -100,21 +126,25 @@ public function submit(array $form, array &$form_state) { public function previous(array $form, array &$form_state) { $entity = $this->buildEntity($form, $form_state); // Get the steps by key. - $steps = array_keys($this->entity_info['steps']); + $steps = array_keys($this->entityInfo[$this->stepKey]); // Get the steps before the current step. - $before = array_slice($this->entity_info['steps'], 0, array_search($this->getOperation(), $steps)); + $before = array_slice($this->entityInfo[$this->stepKey], 0, array_search($this->getOperation(), $steps)); // Get the steps before the current step by key. $before = array_keys($before); // Reverse the steps for easy access to the next step. $before = array_reverse($before); - $form_state['redirect'] = 'admin/config/system/conditions/' . $entity->id() . '/' . $before[0]; + $form_state['redirect'] = $this->redirect($entity, $before[0]); } /** * Form submission handler for the 'previous' action. * * Retrieves the entity, saves it and deletes the values in the tempstore. + * This is handled as a separate method from submit() in order to keep the + * concerns of next() separate from finish(). There is no next() method + * because submit() will be fired in all forward moving steps. Submit just + * checks the operator to make sure that it is not firing on finish(). * * @param array $form * An associative array containing the structure of the form. @@ -125,6 +155,25 @@ public function finish(array $form, array &$form_state) { $entity = parent::submit($form, $form_state); $entity->save(); drupal_container()->get('user.tempstore')->get($entity->entityType())->delete($entity->id()); - $form_state['redirect'] = 'admin/config/system/conditions'; + $form_state['redirect'] = $this->redirect($entity); + } + + /** + * A simple method for managing the redirect url of each step. + * + * @param EntityInterface $entity + * The entity being saved or updated. + * @param string $step + * A simple string representation of the current step and corresponding + * form controller. + * + * @return string + * A uri for to be placed in $form_state['redirect']. + */ + public function redirect(EntityInterface $entity, $step = NULL) { + if (!empty($step)) { + return $this->entityInfo[$this->destinationKey] . '/' . $entity->id() . '/' . $step; + } + return $this->entityInfo[$this->destinationKey]; } }