Commit 394a3a72 authored by Tom Ashe's avatar Tom Ashe Committed by Tom Ashe
Browse files

Issue #2943888 by pmagunia, Grimreaper, robcarr, junaidpv, Kojo Unsui,...

Issue #2943888 by pmagunia, Grimreaper, robcarr, junaidpv, Kojo Unsui, attisan, joekers, robotjox, fy1128, rszrama, TomTech: Add the ability to re-purchase a license to extend it
parent 5ab0600a
Loading
Loading
Loading
Loading
+35 −13
Original line number Diff line number Diff line
@@ -5,13 +5,13 @@
 * Contains commerce_license.module.
 */

use Drupal\commerce_license\FormAlter\GrantedEntityFormAlter;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\commerce_license\FormAlter\GrantedEntityFormAlter;

/**
 * Implements hook_help().
@@ -20,8 +20,7 @@ function commerce_license_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the commerce_license module.
    case 'help.page.commerce_license':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('License entities and product behavior') . '</p>';
      return $output;

@@ -34,7 +33,7 @@ function commerce_license_help($route_name, RouteMatchInterface $route_match) {
 */
function commerce_license_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {
  // Add constraints to product variations which use subscription licenses.
  if ($entity_type->id() == 'commerce_product_variation'
  if ($entity_type->id() === 'commerce_product_variation'
    && !empty($fields['subscription_type'])
    && !empty($fields['license_type'])
  ) {
@@ -67,14 +66,38 @@ function commerce_license_commerce_order_item_delete(EntityInterface $entity) {

  // Only act if the order is in draft. If the order is being edited when
  // complete, the license is probably active.
  if ($entity->getOrder() && $entity->getOrder()->getState()->getId() != 'draft') {
  $order = $entity->getOrder();
  if ($order) {
    $order_state = $order->getState()->getId();
    if ($order_state !== 'draft') {
      return;
    }
  }
  else {
    // The order was deleted.
    return;
  }

  /** @var \Drupal\commerce_license\Entity\LicenseInterface $license */
  $license = $entity->get('license')->entity;
  if ($license->getState()->getId() === 'renewal_in_progress') {
    // Already active license was chosen to renew.
    // Need to put it back in 'active' state.
    // But directly setting 'active' state
    // will be caught by License::preSave() and extend its expiry time!
    // So, first put it into 'renewal_cancelled' state,
    // then to 'active'.
    $license->getState()->applyTransitionById('cancel_renewal');
    $license->save();

    $license->getState()->applyTransitionById('confirm');
    $license->save();
  }
  else {
    // Delete the license.
  $license = $entity->license->entity;
    $license->delete();
  }
}

/**
 * Implements hook_form_alter().
@@ -108,7 +131,7 @@ function commerce_license_field_widget_form_alter(&$element, FormStateInterface
  $field_name = $field_definition->getName();
  $entity_type_id = $field_definition->getTargetEntityTypeId();

  if ($field_name == 'license_type' && $entity_type_id == 'commerce_product_variation') {
  if ($field_name === 'license_type' && $entity_type_id === 'commerce_product_variation') {
    // If the plugin ID form element doesn't have an '#options' key, then we
    // can't remove the plugins that should not be allowed here, either because
    // the widget for this field has been changed by an admin, or because the
@@ -118,7 +141,7 @@ function commerce_license_field_widget_form_alter(&$element, FormStateInterface
    // on the site, and so granting license that shouldn't be allowed is a
    // security risk.
    if (!isset($element['target_plugin_id']['#options'])) {
      throw new \Exception("Unable to change the plugin type options on the license_type field on the commerce_product_variation. Check the field widget type.");
      throw new \RuntimeException('Unable to change the plugin type options on the license_type field on the commerce_product_variation. Check the field widget type.');
    }

    // Get the allowed license types for this product variation type.
@@ -175,7 +198,7 @@ function commerce_license_theme() {
function commerce_license_theme_suggestions_commerce_license(array $variables) {
  $suggestions = [];
  $entity = $variables['elements']['#commerce_license'];
  $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
  $sanitized_view_mode = str_replace('.', '_', $variables['elements']['#view_mode']);

  $suggestions[] = 'commerce_license__' . $sanitized_view_mode;
  $suggestions[] = 'commerce_license__' . $entity->bundle();
@@ -213,18 +236,17 @@ function commerce_license_theme_suggestions_commerce_license_expire(array $varia
 * @return string
 *   The timezone name.
 */
function commerce_license_get_user_timezone(AccountInterface $user) {
function commerce_license_get_user_timezone(AccountInterface $user): string {
  $config = \Drupal::config('system.date');
  if ($user && $config->get('timezone.user.configurable') && $user->isAuthenticated() && $user->getTimezone()) {
  if ($config->get('timezone.user.configurable') && $user->isAuthenticated() && $user->getTimezone()) {
    return $user->getTimezone();
  }
  else {

  // Ignore PHP strict notice if time zone has not yet been set in the php.ini
  // configuration.
  $config_data_default_timezone = $config->get('timezone.default');
  return !empty($config_data_default_timezone) ? $config_data_default_timezone : @date_default_timezone_get();
}
}

/**
 * Implements hook_cron().
+9 −0
Original line number Diff line number Diff line
@@ -24,6 +24,15 @@ services:
    tags:
      - { name: commerce_order.order_processor }

  commerce_license.license_renewal_cart_event_subscriber:
    class: Drupal\commerce_license\EventSubscriber\LicenseRenewalCartEventSubscriber
    arguments:
      - '@entity_type.manager'
      - '@messenger'
      - '@date.formatter'
    tags:
      - { name: event_subscriber }

  commerce_license.license_multiples_cart_event_subscriber:
    class: Drupal\commerce_license\EventSubscriber\LicenseMultiplesCartEventSubscriber
    arguments: ['@messenger']
+13 −1
Original line number Diff line number Diff line
@@ -11,6 +11,10 @@ license_default:
      label: Pending
    active:
      label: Active
    renewal_in_progress:
      label: Renewal in progress
    renewal_cancelled:
      label: Renewal cancelled
    suspended:
      label: Suspended
    expired:
@@ -31,8 +35,16 @@ license_default:
      to: pending
    confirm:
      label: 'Confirm Activation'
      from: [new, pending]
      from: [new, pending, renewal_in_progress, renewal_cancelled]
      to: active
    renewal_in_progress:
      label: 'Renewal in progress'
      from: [active]
      to: renewal_in_progress
    cancel_renewal:
      label: 'Cancel renewal'
      from: [renewal_in_progress]
      to: renewal_cancelled
    suspend:
      label: 'Suspend'
      from: [active]
+3 −2
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@
  "minimum-stability": "dev",
  "support": {
    "issues": "https://www.drupal.org/project/issues/commerce_license",
    "source": "http://cgit.drupalcode.org/commerce_license"
    "source": "https://cgit.drupalcode.org/commerce_license"
  },
  "require": {
    "drupal/advancedqueue": "^1.0",
@@ -16,6 +16,7 @@
  },
  "require-dev": {
    "drupal/commerce_recurring": "^1.0@beta",
    "drupal/recurring_period": "^1.0"
    "drupal/recurring_period": "^1.0",
    "dms/phpunit-arraysubset-asserts": "^0.3"
  }
}
+9 −0
Original line number Diff line number Diff line
@@ -8,6 +8,15 @@ commerce_product.commerce_product_variation_type.*.third_party.commerce_license:
    activate_on_place:
      type: boolean
      label: 'Whether to activate a license when the order is placed'
    allow_renewal:
      type: boolean
      label: 'Allow renewal of license before expiration'
    interval:
      type: text
      label: 'Allow renewal of license within this timeframe of expiration (multiplier)'
    period:
      type: text
      label: 'Allow renewal of license within this timeframe of expiration (period unit)'

views.field.commerce_license__entity_label:
  type: views.field.entity_label
Loading