4
4
5
5
use Drupal \Core \Entity \EntityInterface ;
6
6
use Drupal \Core \Entity \EntityListBuilder ;
7
+ use Drupal \Core \Entity \EntityTypeInterface ;
8
+ use Drupal \Core \Entity \EntityStorageInterface ;
7
9
use Drupal \Core \Form \FormInterface ;
10
+ use Drupal \Core \Form \FormBuilderInterface ;
8
11
use Drupal \Core \Form \FormStateInterface ;
12
+ use Symfony \Component \DependencyInjection \ContainerInterface ;
9
13
10
14
/**
11
- * Defines the list builder for shipping methods .
15
+ * Defines the list builder for promotions .
12
16
*/
13
17
class PromotionListBuilder extends EntityListBuilder implements FormInterface {
14
18
15
19
/**
16
- * The key to use for the form element containing the entities.
20
+ * The form builder.
21
+ *
22
+ * @var \Drupal\Core\Form\FormBuilderInterface
23
+ */
24
+ protected $ formBuilder ;
25
+
26
+ /**
27
+ * The usage.
17
28
*
18
- * @var string
29
+ * @var \Drupal\commerce_promotion\PromotionUsageInterface
19
30
*/
20
- protected $ entitiesKey = ' promotions ' ;
31
+ protected $ usage ;
21
32
22
33
/**
23
34
* The entities being listed.
@@ -26,6 +37,13 @@ class PromotionListBuilder extends EntityListBuilder implements FormInterface {
26
37
*/
27
38
protected $ entities = [];
28
39
40
+ /**
41
+ * The usage counts.
42
+ *
43
+ * @var array
44
+ */
45
+ protected $ usageCounts ;
46
+
29
47
/**
30
48
* Whether tabledrag is enabled.
31
49
*
@@ -34,11 +52,35 @@ class PromotionListBuilder extends EntityListBuilder implements FormInterface {
34
52
protected $ hasTableDrag = TRUE ;
35
53
36
54
/**
37
- * The form builder .
55
+ * Constructs a new PromotionListBuilder object .
38
56
*
39
- * @var \Drupal\Core\Form\FormBuilderInterface
57
+ * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
58
+ * The entity type definition.
59
+ * @param \Drupal\Core\Entity\EntityStorageInterface $storage
60
+ * The entity storage.
61
+ * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
62
+ * The form builder.
63
+ * @param \Drupal\commerce_promotion\PromotionUsageInterface $usage
64
+ * The usage.
40
65
*/
41
- protected $ formBuilder ;
66
+ public function __construct (EntityTypeInterface $ entity_type , EntityStorageInterface $ storage , FormBuilderInterface $ form_builder , PromotionUsageInterface $ usage ) {
67
+ parent ::__construct ($ entity_type , $ storage );
68
+
69
+ $ this ->formBuilder = $ form_builder ;
70
+ $ this ->usage = $ usage ;
71
+ }
72
+
73
+ /**
74
+ * {@inheritdoc}
75
+ */
76
+ public static function createInstance (ContainerInterface $ container , EntityTypeInterface $ entity_type ) {
77
+ return new static (
78
+ $ entity_type ,
79
+ $ container ->get ('entity.manager ' )->getStorage ($ entity_type ->id ()),
80
+ $ container ->get ('form_builder ' ),
81
+ $ container ->get ('commerce_promotion.usage ' )
82
+ );
83
+ }
42
84
43
85
/**
44
86
* {@inheritdoc}
@@ -55,6 +97,8 @@ public function load() {
55
97
$ entities = $ this ->storage ->loadMultiple ($ entity_ids );
56
98
// Sort the entities using the entity class's sort() method.
57
99
uasort ($ entities , [$ this ->entityType ->getClass (), 'sort ' ]);
100
+ // Load the usage counts for each promotion.
101
+ $ this ->usageCounts = $ this ->usage ->getUsageMultiple ($ entities );
58
102
59
103
return $ entities ;
60
104
}
@@ -64,7 +108,9 @@ public function load() {
64
108
*/
65
109
public function buildHeader () {
66
110
$ header ['name ' ] = $ this ->t ('Name ' );
67
- $ header ['status ' ] = $ this ->t ('Enabled ' );
111
+ $ header ['usage ' ] = $ this ->t ('Usage ' );
112
+ $ header ['start_date ' ] = $ this ->t ('Start date ' );
113
+ $ header ['end_date ' ] = $ this ->t ('End date ' );
68
114
if ($ this ->hasTableDrag ) {
69
115
$ header ['weight ' ] = $ this ->t ('Weight ' );
70
116
}
@@ -75,11 +121,19 @@ public function buildHeader() {
75
121
* {@inheritdoc}
76
122
*/
77
123
public function buildRow (EntityInterface $ entity ) {
78
- /** @var \Drupal\commerce_shipping\Entity\ShippingMethodInterface $entity */
124
+ $ current_usage = $ this ->usageCounts [$ entity ->id ()];
125
+ $ usage_limit = $ entity ->getUsageLimit ();
126
+ $ usage_limit = $ usage_limit ?: $ this ->t ('Unlimited ' );
127
+ /** @var \Drupal\commerce_promotion\Entity\PromotionInterface $entity */
79
128
$ row ['#attributes ' ]['class ' ][] = 'draggable ' ;
80
129
$ row ['#weight ' ] = $ entity ->getWeight ();
81
130
$ row ['name ' ] = $ entity ->label ();
82
- $ row ['status ' ] = $ entity ->isEnabled () ? $ this ->t ('Enabled ' ) : $ this ->t ('Disabled ' );
131
+ if (!$ entity ->isEnabled ()) {
132
+ $ row ['name ' ] .= ' ( ' . $ this ->t ('Disabled ' ) . ') ' ;
133
+ }
134
+ $ row ['usage ' ] = $ current_usage . ' / ' . $ usage_limit ;
135
+ $ row ['start_date ' ] = $ entity ->getStartDate ()->format ('M jS Y ' );
136
+ $ row ['end_date ' ] = $ entity ->getEndDate () ? $ entity ->getEndDate ()->format ('M jS Y ' ) : '— ' ;
83
137
if ($ this ->hasTableDrag ) {
84
138
$ row ['weight ' ] = [
85
139
'#type ' => 'weight ' ,
@@ -97,7 +151,7 @@ public function buildRow(EntityInterface $entity) {
97
151
* {@inheritdoc}
98
152
*/
99
153
public function render () {
100
- return \Drupal:: formBuilder () ->getForm ($ this );
154
+ return $ this -> formBuilder ->getForm ($ this );
101
155
}
102
156
103
157
/**
@@ -115,27 +169,25 @@ public function buildForm(array $form, FormStateInterface $form_state) {
115
169
$ delta = ceil ($ count / 2 );
116
170
}
117
171
118
- $ form [$ this -> entitiesKey ] = [
172
+ $ form [' promotions ' ] = [
119
173
'#type ' => 'table ' ,
120
174
'#header ' => $ this ->buildHeader (),
121
175
'#empty ' => $ this ->t ('There are no @label yet. ' , ['@label ' => $ this ->entityType ->getPluralLabel ()]),
122
176
];
123
177
foreach ($ this ->entities as $ entity ) {
124
178
$ row = $ this ->buildRow ($ entity );
125
- if (isset ($ row ['name ' ])) {
126
- $ row ['name ' ] = ['#markup ' => $ row ['name ' ]];
127
- }
128
- if (isset ($ row ['status ' ])) {
129
- $ row ['status ' ] = ['#markup ' => $ row ['status ' ]];
130
- }
179
+ $ row ['name ' ] = ['#markup ' => $ row ['name ' ]];
180
+ $ row ['usage ' ] = ['#markup ' => $ row ['usage ' ]];
181
+ $ row ['start_date ' ] = ['#markup ' => $ row ['start_date ' ]];
182
+ $ row ['end_date ' ] = ['#markup ' => $ row ['end_date ' ]];
131
183
if (isset ($ row ['weight ' ])) {
132
184
$ row ['weight ' ]['#delta ' ] = $ delta ;
133
185
}
134
- $ form [$ this -> entitiesKey ][$ entity ->id ()] = $ row ;
186
+ $ form [' promotions ' ][$ entity ->id ()] = $ row ;
135
187
}
136
188
137
189
if ($ this ->hasTableDrag ) {
138
- $ form [$ this -> entitiesKey ]['#tabledrag ' ][] = [
190
+ $ form [' promotions ' ]['#tabledrag ' ][] = [
139
191
'action ' => 'order ' ,
140
192
'relationship ' => 'sibling ' ,
141
193
'group ' => 'weight ' ,
@@ -162,7 +214,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
162
214
* {@inheritdoc}
163
215
*/
164
216
public function submitForm (array &$ form , FormStateInterface $ form_state ) {
165
- foreach ($ form_state ->getValue ($ this -> entitiesKey ) as $ id => $ value ) {
217
+ foreach ($ form_state ->getValue (' promotions ' ) as $ id => $ value ) {
166
218
if (isset ($ this ->entities [$ id ]) && $ this ->entities [$ id ]->getWeight () != $ value ['weight ' ]) {
167
219
// Save entity only when its weight was changed.
168
220
$ this ->entities [$ id ]->setWeight ($ value ['weight ' ]);
0 commit comments