Loading modules/checkout/src/CheckoutOrderManager.php +32 −3 Changes for modules/checkout/src/CheckoutOrderManager.php: 32 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -31,12 +31,41 @@ class CheckoutOrderManager implements CheckoutOrderManagerInterface { * {@inheritdoc} */ public function getCheckoutFlow(OrderInterface $order) { if ($order->checkout_flow->isEmpty()) { $order->checkout_flow = $this->chainCheckoutFlowResolver->resolve($order); if ($order->get('checkout_flow')->isEmpty()) { $checkout_flow = $this->chainCheckoutFlowResolver->resolve($order); $order->set('checkout_flow', $checkout_flow); $order->save(); } return $order->checkout_flow->entity; return $order->get('checkout_flow')->entity; } /** * {@inheritdoc} */ public function getCheckoutStepId(OrderInterface $order, $requested_step_id = NULL) { // Customers can't edit orders that have already been placed. if ($order->getState()->value != 'draft') { return 'complete'; } $checkout_flow = $this->getCheckoutFlow($order); $available_step_ids = array_keys($checkout_flow->getPlugin()->getVisibleSteps()); $selected_step_id = $order->get('checkout_step')->value; $selected_step_id = $selected_step_id ?: reset($available_step_ids); if (empty($requested_step_id) || $requested_step_id == $selected_step_id) { return $selected_step_id; } if (in_array($requested_step_id, $available_step_ids)) { // Allow access to a previously completed step. $requested_step_index = array_search($requested_step_id, $available_step_ids); $selected_step_index = array_search($selected_step_id, $available_step_ids); if ($requested_step_index <= $selected_step_index) { $selected_step_id = $requested_step_id; } } return $selected_step_id; } } modules/checkout/src/CheckoutOrderManagerInterface.php +17 Changes for modules/checkout/src/CheckoutOrderManagerInterface.php: 17 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -20,4 +20,21 @@ interface CheckoutOrderManagerInterface { */ public function getCheckoutFlow(OrderInterface $order); /** * Gets the order's checkout step ID. * * Ensures that the user is allowed to access the requested step ID, * when given. In case the requested step ID is empty, invalid, or * not allowed, a different step ID will be returned. * * @param \Drupal\commerce_order\Entity\OrderInterface $order * The order. * @param string $requested_step_id * (Optional) The requested step ID. * * @return string * The checkout step ID. */ public function getCheckoutStepId(OrderInterface $order, $requested_step_id = NULL); } modules/checkout/src/Controller/CheckoutController.php +15 −4 Changes for modules/checkout/src/Controller/CheckoutController.php: 15 added lines, 4 removed lines. Original line number Diff line number Diff line Loading @@ -9,10 +9,11 @@ use Drupal\Core\Access\AccessResult; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\DependencyInjection\DependencySerializationTrait; use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Form\FormState; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; /** * Provides the checkout form page. Loading Loading @@ -75,15 +76,22 @@ class CheckoutController implements ContainerInjectionInterface { * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match. * * @return array * @return array|\Symfony\Component\HttpFoundation\RedirectResponse * The render form. */ public function formPage(RouteMatchInterface $route_match) { /** @var \Drupal\commerce_order\Entity\OrderInterface $order */ $order = $route_match->getParameter('commerce_order'); $requested_step_id = $route_match->getParameter('step'); $step_id = $this->checkoutOrderManager->getCheckoutStepId($order, $requested_step_id); if ($requested_step_id != $step_id) { $url = Url::fromRoute('commerce_checkout.form', ['commerce_order' => $order->id(), 'step' => $step_id]); return new RedirectResponse($url->toString()); } $checkout_flow = $this->checkoutOrderManager->getCheckoutFlow($order); $form_state = new FormState(); return $this->formBuilder->buildForm($checkout_flow->getPlugin(), $form_state); $checkout_flow_plugin = $checkout_flow->getPlugin(); return $this->formBuilder->getForm($checkout_flow_plugin, $step_id); } /** Loading @@ -100,6 +108,9 @@ class CheckoutController implements ContainerInjectionInterface { public function checkAccess(RouteMatchInterface $route_match, AccountInterface $account) { /** @var \Drupal\commerce_order\Entity\OrderInterface $order */ $order = $route_match->getParameter('commerce_order'); if ($order->getState()->value == 'canceled') { return AccessResult::forbidden()->addCacheableDependency($order); } // The user can checkout only their own non-empty orders. if ($account->isAuthenticated()) { Loading modules/checkout/src/Plugin/Block/CheckoutProgressBlock.php +1 −1 Changes for modules/checkout/src/Plugin/Block/CheckoutProgressBlock.php: 1 added line, 1 removed line. Original line number Diff line number Diff line Loading @@ -89,7 +89,7 @@ class CheckoutProgressBlock extends BlockBase implements ContainerFactoryPluginI // Prepare the steps as expected by the template. $steps = []; $visible_steps = $checkout_flow_plugin->getVisibleSteps(); $current_step_id = $checkout_flow_plugin->getStepId(); $current_step_id = $this->checkoutOrderManager->getCheckoutStepId($order); $current_step_index = array_search($current_step_id, array_keys($visible_steps)); $index = 0; foreach ($visible_steps as $step_id => $step_definition) { Loading modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowBase.php +18 −55 Changes for modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowBase.php: 18 added lines, 55 removed lines. Original line number Diff line number Diff line Loading @@ -43,13 +43,6 @@ abstract class CheckoutFlowBase extends PluginBase implements CheckoutFlowInterf */ protected $order; /** * The current step ID. * * @var string */ protected $stepId; /** * Constructs a new CheckoutFlowBase object. * Loading @@ -73,12 +66,6 @@ abstract class CheckoutFlowBase extends PluginBase implements CheckoutFlowInterf $this->entityTypeManager = $entity_type_manager; $this->eventDispatcher = $event_dispatcher; $this->order = $route_match->getParameter('commerce_order'); // The order is empty when the checkout flow is initialized outside of the // checkout form (usually in the checkout flow admin UI). There's no need // to determine the current step ID in that case, it won't be used. if ($this->order) { $this->stepId = $this->processStepId($route_match->getParameter('step')); } } /** Loading @@ -95,29 +82,6 @@ abstract class CheckoutFlowBase extends PluginBase implements CheckoutFlowInterf ); } /** * Processes the requested step ID. * * @param string $requested_step_id * The step ID. * * @return string * The processed step ID. */ protected function processStepId($requested_step_id) { $step_ids = array_keys($this->getVisibleSteps()); $step_id = $requested_step_id; if (empty($step_id) || !in_array($step_id, $step_ids)) { // Take the step ID from the order, or default to the first one. $step_id = $this->order->checkout_step->value; if (empty($step_id)) { $step_id = reset($step_ids); } } return $step_id; } /** * {@inheritdoc} */ Loading @@ -128,25 +92,18 @@ abstract class CheckoutFlowBase extends PluginBase implements CheckoutFlowInterf /** * {@inheritdoc} */ public function getStepId() { return $this->stepId; } /** * {@inheritdoc} */ public function getPreviousStepId() { public function getPreviousStepId($step_id) { $step_ids = array_keys($this->getVisibleSteps()); $current_index = array_search($this->stepId, $step_ids); $current_index = array_search($step_id, $step_ids); return isset($step_ids[$current_index - 1]) ? $step_ids[$current_index - 1] : NULL; } /** * {@inheritdoc} */ public function getNextStepId() { public function getNextStepId($step_id) { $step_ids = array_keys($this->getVisibleSteps()); $current_index = array_search($this->stepId, $step_ids); $current_index = array_search($step_id, $step_ids); return isset($step_ids[$current_index + 1]) ? $step_ids[$current_index + 1] : NULL; } Loading @@ -154,7 +111,7 @@ abstract class CheckoutFlowBase extends PluginBase implements CheckoutFlowInterf * {@inheritdoc} */ public function redirectToStep($step_id) { $this->order->checkout_step = $step_id; $this->order->set('checkout_step', $step_id); if ($step_id == 'complete') { $transition = $this->order->getState()->getWorkflow()->getTransition('place'); $this->order->getState()->applyTransition($transition); Loading Loading @@ -306,13 +263,19 @@ abstract class CheckoutFlowBase extends PluginBase implements CheckoutFlowInterf /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { public function buildForm(array $form, FormStateInterface $form_state, $step_id = NULL) { // The $step_id argument is optional only because PHP disallows adding required // arguments to an existing interface's method. if (empty($step_id)) { throw new \InvalidArgumentException('The $step_id cannot be empty.'); } $steps = $this->getVisibleSteps(); $form['#tree'] = TRUE; $form['#title'] = $steps[$this->stepId]['label']; $form['#step_id'] = $step_id; $form['#title'] = $steps[$step_id]['label']; $form['#theme'] = ['commerce_checkout_form']; $form['#attached']['library'][] = 'commerce_checkout/form'; if ($steps[$this->stepId]['has_order_summary']) { if ($steps[$step_id]['has_order_summary']) { if ($order_summary = $this->buildOrderSummary($form, $form_state)) { $form['order_summary'] = $order_summary; } Loading @@ -331,8 +294,8 @@ abstract class CheckoutFlowBase extends PluginBase implements CheckoutFlowInterf * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { if ($next_step_id = $this->getNextStepId()) { $this->order->checkout_step = $next_step_id; if ($next_step_id = $this->getNextStepId($form['#step_id'])) { $this->order->set('checkout_step', $next_step_id); $form_state->setRedirect('commerce_checkout.form', [ 'commerce_order' => $this->order->id(), 'step' => $next_step_id, Loading Loading @@ -361,8 +324,8 @@ abstract class CheckoutFlowBase extends PluginBase implements CheckoutFlowInterf */ protected function actions(array $form, FormStateInterface $form_state) { $steps = $this->getVisibleSteps(); $next_step_id = $this->getNextStepId(); $previous_step_id = $this->getPreviousStepId(); $next_step_id = $this->getNextStepId($form['#step_id']); $previous_step_id = $this->getPreviousStepId($form['#step_id']); $has_next_step = $next_step_id && isset($steps[$next_step_id]['next_label']); $has_previous_step = $previous_step_id && isset($steps[$previous_step_id]['previous_label']); Loading Loading
modules/checkout/src/CheckoutOrderManager.php +32 −3 Changes for modules/checkout/src/CheckoutOrderManager.php: 32 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -31,12 +31,41 @@ class CheckoutOrderManager implements CheckoutOrderManagerInterface { * {@inheritdoc} */ public function getCheckoutFlow(OrderInterface $order) { if ($order->checkout_flow->isEmpty()) { $order->checkout_flow = $this->chainCheckoutFlowResolver->resolve($order); if ($order->get('checkout_flow')->isEmpty()) { $checkout_flow = $this->chainCheckoutFlowResolver->resolve($order); $order->set('checkout_flow', $checkout_flow); $order->save(); } return $order->checkout_flow->entity; return $order->get('checkout_flow')->entity; } /** * {@inheritdoc} */ public function getCheckoutStepId(OrderInterface $order, $requested_step_id = NULL) { // Customers can't edit orders that have already been placed. if ($order->getState()->value != 'draft') { return 'complete'; } $checkout_flow = $this->getCheckoutFlow($order); $available_step_ids = array_keys($checkout_flow->getPlugin()->getVisibleSteps()); $selected_step_id = $order->get('checkout_step')->value; $selected_step_id = $selected_step_id ?: reset($available_step_ids); if (empty($requested_step_id) || $requested_step_id == $selected_step_id) { return $selected_step_id; } if (in_array($requested_step_id, $available_step_ids)) { // Allow access to a previously completed step. $requested_step_index = array_search($requested_step_id, $available_step_ids); $selected_step_index = array_search($selected_step_id, $available_step_ids); if ($requested_step_index <= $selected_step_index) { $selected_step_id = $requested_step_id; } } return $selected_step_id; } }
modules/checkout/src/CheckoutOrderManagerInterface.php +17 Changes for modules/checkout/src/CheckoutOrderManagerInterface.php: 17 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -20,4 +20,21 @@ interface CheckoutOrderManagerInterface { */ public function getCheckoutFlow(OrderInterface $order); /** * Gets the order's checkout step ID. * * Ensures that the user is allowed to access the requested step ID, * when given. In case the requested step ID is empty, invalid, or * not allowed, a different step ID will be returned. * * @param \Drupal\commerce_order\Entity\OrderInterface $order * The order. * @param string $requested_step_id * (Optional) The requested step ID. * * @return string * The checkout step ID. */ public function getCheckoutStepId(OrderInterface $order, $requested_step_id = NULL); }
modules/checkout/src/Controller/CheckoutController.php +15 −4 Changes for modules/checkout/src/Controller/CheckoutController.php: 15 added lines, 4 removed lines. Original line number Diff line number Diff line Loading @@ -9,10 +9,11 @@ use Drupal\Core\Access\AccessResult; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\DependencyInjection\DependencySerializationTrait; use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Form\FormState; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; /** * Provides the checkout form page. Loading Loading @@ -75,15 +76,22 @@ class CheckoutController implements ContainerInjectionInterface { * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match. * * @return array * @return array|\Symfony\Component\HttpFoundation\RedirectResponse * The render form. */ public function formPage(RouteMatchInterface $route_match) { /** @var \Drupal\commerce_order\Entity\OrderInterface $order */ $order = $route_match->getParameter('commerce_order'); $requested_step_id = $route_match->getParameter('step'); $step_id = $this->checkoutOrderManager->getCheckoutStepId($order, $requested_step_id); if ($requested_step_id != $step_id) { $url = Url::fromRoute('commerce_checkout.form', ['commerce_order' => $order->id(), 'step' => $step_id]); return new RedirectResponse($url->toString()); } $checkout_flow = $this->checkoutOrderManager->getCheckoutFlow($order); $form_state = new FormState(); return $this->formBuilder->buildForm($checkout_flow->getPlugin(), $form_state); $checkout_flow_plugin = $checkout_flow->getPlugin(); return $this->formBuilder->getForm($checkout_flow_plugin, $step_id); } /** Loading @@ -100,6 +108,9 @@ class CheckoutController implements ContainerInjectionInterface { public function checkAccess(RouteMatchInterface $route_match, AccountInterface $account) { /** @var \Drupal\commerce_order\Entity\OrderInterface $order */ $order = $route_match->getParameter('commerce_order'); if ($order->getState()->value == 'canceled') { return AccessResult::forbidden()->addCacheableDependency($order); } // The user can checkout only their own non-empty orders. if ($account->isAuthenticated()) { Loading
modules/checkout/src/Plugin/Block/CheckoutProgressBlock.php +1 −1 Changes for modules/checkout/src/Plugin/Block/CheckoutProgressBlock.php: 1 added line, 1 removed line. Original line number Diff line number Diff line Loading @@ -89,7 +89,7 @@ class CheckoutProgressBlock extends BlockBase implements ContainerFactoryPluginI // Prepare the steps as expected by the template. $steps = []; $visible_steps = $checkout_flow_plugin->getVisibleSteps(); $current_step_id = $checkout_flow_plugin->getStepId(); $current_step_id = $this->checkoutOrderManager->getCheckoutStepId($order); $current_step_index = array_search($current_step_id, array_keys($visible_steps)); $index = 0; foreach ($visible_steps as $step_id => $step_definition) { Loading
modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowBase.php +18 −55 Changes for modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowBase.php: 18 added lines, 55 removed lines. Original line number Diff line number Diff line Loading @@ -43,13 +43,6 @@ abstract class CheckoutFlowBase extends PluginBase implements CheckoutFlowInterf */ protected $order; /** * The current step ID. * * @var string */ protected $stepId; /** * Constructs a new CheckoutFlowBase object. * Loading @@ -73,12 +66,6 @@ abstract class CheckoutFlowBase extends PluginBase implements CheckoutFlowInterf $this->entityTypeManager = $entity_type_manager; $this->eventDispatcher = $event_dispatcher; $this->order = $route_match->getParameter('commerce_order'); // The order is empty when the checkout flow is initialized outside of the // checkout form (usually in the checkout flow admin UI). There's no need // to determine the current step ID in that case, it won't be used. if ($this->order) { $this->stepId = $this->processStepId($route_match->getParameter('step')); } } /** Loading @@ -95,29 +82,6 @@ abstract class CheckoutFlowBase extends PluginBase implements CheckoutFlowInterf ); } /** * Processes the requested step ID. * * @param string $requested_step_id * The step ID. * * @return string * The processed step ID. */ protected function processStepId($requested_step_id) { $step_ids = array_keys($this->getVisibleSteps()); $step_id = $requested_step_id; if (empty($step_id) || !in_array($step_id, $step_ids)) { // Take the step ID from the order, or default to the first one. $step_id = $this->order->checkout_step->value; if (empty($step_id)) { $step_id = reset($step_ids); } } return $step_id; } /** * {@inheritdoc} */ Loading @@ -128,25 +92,18 @@ abstract class CheckoutFlowBase extends PluginBase implements CheckoutFlowInterf /** * {@inheritdoc} */ public function getStepId() { return $this->stepId; } /** * {@inheritdoc} */ public function getPreviousStepId() { public function getPreviousStepId($step_id) { $step_ids = array_keys($this->getVisibleSteps()); $current_index = array_search($this->stepId, $step_ids); $current_index = array_search($step_id, $step_ids); return isset($step_ids[$current_index - 1]) ? $step_ids[$current_index - 1] : NULL; } /** * {@inheritdoc} */ public function getNextStepId() { public function getNextStepId($step_id) { $step_ids = array_keys($this->getVisibleSteps()); $current_index = array_search($this->stepId, $step_ids); $current_index = array_search($step_id, $step_ids); return isset($step_ids[$current_index + 1]) ? $step_ids[$current_index + 1] : NULL; } Loading @@ -154,7 +111,7 @@ abstract class CheckoutFlowBase extends PluginBase implements CheckoutFlowInterf * {@inheritdoc} */ public function redirectToStep($step_id) { $this->order->checkout_step = $step_id; $this->order->set('checkout_step', $step_id); if ($step_id == 'complete') { $transition = $this->order->getState()->getWorkflow()->getTransition('place'); $this->order->getState()->applyTransition($transition); Loading Loading @@ -306,13 +263,19 @@ abstract class CheckoutFlowBase extends PluginBase implements CheckoutFlowInterf /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { public function buildForm(array $form, FormStateInterface $form_state, $step_id = NULL) { // The $step_id argument is optional only because PHP disallows adding required // arguments to an existing interface's method. if (empty($step_id)) { throw new \InvalidArgumentException('The $step_id cannot be empty.'); } $steps = $this->getVisibleSteps(); $form['#tree'] = TRUE; $form['#title'] = $steps[$this->stepId]['label']; $form['#step_id'] = $step_id; $form['#title'] = $steps[$step_id]['label']; $form['#theme'] = ['commerce_checkout_form']; $form['#attached']['library'][] = 'commerce_checkout/form'; if ($steps[$this->stepId]['has_order_summary']) { if ($steps[$step_id]['has_order_summary']) { if ($order_summary = $this->buildOrderSummary($form, $form_state)) { $form['order_summary'] = $order_summary; } Loading @@ -331,8 +294,8 @@ abstract class CheckoutFlowBase extends PluginBase implements CheckoutFlowInterf * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { if ($next_step_id = $this->getNextStepId()) { $this->order->checkout_step = $next_step_id; if ($next_step_id = $this->getNextStepId($form['#step_id'])) { $this->order->set('checkout_step', $next_step_id); $form_state->setRedirect('commerce_checkout.form', [ 'commerce_order' => $this->order->id(), 'step' => $next_step_id, Loading Loading @@ -361,8 +324,8 @@ abstract class CheckoutFlowBase extends PluginBase implements CheckoutFlowInterf */ protected function actions(array $form, FormStateInterface $form_state) { $steps = $this->getVisibleSteps(); $next_step_id = $this->getNextStepId(); $previous_step_id = $this->getPreviousStepId(); $next_step_id = $this->getNextStepId($form['#step_id']); $previous_step_id = $this->getPreviousStepId($form['#step_id']); $has_next_step = $next_step_id && isset($steps[$next_step_id]['next_label']); $has_previous_step = $previous_step_id && isset($steps[$previous_step_id]['previous_label']); Loading