22
33namespace Drupal \commerce_promotion \Entity ;
44
5+ use Drupal \commerce_order \Entity \OrderInterface ;
56use Drupal \Core \Field \BaseFieldDefinition ;
67use Drupal \Core \Entity \ContentEntityBase ;
78use Drupal \Core \Entity \EntityTypeInterface ;
@@ -71,18 +72,32 @@ public function setCode($code) {
7172 /**
7273 * {@inheritdoc}
7374 */
74- public function isActive () {
75+ public function isEnabled () {
7576 return (bool ) $ this ->getEntityKey ('status ' );
7677 }
7778
7879 /**
7980 * {@inheritdoc}
8081 */
81- public function setActive ( $ active ) {
82- $ this ->set ('status ' , (bool ) $ active );
82+ public function setEnabled ( $ enabled ) {
83+ $ this ->set ('status ' , (bool ) $ enabled );
8384 return $ this ;
8485 }
8586
87+ /**
88+ * {@inheritdoc}
89+ */
90+ public function available (OrderInterface $ order ) {
91+ if (!$ this ->isEnabled ()) {
92+ return FALSE ;
93+ }
94+ if (!$ this ->getPromotion ()->available ($ order )) {
95+ return FALSE ;
96+ }
97+
98+ return TRUE ;
99+ }
100+
86101 /**
87102 * {@inheritdoc}
88103 */
@@ -99,7 +114,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
99114
100115 $ fields ['code ' ] = BaseFieldDefinition::create ('string ' )
101116 ->setLabel (t ('Coupon code ' ))
102- ->setDescription (t ('The Coupon entity code . ' ))
117+ ->setDescription (t ('The unique, machine-readable identifier for a coupon . ' ))
103118 ->addConstraint ('CouponCode ' )
104119 ->setSettings ([
105120 'max_length ' => 50 ,
@@ -119,14 +134,18 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
119134 ->setDisplayConfigurable ('view ' , TRUE );
120135
121136 $ fields ['status ' ] = BaseFieldDefinition::create ('boolean ' )
122- ->setLabel (t ('Active status ' ))
123- ->setDescription (t ('A boolean indicating whether the Coupon is active . ' ))
137+ ->setLabel (t ('Status ' ))
138+ ->setDescription (t ('Whether the coupon is enabled . ' ))
124139 ->setDefaultValue (TRUE )
125- ->setDisplayOptions ('form ' , [
126- 'type ' => 'boolean_checkbox ' ,
127- 'weight ' => 99 ,
140+ ->setRequired (TRUE )
141+ ->setSettings ([
142+ 'on_label ' => t ('Enabled ' ),
143+ 'off_label ' => t ('Disabled ' ),
128144 ])
129- ->setDisplayConfigurable ('form ' , TRUE );
145+ ->setDisplayOptions ('form ' , [
146+ 'type ' => 'options_buttons ' ,
147+ 'weight ' => 0 ,
148+ ]);
130149
131150 return $ fields ;
132151 }
0 commit comments