Skip to content

Commit 5ebb309

Browse files
committed
Issue #2871924: Rework the promotion end date widget
1 parent a02d6d7 commit 5ebb309

File tree

4 files changed

+72
-68
lines changed

4 files changed

+72
-68
lines changed

modules/promotion/src/Entity/Promotion.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
598598
->setRequired(FALSE)
599599
->setSetting('datetime_type', 'date')
600600
->setDisplayOptions('form', [
601-
'type' => 'commerce_optional_date',
601+
'type' => 'commerce_end_date',
602602
'weight' => 6,
603603
]);
604604

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Drupal\commerce_promotion\Plugin\Field\FieldWidget;
4+
5+
use Drupal\Core\Datetime\DrupalDateTime;
6+
use Drupal\Core\Field\FieldItemListInterface;
7+
use Drupal\Core\Form\FormStateInterface;
8+
use Drupal\datetime\Plugin\Field\FieldWidget\DateTimeDefaultWidget;
9+
10+
/**
11+
* Plugin implementation of the 'commerce_end_date' widget.
12+
*
13+
* @FieldWidget(
14+
* id = "commerce_end_date",
15+
* label = @Translation("End date"),
16+
* field_types = {
17+
* "datetime"
18+
* }
19+
* )
20+
*/
21+
class EndDateWidget extends DateTimeDefaultWidget {
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
27+
$element = parent::formElement($items, $delta, $element, $form, $form_state);
28+
$element['has_value'] = [
29+
'#type' => 'checkbox',
30+
'#title' => $this->t('Provide an end date'),
31+
'#default_value' => !empty($element['value']['#default_value']),
32+
'#access' => empty($element['value']['#default_value']),
33+
];
34+
$element['value']['#weight'] = 10;
35+
$element['value']['#description'] = '';
36+
// Workaround for #2419131.
37+
$field_name = $this->fieldDefinition->getName();
38+
$element['container']['#type'] = 'container';
39+
$element['container'] = [
40+
'#type' => 'container',
41+
'#states' => [
42+
'visible' => [
43+
':input[name="' . $field_name . '[' . $delta . '][has_value]"]' => ['checked' => TRUE],
44+
],
45+
],
46+
];
47+
$element['container']['value'] = $element['value'];
48+
unset($element['value']);
49+
50+
return $element;
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
57+
foreach ($values as &$item) {
58+
if (!empty($item['container']['value']) && $item['container']['value'] instanceof DrupalDateTime) {
59+
$date = $item['container']['value'];
60+
// Adjust the date for storage.
61+
datetime_date_default_time($date);
62+
$date->setTimezone(new \DateTimezone(DATETIME_STORAGE_TIMEZONE));
63+
$item['value'] = $date->format(DATETIME_DATE_STORAGE_FORMAT);
64+
unset($item['container']);
65+
}
66+
}
67+
return $values;
68+
}
69+
70+
}

modules/promotion/tests/src/FunctionalJavascript/PromotionTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function testCreatePromotionWithEndDate() {
120120

121121
// Set an end date.
122122
$this->getSession()->getPage()->checkField('end_date[0][has_value]');
123-
$edit['end_date[0][value][date]'] = date("Y") + 1 . '-01-01';
123+
$edit['end_date[0][container][value][date]'] = date("Y") + 1 . '-01-01';
124124

125125
$this->submitForm($edit, t('Save'));
126126
$this->assertSession()->pageTextContains("Saved the $name promotion.");

src/Plugin/Field/FieldWidget/OptionalDateWidget.php

-66
This file was deleted.

0 commit comments

Comments
 (0)