44
55use Drupal \Core \Entity \EntityInterface ;
66use Drupal \Core \Entity \EntityListBuilder ;
7+ use Drupal \Core \Entity \EntityTypeInterface ;
8+ use Drupal \Core \Entity \EntityStorageInterface ;
79use Drupal \Core \Form \FormInterface ;
10+ use Drupal \Core \Form \FormBuilderInterface ;
811use Drupal \Core \Form \FormStateInterface ;
12+ use Symfony \Component \DependencyInjection \ContainerInterface ;
913
1014/**
11- * Defines the list builder for shipping methods .
15+ * Defines the list builder for promotions .
1216 */
1317class PromotionListBuilder extends EntityListBuilder implements FormInterface {
1418
1519 /**
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.
1728 *
18- * @var string
29+ * @var \Drupal\commerce_promotion\PromotionUsageInterface
1930 */
20- protected $ entitiesKey = ' promotions ' ;
31+ protected $ usage ;
2132
2233 /**
2334 * The entities being listed.
@@ -26,6 +37,13 @@ class PromotionListBuilder extends EntityListBuilder implements FormInterface {
2637 */
2738 protected $ entities = [];
2839
40+ /**
41+ * The usage counts.
42+ *
43+ * @var array
44+ */
45+ protected $ usageCounts ;
46+
2947 /**
3048 * Whether tabledrag is enabled.
3149 *
@@ -34,11 +52,35 @@ class PromotionListBuilder extends EntityListBuilder implements FormInterface {
3452 protected $ hasTableDrag = TRUE ;
3553
3654 /**
37- * The form builder .
55+ * Constructs a new PromotionListBuilder object .
3856 *
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.
4065 */
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+ }
4284
4385 /**
4486 * {@inheritdoc}
@@ -55,6 +97,8 @@ public function load() {
5597 $ entities = $ this ->storage ->loadMultiple ($ entity_ids );
5698 // Sort the entities using the entity class's sort() method.
5799 uasort ($ entities , [$ this ->entityType ->getClass (), 'sort ' ]);
100+ // Load the usage counts for each promotion.
101+ $ this ->usageCounts = $ this ->usage ->getUsageMultiple ($ entities );
58102
59103 return $ entities ;
60104 }
@@ -64,7 +108,9 @@ public function load() {
64108 */
65109 public function buildHeader () {
66110 $ 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 ' );
68114 if ($ this ->hasTableDrag ) {
69115 $ header ['weight ' ] = $ this ->t ('Weight ' );
70116 }
@@ -75,11 +121,19 @@ public function buildHeader() {
75121 * {@inheritdoc}
76122 */
77123 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 */
79128 $ row ['#attributes ' ]['class ' ][] = 'draggable ' ;
80129 $ row ['#weight ' ] = $ entity ->getWeight ();
81130 $ 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 ' ) : '— ' ;
83137 if ($ this ->hasTableDrag ) {
84138 $ row ['weight ' ] = [
85139 '#type ' => 'weight ' ,
@@ -97,7 +151,7 @@ public function buildRow(EntityInterface $entity) {
97151 * {@inheritdoc}
98152 */
99153 public function render () {
100- return \Drupal:: formBuilder () ->getForm ($ this );
154+ return $ this -> formBuilder ->getForm ($ this );
101155 }
102156
103157 /**
@@ -115,27 +169,25 @@ public function buildForm(array $form, FormStateInterface $form_state) {
115169 $ delta = ceil ($ count / 2 );
116170 }
117171
118- $ form [$ this -> entitiesKey ] = [
172+ $ form [' promotions ' ] = [
119173 '#type ' => 'table ' ,
120174 '#header ' => $ this ->buildHeader (),
121175 '#empty ' => $ this ->t ('There are no @label yet. ' , ['@label ' => $ this ->entityType ->getPluralLabel ()]),
122176 ];
123177 foreach ($ this ->entities as $ entity ) {
124178 $ 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 ' ]];
131183 if (isset ($ row ['weight ' ])) {
132184 $ row ['weight ' ]['#delta ' ] = $ delta ;
133185 }
134- $ form [$ this -> entitiesKey ][$ entity ->id ()] = $ row ;
186+ $ form [' promotions ' ][$ entity ->id ()] = $ row ;
135187 }
136188
137189 if ($ this ->hasTableDrag ) {
138- $ form [$ this -> entitiesKey ]['#tabledrag ' ][] = [
190+ $ form [' promotions ' ]['#tabledrag ' ][] = [
139191 'action ' => 'order ' ,
140192 'relationship ' => 'sibling ' ,
141193 'group ' => 'weight ' ,
@@ -162,7 +214,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
162214 * {@inheritdoc}
163215 */
164216 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 ) {
166218 if (isset ($ this ->entities [$ id ]) && $ this ->entities [$ id ]->getWeight () != $ value ['weight ' ]) {
167219 // Save entity only when its weight was changed.
168220 $ this ->entities [$ id ]->setWeight ($ value ['weight ' ]);
0 commit comments