Skip to content

Commit 7887beb

Browse files
committed
Issue #2863051 by bojanz: Allow the checkout sidebar to contain panes
1 parent 4e84a63 commit 7887beb

13 files changed

+196
-118
lines changed

modules/checkout/commerce_checkout.module

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function commerce_checkout_theme() {
2929
'commerce_checkout_form' => [
3030
'render element' => 'form',
3131
],
32-
'commerce_checkout_form__with_summary' => [
32+
'commerce_checkout_form__with_sidebar' => [
3333
'base hook' => 'commerce_checkout_form',
3434
],
3535
];
@@ -44,9 +44,9 @@ function commerce_checkout_theme_suggestions_commerce_checkout_form(array $varia
4444
$original = $variables['theme_hook_original'];
4545
$suggestions = [];
4646
$suggestions[] = $original;
47-
// If the checkout form has an order summary, suggest the enhanced layout.
48-
if (isset($variables['form']['order_summary']) && Element::isVisibleElement($variables['form']['order_summary'])) {
49-
$suggestions[] = $original . '__with_summary';
47+
// If the checkout form has a sidebar, suggest the enhanced layout.
48+
if (isset($variables['form']['sidebar']) && Element::isVisibleElement($variables['form']['sidebar'])) {
49+
$suggestions[] = $original . '__with_sidebar';
5050
}
5151

5252
return $suggestions;

modules/checkout/config/install/commerce_checkout.commerce_checkout_flow.default.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ label: Default
66
plugin: multistep_default
77
configuration:
88
display_checkout_progress: true
9-
order_summary_view: commerce_checkout_order_summary
109
panes:
1110
login:
1211
allow_guest_checkout: true
@@ -26,3 +25,7 @@ configuration:
2625
completion_message:
2726
step: complete
2827
weight: 4
28+
order_summary:
29+
view: commerce_checkout_order_summary
30+
step: _sidebar
31+
weight: 5

modules/checkout/config/schema/commerce_checkout.schema.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ commerce_checkout.commerce_checkout_flow.plugin.*:
2828
display_checkout_progress:
2929
type: boolean
3030
label: 'Display checkout progress'
31-
order_summary_view:
32-
type: string
33-
label: 'Order summary view'
3431

3532
commerce_checkout.commerce_checkout_flow.plugin.multistep_default:
3633
type: commerce_checkout_flow_with_panes_configuration
@@ -70,6 +67,13 @@ commerce_checkout.commerce_checkout_pane.login:
7067
type: boolean
7168
label: 'Allow registration'
7269

70+
commerce_checkout.commerce_checkout_pane.order_summary:
71+
type: commerce_checkout_pane_configuration
72+
mapping:
73+
view:
74+
type: string
75+
label: 'View'
76+
7377
commerce_checkout_pane_configuration:
7478
type: mapping
7579
mapping:

modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowBase.php

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ public function getSteps() {
133133
'payment' => [
134134
'label' => $this->t('Payment'),
135135
'next_label' => $this->t('Pay and complete purchase'),
136-
'has_order_summary' => FALSE,
136+
'has_sidebar' => FALSE,
137137
],
138138
'complete' => [
139139
'label' => $this->t('Complete'),
140140
'next_label' => $this->t('Pay and complete purchase'),
141-
'has_order_summary' => FALSE,
141+
'has_sidebar' => FALSE,
142142
],
143143
];
144144
}
@@ -178,36 +178,19 @@ public function setConfiguration(array $configuration) {
178178
public function defaultConfiguration() {
179179
return [
180180
'display_checkout_progress' => TRUE,
181-
'order_summary_view' => 'commerce_checkout_order_summary',
182181
];
183182
}
184183

185184
/**
186185
* {@inheritdoc}
187186
*/
188187
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
189-
$view_storage = $this->entityTypeManager->getStorage('view');
190-
$available_summary_views = [];
191-
/** @var \Drupal\views\Entity\View $view */
192-
foreach ($view_storage->loadMultiple() as $view) {
193-
if (strpos($view->get('tag'), 'commerce_order_summary') !== FALSE) {
194-
$available_summary_views[$view->id()] = $view->label();
195-
}
196-
}
197-
198188
$form['display_checkout_progress'] = [
199189
'#type' => 'checkbox',
200190
'#title' => $this->t('Display checkout progress'),
201191
'#description' => $this->t('Used by the checkout progress block to determine visibility.'),
202192
'#default_value' => $this->configuration['display_checkout_progress'],
203193
];
204-
$form['order_summary_view'] = [
205-
'#type' => 'select',
206-
'#title' => $this->t('Order summary view'),
207-
'#options' => $available_summary_views,
208-
'#empty_value' => '',
209-
'#default_value' => $this->configuration['order_summary_view'],
210-
];
211194

212195
return $form;
213196
}
@@ -224,7 +207,6 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
224207
if (!$form_state->getErrors()) {
225208
$values = $form_state->getValue($form['#parents']);
226209
$this->configuration['display_checkout_progress'] = $values['display_checkout_progress'];
227-
$this->configuration['order_summary_view'] = $values['order_summary_view'];
228210
}
229211
}
230212

@@ -258,10 +240,14 @@ public function buildForm(array $form, FormStateInterface $form_state, $step_id
258240
$form['#title'] = $steps[$step_id]['label'];
259241
$form['#theme'] = ['commerce_checkout_form'];
260242
$form['#attached']['library'][] = 'commerce_checkout/form';
261-
if ($steps[$step_id]['has_order_summary']) {
262-
if ($order_summary = $this->buildOrderSummary($form, $form_state)) {
263-
$form['order_summary'] = $order_summary;
264-
}
243+
if ($this->hasSidebar($step_id)) {
244+
$form['sidebar']['order_summary'] = [
245+
'#type' => 'view',
246+
'#name' => 'commerce_checkout_order_summary',
247+
'#display_id' => 'default',
248+
'#arguments' => [$this->order->id()],
249+
'#embed' => TRUE,
250+
];
265251
}
266252
$form['actions'] = $this->actions($form, $form_state);
267253

@@ -295,29 +281,17 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
295281
}
296282

297283
/**
298-
* Builds the order summary for the current checkout step.
284+
* Gets whether the given step has a sidebar.
299285
*
300-
* @param array $form
301-
* An associative array containing the structure of the form.
302-
* @param \Drupal\Core\Form\FormStateInterface $form_state
303-
* The current state of the form.
286+
* @param string $step_id
287+
* The step ID.
304288
*
305-
* @return array
306-
* The form structure.
289+
* @return bool
290+
* TRUE if the given step has a sidebar, FALSE otherwise.
307291
*/
308-
protected function buildOrderSummary(array $form, FormStateInterface $form_state) {
309-
$order_summary = [];
310-
if (!empty($this->configuration['order_summary_view'])) {
311-
$order_summary = [
312-
'#type' => 'view',
313-
'#name' => $this->configuration['order_summary_view'],
314-
'#display_id' => 'default',
315-
'#arguments' => [$this->order->id()],
316-
'#embed' => TRUE,
317-
];
318-
}
319-
320-
return $order_summary;
292+
protected function hasSidebar($step_id) {
293+
$steps = $this->getVisibleSteps();
294+
return $steps[$step_id]['has_sidebar'];
321295
}
322296

323297
/**

modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function redirectToStep($step_id);
6969
* customer back to this step.
7070
* - next_label: The label shown on the button that sends the customer to
7171
* this step.
72+
* - has_sidebar: Whether the step has a sidebar.
7273
* If the previous_label or next_label keys are missing, the corresponding
7374
* buttons will not be shown to the customer.
7475
*/

modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowWithPanesBase.php

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static function create(ContainerInterface $container, array $configuratio
8181
/**
8282
* {@inheritdoc}
8383
*/
84-
public function getPanes($step_id = NULL) {
84+
public function getPanes() {
8585
if (empty($this->panes)) {
8686
foreach ($this->paneManager->getDefinitions() as $pane_id => $pane_definition) {
8787
$pane_configuration = $this->getPaneConfiguration($pane_id);
@@ -98,13 +98,18 @@ public function getPanes($step_id = NULL) {
9898
}, $this->panes);
9999
}
100100

101-
$panes = $this->panes;
102-
if ($step_id) {
103-
$panes = array_filter($panes, function ($pane) use ($step_id) {
104-
/** @var \Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneInterface $pane */
105-
return $pane->getStepId() == $step_id;
106-
});
107-
}
101+
return $this->panes;
102+
}
103+
104+
/**
105+
* {@inheritdoc}
106+
*/
107+
public function getVisiblePanes($step_id) {
108+
$panes = $this->getPanes();
109+
$panes = array_filter($panes, function ($pane) use ($step_id) {
110+
/** @var \Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneInterface $pane */
111+
return ($pane->getStepId() == $step_id) && $pane->isVisible();
112+
});
108113

109114
return $panes;
110115
}
@@ -125,15 +130,7 @@ public function getVisibleSteps() {
125130
$steps = $this->getSteps();
126131
foreach ($steps as $step_id => $step) {
127132
// A step is visible if it has at least one visible pane.
128-
$is_visible = FALSE;
129-
foreach ($this->getPanes($step_id) as $pane) {
130-
if ($pane->isVisible()) {
131-
$is_visible = TRUE;
132-
break;
133-
}
134-
}
135-
136-
if (!$is_visible) {
133+
if (empty($this->getVisiblePanes($step_id))) {
137134
unset($steps[$step_id]);
138135
}
139136
}
@@ -201,6 +198,10 @@ protected function getTableRegions() {
201198
'message' => $this->t('No pane is displayed.'),
202199
];
203200
}
201+
$regions['_sidebar'] = [
202+
'title' => $this->t('Sidebar'),
203+
'message' => $this->t('No pane is displayed.'),
204+
];
204205
$regions['_disabled'] = [
205206
'title' => $this->t('Disabled', [], ['context' => 'Plural']),
206207
'message' => $this->t('No pane is disabled.'),
@@ -533,15 +534,26 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
533534
public function buildForm(array $form, FormStateInterface $form_state, $step_id = NULL) {
534535
$form = parent::buildForm($form, $form_state, $step_id);
535536

536-
$panes = $this->getPanes($step_id);
537-
foreach ($panes as $pane_id => $pane) {
538-
if ($pane->isVisible()) {
539-
$form[$pane_id] = [
540-
'#parents' => [$pane_id],
537+
foreach ($this->getVisiblePanes($step_id) as $pane_id => $pane) {
538+
$form[$pane_id] = [
539+
'#parents' => [$pane_id],
540+
'#type' => $pane->getWrapperElement(),
541+
'#title' => $pane->getLabel(),
542+
];
543+
$form[$pane_id] = $pane->buildPaneForm($form[$pane_id], $form_state, $form);
544+
}
545+
if ($this->hasSidebar($step_id)) {
546+
// The base class adds a hardcoded order summary view to the sidebar.
547+
// Remove it, there's a pane for that.
548+
unset($form['sidebar']);
549+
550+
foreach ($this->getVisiblePanes('_sidebar') as $pane_id => $pane) {
551+
$form['sidebar'][$pane_id] = [
552+
'#parents' => ['sidebar', $pane_id],
541553
'#type' => $pane->getWrapperElement(),
542554
'#title' => $pane->getLabel(),
543555
];
544-
$form[$pane_id] = $pane->buildPaneForm($form[$pane_id], $form_state, $form);
556+
$form['sidebar'][$pane_id] = $pane->buildPaneForm($form['sidebar'][$pane_id], $form_state, $form);
545557
}
546558
}
547559

@@ -554,10 +566,12 @@ public function buildForm(array $form, FormStateInterface $form_state, $step_id
554566
public function validateForm(array &$form, FormStateInterface $form_state) {
555567
parent::validateForm($form, $form_state);
556568

557-
$panes = $this->getPanes($form['#step_id']);
558-
foreach ($panes as $pane_id => $pane) {
559-
if ($pane->isVisible()) {
560-
$pane->validatePaneForm($form[$pane_id], $form_state, $form);
569+
foreach ($this->getVisiblePanes($form['#step_id']) as $pane_id => $pane) {
570+
$pane->validatePaneForm($form[$pane_id], $form_state, $form);
571+
}
572+
if ($this->hasSidebar($form['#step_id'])) {
573+
foreach ($this->getVisiblePanes('_sidebar') as $pane_id => $pane) {
574+
$pane->validatePaneForm($form['sidebar'][$pane_id], $form_state, $form);
561575
}
562576
}
563577
}
@@ -566,10 +580,12 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
566580
* {@inheritdoc}
567581
*/
568582
public function submitForm(array &$form, FormStateInterface $form_state) {
569-
$panes = $this->getPanes($form['#step_id']);
570-
foreach ($panes as $pane_id => $pane) {
571-
if ($pane->isVisible()) {
572-
$pane->submitPaneForm($form[$pane_id], $form_state, $form);
583+
foreach ($this->getVisiblePanes($form['#step_id']) as $pane_id => $pane) {
584+
$pane->submitPaneForm($form[$pane_id], $form_state, $form);
585+
}
586+
if ($this->hasSidebar($form['#step_id'])) {
587+
foreach ($this->getVisiblePanes('_sidebar') as $pane_id => $pane) {
588+
$pane->submitPaneForm($form['sidebar'][$pane_id], $form_state, $form);
573589
}
574590
}
575591

modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowWithPanesInterface.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,23 @@
88
interface CheckoutFlowWithPanesInterface extends CheckoutFlowInterface {
99

1010
/**
11-
* Gets the checkout flow's panes.
11+
* Gets the panes.
12+
*
13+
* @return \Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneInterface[]
14+
* The panes, keyed by pane id, ordered by weight.
15+
*/
16+
public function getPanes();
17+
18+
/**
19+
* Gets the visible panes for the given step ID.
1220
*
1321
* @param string $step_id
14-
* (Optional) The step ID to filter on.
22+
* The step ID.
1523
*
1624
* @return \Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneInterface[]
1725
* The panes, keyed by pane id, ordered by weight.
1826
*/
19-
public function getPanes($step_id = NULL);
27+
public function getVisiblePanes($step_id);
2028

2129
/**
2230
* Gets a pane with the given ID.

modules/checkout/src/Plugin/Commerce/CheckoutFlow/MultistepDefault.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ public function getSteps() {
2323
'login' => [
2424
'label' => $this->t('Login'),
2525
'previous_label' => $this->t('Go back'),
26-
'has_order_summary' => FALSE,
26+
'has_sidebar' => FALSE,
2727
],
2828
'order_information' => [
2929
'label' => $this->t('Order information'),
30-
'has_order_summary' => TRUE,
30+
'has_sidebar' => TRUE,
3131
'previous_label' => $this->t('Go back'),
3232
],
3333
'review' => [
3434
'label' => $this->t('Review'),
3535
'next_label' => $this->t('Continue to review'),
3636
'previous_label' => $this->t('Go back'),
37-
'has_order_summary' => TRUE,
37+
'has_sidebar' => TRUE,
3838
],
3939
] + parent::getSteps();
4040
}

modules/checkout/src/Plugin/Commerce/CheckoutPane/CheckoutPaneBase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public function setConfiguration(array $configuration) {
100100
*/
101101
public function defaultConfiguration() {
102102
$available_steps = array_keys($this->checkoutFlow->getSteps());
103+
$available_steps[] = '_sidebar';
103104
$default_step = $this->pluginDefinition['default_step'];
104105
if (!$default_step || !in_array($default_step, $available_steps)) {
105106
// The specified default step isn't available on the parent checkout flow.

0 commit comments

Comments
 (0)