Skip to content

Commit

Permalink
Issue #2871924: Rework the promotion end date widget
Browse files Browse the repository at this point in the history
  • Loading branch information
bojanz committed Apr 21, 2017
1 parent a02d6d7 commit 5ebb309
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 68 deletions.
2 changes: 1 addition & 1 deletion modules/promotion/src/Entity/Promotion.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setRequired(FALSE)
->setSetting('datetime_type', 'date')
->setDisplayOptions('form', [
'type' => 'commerce_optional_date',
'type' => 'commerce_end_date',
'weight' => 6,
]);

Expand Down
70 changes: 70 additions & 0 deletions modules/promotion/src/Plugin/Field/FieldWidget/EndDateWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Drupal\commerce_promotion\Plugin\Field\FieldWidget;

use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\datetime\Plugin\Field\FieldWidget\DateTimeDefaultWidget;

/**
* Plugin implementation of the 'commerce_end_date' widget.
*
* @FieldWidget(
* id = "commerce_end_date",
* label = @Translation("End date"),
* field_types = {
* "datetime"
* }
* )
*/
class EndDateWidget extends DateTimeDefaultWidget {

/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$element['has_value'] = [
'#type' => 'checkbox',
'#title' => $this->t('Provide an end date'),
'#default_value' => !empty($element['value']['#default_value']),
'#access' => empty($element['value']['#default_value']),
];
$element['value']['#weight'] = 10;
$element['value']['#description'] = '';
// Workaround for #2419131.
$field_name = $this->fieldDefinition->getName();
$element['container']['#type'] = 'container';
$element['container'] = [
'#type' => 'container',
'#states' => [
'visible' => [
':input[name="' . $field_name . '[' . $delta . '][has_value]"]' => ['checked' => TRUE],
],
],
];
$element['container']['value'] = $element['value'];
unset($element['value']);

return $element;
}

/**
* {@inheritdoc}
*/
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
foreach ($values as &$item) {
if (!empty($item['container']['value']) && $item['container']['value'] instanceof DrupalDateTime) {
$date = $item['container']['value'];
// Adjust the date for storage.
datetime_date_default_time($date);
$date->setTimezone(new \DateTimezone(DATETIME_STORAGE_TIMEZONE));
$item['value'] = $date->format(DATETIME_DATE_STORAGE_FORMAT);
unset($item['container']);
}
}
return $values;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function testCreatePromotionWithEndDate() {

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

$this->submitForm($edit, t('Save'));
$this->assertSession()->pageTextContains("Saved the $name promotion.");
Expand Down
66 changes: 0 additions & 66 deletions src/Plugin/Field/FieldWidget/OptionalDateWidget.php

This file was deleted.

0 comments on commit 5ebb309

Please sign in to comment.