Skip to content

Commit 958f297

Browse files
committed
2906332 Refactor message handeling method
1 parent 7507595 commit 958f297

File tree

5 files changed

+219
-0
lines changed

5 files changed

+219
-0
lines changed

modules/checkout/commerce_checkout.module

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function commerce_checkout_theme() {
2525
'commerce_checkout_completion_message' => [
2626
'variables' => [
2727
'order_entity' => NULL,
28+
'completion_messages' => NULL,
2829
'payment_instructions' => NULL,
2930
],
3031
],

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,71 @@
1515
*/
1616
class CompletionMessage extends CheckoutPaneBase {
1717

18+
/**
19+
* @var \Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CompletionMessages
20+
*/
21+
private $completionMessags;
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function __construct(array $configuration, $plugin_id, $plugin_definition, \Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowInterface $checkout_flow, \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager) {
27+
parent::__construct($configuration, $plugin_id, $plugin_definition, $checkout_flow, $entity_type_manager);
28+
$this->completionMessags = new CompletionMessages();
29+
}
30+
1831
/**
1932
* {@inheritdoc}
2033
*/
2134
public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
35+
$this->preparePaneForm();
36+
2237
$pane_form['#theme'] = 'commerce_checkout_completion_message';
2338
$pane_form['#order_entity'] = $this->order;
39+
$pane_form['#completion_messages'] = $this->completionMessags;
2440

2541
return $pane_form;
2642
}
2743

44+
/**
45+
* Prepares the necessary data for the completion messages.
46+
*/
47+
public function preparePaneForm() {
48+
$this->populateCompletionMessages();
49+
}
50+
51+
/**
52+
* Gets the completion messages.
53+
*/
54+
private function populateCompletionMessages() {
55+
$this->populateCompletionMessagesWithDefaultMessage();
56+
$this->populateCompletionMessagesWithLoggedInMessageIfLoggedIn();
57+
58+
$this->allowOthersToModifyMessages();
59+
}
60+
61+
/**
62+
* Gets the default completion message.
63+
*/
64+
private function populateCompletionMessagesWithDefaultMessage() {
65+
return $this->completionMessags->addMessage($this->t('Your order number is @number.', ['@number' => $this->order->id()]));
66+
}
67+
68+
/**
69+
* Populate the completion messages with the logged in message.
70+
*/
71+
private function populateCompletionMessagesWithLoggedInMessageIfLoggedIn() {
72+
if (\Drupal::currentUser()->isAuthenticated()) {
73+
$this->completionMessags->addMessage($this->t('You can view your order on your account page when logged in.'));
74+
}
75+
}
76+
77+
/**
78+
* Allow other modules to alter the messages.
79+
*/
80+
private function allowOthersToModifyMessages() {
81+
\Drupal::moduleHandler()
82+
->alter('checkout_completion_messages', $this->completionMessags, $this->order);
83+
}
84+
2885
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
namespace Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane;
4+
5+
use Drupal\Core\StringTranslation\TranslatableMarkup;
6+
use Drupal\Core\TypedData\TranslatableInterface;
7+
8+
/**
9+
* Acts as a container to collect all completion messages.
10+
*
11+
* @package Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane
12+
*/
13+
class CompletionMessages implements \Iterator, \Countable {
14+
15+
/**
16+
* @var \Drupal\Core\TypedData\TranslatableInterface[]
17+
*/
18+
private $messages;
19+
20+
/**
21+
* @var int
22+
*/
23+
private $position;
24+
25+
/**
26+
* Sets up the position to 0.
27+
*/
28+
public function __construct() {
29+
$this->position = 0;
30+
}
31+
32+
/**
33+
* Return the current element
34+
*
35+
* @link http://php.net/manual/en/iterator.current.php
36+
* @return mixed Can return any type.
37+
* @since 5.0.0
38+
*/
39+
public function current() {
40+
return $this->messages[$this->position];
41+
}
42+
43+
/**
44+
* Move forward to next element
45+
*
46+
* @link http://php.net/manual/en/iterator.next.php
47+
* @return void Any returned value is ignored.
48+
* @since 5.0.0
49+
*/
50+
public function next() {
51+
++$this->position;
52+
}
53+
54+
/**
55+
* Return the key of the current element
56+
*
57+
* @link http://php.net/manual/en/iterator.key.php
58+
* @return mixed scalar on success, or null on failure.
59+
* @since 5.0.0
60+
*/
61+
public function key() {
62+
return $this->position;
63+
}
64+
65+
/**
66+
* Checks if current position is valid
67+
*
68+
* @link http://php.net/manual/en/iterator.valid.php
69+
* @return boolean The return value will be casted to boolean and then
70+
* evaluated. Returns true on success or false on failure.
71+
* @since 5.0.0
72+
*/
73+
public function valid() {
74+
return isset($this->messages[$this->position]);
75+
}
76+
77+
/**
78+
* Rewind the Iterator to the first element
79+
*
80+
* @link http://php.net/manual/en/iterator.rewind.php
81+
* @return void Any returned value is ignored.
82+
* @since 5.0.0
83+
*/
84+
public function rewind() {
85+
$this->position = 0;
86+
}
87+
88+
/**
89+
* Adds a message to the array.
90+
*
91+
* @param \Drupal\Core\StringTranslation\TranslatableMarkup $message
92+
*/
93+
public function addMessage(TranslatableMarkup $message) {
94+
$this->messages[] = $message;
95+
}
96+
97+
/**
98+
* Count elements of an object
99+
*
100+
* @link http://php.net/manual/en/countable.count.php
101+
* @return int The custom count as an integer.
102+
* </p>
103+
* <p>
104+
* The return value is cast to an integer.
105+
* @since 5.1.0
106+
*/
107+
public function count() {
108+
return count($this->messages);
109+
}
110+
}

modules/checkout/templates/commerce-checkout-completion-message.html.twig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
*/
1212
#}
1313
<div class="checkout-complete">
14+
{% for message in completion_messages %}
15+
{{ message }}
16+
{% endfor %}
17+
1418
{{ 'Your order number is @number.'|t({'@number': order_entity.getOrderNumber}) }} <br>
1519
{{ 'You can view your order on your account page when logged in.'|t }} <br>
1620

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: robharings
5+
* Date: 09/09/2017
6+
* Time: 13:31
7+
*/
8+
9+
namespace Drupal\Tests\commerce_checkout\Kernel;
10+
11+
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CompletionMessages;
12+
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;
13+
14+
/**
15+
* Tests the completion messages class.
16+
*
17+
* @covers \Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CompletionMessages
18+
*/
19+
class CompletionMessagesTest extends CommerceKernelTestBase {
20+
21+
/**
22+
* @var CompletionMessages
23+
*/
24+
private $completionMessages;
25+
26+
public function setUp() {
27+
parent::setUp();
28+
$this->completionMessages = new CompletionMessages();
29+
}
30+
31+
public function testAddMessage() {
32+
$this->completionMessages->addMessage(t('Message 1'));
33+
$this->completionMessages->addMessage(t('Message 2'));
34+
35+
$this->assertCount(2, $this->completionMessages);
36+
}
37+
38+
public function testMessagesIterator() {
39+
$this->completionMessages->addMessage(t('Message 1'));
40+
$this->completionMessages->addMessage(t('Message 2'));
41+
42+
$this->assertEquals('Message 1', $this->completionMessages->current()->render());
43+
$this->completionMessages->next();
44+
$this->assertEquals('Message 2', $this->completionMessages->current()->render());
45+
}
46+
47+
}

0 commit comments

Comments
 (0)