Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull upstream changes from commerce_promotion #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions commerce_fee.module
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\views\Plugin\views\field\EntityField;

/**
* Implements hook_commerce_condition_info_alter().
Expand Down Expand Up @@ -67,6 +68,31 @@ function template_preprocess_commerce_fee(array &$variables) {
}
}

/**
* Implements hook_form_FORM_ID_alter().
*
* Removes core's built-in formatters from views field options for
* fee start_date and end_date fields, since they perform timezone
* conversion. The "Default (Store timezone)" formatter should be used instead.
*/
function commerce_fee_form_views_ui_config_item_form_alter(&$form, FormStateInterface $form_state) {
/** @var \Drupal\views\Plugin\views\field\EntityField $handler */
$handler = $form_state->get('handler');
if ($handler instanceof EntityField && !empty($handler->definition['entity_type'])) {
$entity_type_id = $handler->definition['entity_type'];
$field_name = $handler->definition['field_name'];
/** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */
$field_manager = \Drupal::service('entity_field.manager');
$field_definitions = $field_manager->getFieldStorageDefinitions($entity_type_id);
$field_definition = $field_definitions[$field_name];
if ($entity_type_id == 'commerce_fee' && $field_definition->getType() == 'datetime') {
unset($form['options']['type']['#options']['datetime_custom']);
unset($form['options']['type']['#options']['datetime_default']);
unset($form['options']['type']['#options']['datetime_plain']);
}
}
}

/**
* Implements hook_field_widget_form_alter().
*/
Expand Down
56 changes: 56 additions & 0 deletions commerce_fee.post_update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/**
* @file
* Post update functions for Fee.
*/

/**
* Allows fee start and end dates to have a time component.
*/
function commerce_fee_post_update_1(array &$sandbox = NULL) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't need this, since it's a "new" module." But I guess some may have had this installed before

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be happy to remove it if you give me the word. It came in handy for me because I had already installed the broken version in another environment, but now that the update has run I don't need it.

$fee_storage = \Drupal::entityTypeManager()->getStorage('commerce_fee');
if (!isset($sandbox['current_count'])) {
$query = $fee_storage->getQuery();
$sandbox['total_count'] = $query->count()->execute();
$sandbox['current_count'] = 0;

if (empty($sandbox['total_count'])) {
$sandbox['#finished'] = 1;
return;
}
}

$query = $fee_storage->getQuery();
$query->range($sandbox['current_count'], 50);
$result = $query->execute();
if (empty($result)) {
$sandbox['#finished'] = 1;
return;
}

/** @var \Drupal\commerce_fee\Entity\Fee[] $fees */
$fees = $fee_storage->loadMultiple($result);
foreach ($fees as $fee) {
// Re-set each date to ensure it is stored in the updated format.
// Increase the end date by a day to match old inclusive loading
// (where an end date was valid until 23:59:59 of that day).
$start_date = $fee->getStartDate();
$end_date = $fee->getEndDate();
if ($end_date) {
$end_date = $end_date->modify('+1 day');
}
$fee->setStartDate($start_date);
$fee->setEndDate($end_date);

$fee->save();
}

$sandbox['current_count'] += 50;
if ($sandbox['current_count'] >= $sandbox['total_count']) {
$sandbox['#finished'] = 1;
}
else {
$sandbox['#finished'] = ($sandbox['total_count'] - $sandbox['current_count']) / $sandbox['total_count'];
}
}
80 changes: 41 additions & 39 deletions src/Entity/Fee.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,50 @@
use Drupal\commerce\ConditionGroup;
use Drupal\commerce\Entity\CommerceContentEntityBase;
use Drupal\commerce\Plugin\Commerce\Condition\ConditionInterface;
use Drupal\commerce\Plugin\Commerce\Condition\ParentEntityAwareInterface;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_fee\Plugin\Commerce\Fee\OrderItemFeeInterface;
use Drupal\commerce_fee\Plugin\Commerce\Fee\FeeInterface as FeePluginInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;

/**
* Defines the fee entity class.
*
* @ContentEntityType(
* id = "commerce_fee",
* label = @Translation("Fee"),
* label_collection = @Translation("Fees"),
* label_singular = @Translation("fee"),
* label_plural = @Translation("fees"),
* label = @Translation("Fee", context = "Commerce"),
* label_collection = @Translation("Fees", context = "Commerce"),
* label_singular = @Translation("fee", context = "Commerce"),
* label_plural = @Translation("fees", context = "Commerce"),
* label_count = @PluralTranslation(
* singular = "@count fee",
* plural = "@count fees",
* context = "Commerce",
* ),
* handlers = {
* "event" = "Drupal\commerce_fee\Event\FeeEvent",
* "storage" = "Drupal\commerce_fee\FeeStorage",
* "access" = "Drupal\entity\EntityAccessControlHandler",
* "permission_provider" = "Drupal\commerce\EntityPermissionProvider",
* "permission_provider" = "Drupal\entity\EntityPermissionProvider",
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\commerce_fee\FeeListBuilder",
* "views_data" = "Drupal\views\EntityViewsData",
* "views_data" = "Drupal\commerce_fee\FeeViewsData",
* "form" = {
* "default" = "Drupal\commerce_fee\Form\FeeForm",
* "add" = "Drupal\commerce_fee\Form\FeeForm",
* "edit" = "Drupal\commerce_fee\Form\FeeForm",
* "duplicate" = "Drupal\commerce_fee\Form\FeeForm",
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm"
* },
* "local_task_provider" = {
* "default" = "Drupal\entity\Menu\DefaultEntityLocalTaskProvider",
* },
* "route_provider" = {
* "default" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
* "default" = "Drupal\entity\Routing\AdminHtmlRouteProvider",
* "delete-multiple" = "Drupal\entity\Routing\DeleteMultipleRouteProvider",
* },
* "translation" = "Drupal\content_translation\ContentTranslationHandler"
Expand All @@ -60,6 +67,7 @@
* links = {
* "add-form" = "/fee/add",
* "edit-form" = "/fee/{commerce_fee}/edit",
* "duplicate-form" = "/fee/{commerce_fee}/duplicate",
* "delete-form" = "/fee/{commerce_fee}/delete",
* "delete-multiple-form" = "/admin/commerce/fees/delete",
* "collection" = "/admin/commerce/fees",
Expand Down Expand Up @@ -193,7 +201,11 @@ public function getConditions() {
$conditions = [];
foreach ($this->get('conditions') as $field_item) {
/** @var \Drupal\commerce\Plugin\Field\FieldType\PluginItemInterface $field_item */
$conditions[] = $field_item->getTargetInstance();
$condition = $field_item->getTargetInstance();
if ($condition instanceof ParentEntityAwareInterface) {
$condition->setParentEntity($this);
}
$conditions[] = $condition;
}
return $conditions;
}
Expand Down Expand Up @@ -232,34 +244,35 @@ public function setConditionOperator($condition_operator) {
/**
* {@inheritdoc}
*/
public function getStartDate() {
// Can't use the ->date property because it resets the timezone to UTC.
return new DrupalDateTime($this->get('start_date')->value);
public function getStartDate($store_timezone = 'UTC') {
return new DrupalDateTime($this->get('start_date')->value, $store_timezone);
}

/**
* {@inheritdoc}
*/
public function setStartDate(DrupalDateTime $start_date) {
$this->get('start_date')->value = $start_date->format('Y-m-d');
$this->get('start_date')->value = $start_date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
return $this;
}

/**
* {@inheritdoc}
*/
public function getEndDate() {
public function getEndDate($store_timezone = 'UTC') {
if (!$this->get('end_date')->isEmpty()) {
return new DrupalDateTime($this->get('end_date')->value);
return new DrupalDateTime($this->get('end_date')->value, $store_timezone);
}
}

/**
* {@inheritdoc}
*/
public function setEndDate(DrupalDateTime $end_date = NULL) {
$this->get('end_date')->value = $end_date ? $end_date->format('Y-m-d') : NULL;
return $this;
$this->get('end_date')->value = NULL;
if ($end_date) {
$this->get('end_date')->value = $end_date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
}
}

/**
Expand Down Expand Up @@ -290,12 +303,14 @@ public function available(OrderInterface $order) {
if (!in_array($order->getStoreId(), $this->getStoreIds())) {
return FALSE;
}
$time = \Drupal::time()->getRequestTime();
if ($this->getStartDate()->format('U') > $time) {
$date = $order->getCalculationDate();
$store_timezone = $date->getTimezone()->getName();
$start_date = $this->getStartDate($store_timezone);
if ($start_date->format('U') > $date->format('U')) {
return FALSE;
}
$end_date = $this->getEndDate();
if ($end_date && $end_date->format('U') <= $time) {
$end_date = $this->getEndDate($store_timezone);
if ($end_date && $end_date->format('U') <= $date->format('U')) {
return FALSE;
}

Expand Down Expand Up @@ -438,20 +453,21 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setLabel(t('Start date'))
->setDescription(t('The date the fee becomes valid.'))
->setRequired(TRUE)
->setSetting('datetime_type', 'date')
->setSetting('datetime_type', 'datetime')
->setDefaultValueCallback('Drupal\commerce_fee\Entity\Fee::getDefaultStartDate')
->setDisplayOptions('form', [
'type' => 'datetime_default',
'type' => 'commerce_store_datetime',
'weight' => 5,
]);

$fields['end_date'] = BaseFieldDefinition::create('datetime')
->setLabel(t('End date'))
->setDescription(t('The date after which the fee is invalid.'))
->setRequired(FALSE)
->setSetting('datetime_type', 'date')
->setSetting('datetime_type', 'datetime')
->setSetting('datetime_optional_label', t('Provide an end date'))
->setDisplayOptions('form', [
'type' => 'commerce_end_date',
'type' => 'commerce_store_datetime',
'weight' => 6,
]);

Expand Down Expand Up @@ -482,21 +498,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
*/
public static function getDefaultStartDate() {
$timestamp = \Drupal::time()->getRequestTime();
return gmdate('Y-m-d', $timestamp);
}

/**
* Default value callback for 'end_date' base field definition.
*
* @see ::baseFieldDefinitions()
*
* @return int
* The default value (date string).
*/
public static function getDefaultEndDate() {
// Today + 1 year.
$timestamp = \Drupal::time()->getRequestTime();
return gmdate('Y-m-d', $timestamp + 31536000);
return gmdate(DateTimeItemInterface::DATETIME_STORAGE_FORMAT, $timestamp);
}

}
42 changes: 32 additions & 10 deletions src/Entity/FeeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,36 +140,58 @@ public function getConditionOperator();
public function setConditionOperator($condition_operator);

/**
* Gets the fee start date.
* Gets the fee start date/time.
*
* The start date/time should always be used in the store timezone.
* Since the fee can belong to multiple stores, the timezone
* isn't known at load/save time, and is provided by the caller instead.
*
* Note that the returned date/time value is the same in any timezone,
* the "2019-10-17 10:00" stored value is returned as "2019-10-17 10:00 CET"
* for "Europe/Berlin" and "2019-10-17 10:00 ET" for "America/New_York".
*
* @param string $store_timezone
* The store timezone. E.g. "Europe/Berlin".
*
* @return \Drupal\Core\Datetime\DrupalDateTime
* The fee start date.
* The promotion start date/time.
*/
public function getStartDate();
public function getStartDate($store_timezone = 'UTC');

/**
* Sets the fee start date.
* Sets the fee start date/time.
*
* @param \Drupal\Core\Datetime\DrupalDateTime $start_date
* The fee start date.
* The fee start date/time.
*
* @return $this
*/
public function setStartDate(DrupalDateTime $start_date);

/**
* Gets the fee end date.
* Gets the fee end date/time.
*
* The end date/time should always be used in the store timezone.
* Since the promotion can belong to multiple stores, the timezone
* isn't known at load/save time, and is provided by the caller instead.
*
* Note that the returned date/time value is the same in any timezone,
* the "2019-10-17 11:00" stored value is returned as "2019-10-17 11:00 CET"
* for "Europe/Berlin" and "2019-10-17 11:00 ET" for "America/New_York".
*
* @param string $store_timezone
* The store timezone. E.g. "Europe/Berlin".
*
* @return \Drupal\Core\Datetime\DrupalDateTime
* The fee end date.
* The fee end date/time.
*/
public function getEndDate();
public function getEndDate($store_timezone = 'UTC');

/**
* Sets the fee end date.
* Sets the fee end date/time.
*
* @param \Drupal\Core\Datetime\DrupalDateTime $end_date
* The fee end date.
* The fee end date/time.
*
* @return $this
*/
Expand Down
4 changes: 2 additions & 2 deletions src/FeeListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public function buildRow(EntityInterface $entity) {
if (!$entity->isEnabled()) {
$row['name'] .= ' (' . $this->t('Disabled') . ')';
}
$row['start_date'] = $entity->getStartDate()->format('M jS Y');
$row['end_date'] = $entity->getEndDate() ? $entity->getEndDate()->format('M jS Y') : '—';
$row['start_date'] = $entity->getStartDate()->format('M jS Y H:i:s');
$row['end_date'] = $entity->getEndDate() ? $entity->getEndDate()->format('M jS Y H:i:s') : '—';

return $row + parent::buildRow($entity);
}
Expand Down
Loading