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 all 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
6 changes: 1 addition & 5 deletions commerce_fee.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ name: Commerce Fee
type: module
description: 'Provides a UI for managing fees.'
package: Commerce
# core: 8.x
core_version_requirement: ^8.7.7 || ^9
dependencies:
- commerce:commerce
- commerce:commerce_order
- inline_entity_form:inline_entity_form
- drupal:options

version: '8.x-2.4'
core: '8.x'
project: 'commerce'
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'];
}
}
Loading