You are here

commerce_recurring.module in Commerce Recurring Framework 8

Same filename and directory in other branches
  1. 7.2 commerce_recurring.module
  2. 7 commerce_recurring.module

Provides recurring billing for Drupal Commerce.

File

commerce_recurring.module
View source
<?php

/**
 * @file
 * Provides recurring billing for Drupal Commerce.
 */
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\commerce\PurchasableEntityInterface;

/**
 * Implements hook_commerce_entity_trait_info_alter().
 */
function commerce_recurring_commerce_entity_trait_info_alter(array &$definitions) {

  // Expose the purchasable entity trait for every purchasable entity type.
  $entity_types = \Drupal::entityTypeManager()
    ->getDefinitions();
  $entity_types = array_filter($entity_types, function (EntityTypeInterface $entity_type) {
    return $entity_type
      ->entityClassImplements(PurchasableEntityInterface::class);
  });
  $entity_type_ids = array_keys($entity_types);
  $definitions['purchasable_entity_subscription']['entity_types'] = $entity_type_ids;
}

/**
 * Implements hook_cron().
 */
function commerce_recurring_cron() {
  \Drupal::service('commerce_recurring.cron')
    ->run();
}

/**
 * Implements hook_field_widget_form_alter().
 */
function commerce_recurring_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
  $field_definition = $context['items']
    ->getFieldDefinition();
  $field_name = $field_definition
    ->getName();
  $widget_name = $context['widget']
    ->getPluginId();
  if ($field_name == 'subscription_type' && $widget_name == 'commerce_plugin_select') {

    // The standalone subscription type is used for subscriptions not backed
    // by a purchasable entity, so it doesn't make sense to offer it on a
    // purchasable entity form (such as the product variation one).
    unset($element['target_plugin_id']['#options']['standalone']);
  }
}

/**
 * Implements hook_form_FORM_ID_alter() for 'commerce_order_type_form'.
 */
function commerce_recurring_form_commerce_order_type_form_alter(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\commerce_order\Entity\OrderTypeInterface $order_type */
  $order_type = $form_state
    ->getFormObject()
    ->getEntity();
  if ($order_type
    ->id() != 'recurring') {

    // The recurring workflow is only meant for the recurring order type.
    unset($form['workflow']['#options']['Order']['order_recurring']);
  }
}

/**
 * Implements hook_form_FORM_ID_alter() for 'commerce_order_item_type_form'.
 */
function commerce_recurring_form_commerce_order_item_type_form_alter(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\commerce_order\Entity\OrderTypeInterface $order_item_type */
  $order_item_type = $form_state
    ->getFormObject()
    ->getEntity();
  if (!in_array($order_item_type
    ->id(), commerce_recurring_order_item_types())) {

    // Hide the recurring order type from the dropdown, to avoid confusing
    // merchants who are configuring non-recurring order item types.
    unset($form['orderType']['#options']['recurring']);
  }
}

/**
 * Implements hook_form_FORM_ID_alter() for 'commerce_product_variation_type_form'.
 */
function commerce_recurring_form_commerce_product_variation_type_form_alter(array &$form, FormStateInterface $form_state) {
  if (!isset($form['orderItemType'])) {
    return;
  }

  // Recurring order item types are used by the module, they should never
  // be selected by the merchant for a product variation type.
  foreach ($form['orderItemType']['#options'] as $id => $label) {
    if (in_array($id, commerce_recurring_order_item_types())) {
      unset($form['orderItemType']['#options'][$id]);
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter() for 'commerce_order_add_form'.
 */
function commerce_recurring_form_commerce_order_add_form_alter(array &$form, FormStateInterface $form_state) {
  $form['type']['#after_build'] = [
    'commerce_recurring_order_type_after_build',
  ];
}

/**
 * After build callback for the order type radio buttons in the order add form.
 */
function commerce_recurring_order_type_after_build(array $element, FormStateInterface $form_state) {

  // Hide the recurring order type from the radio buttons, it should never
  // be selected by the merchant when creating an order from the admin.
  unset($element['value']['#options']['recurring']);
  $element['value']['recurring']['#access'] = FALSE;
  return $element;
}

/**
 * Gets the recurring order item types.
 *
 * @return string[]
 *   The recurring order item type IDs.
 */
function commerce_recurring_order_item_types() {
  return [
    'recurring_standalone',
    'recurring_product_variation',
  ];
}

/**
 * Implements hook_theme().
 */
function commerce_recurring_theme() {
  return [
    'commerce_subscription_form' => [
      'render element' => 'form',
    ],
    'commerce_recurring_payment_declined' => [
      'variables' => [
        'order_entity' => NULL,
        'billing_information' => NULL,
        'shipping_information' => NULL,
        'payment_method_link' => NULL,
        'retry_num' => NULL,
        'retry_days' => NULL,
        'max_retries' => NULL,
        'remaining_retries' => NULL,
        'now' => NULL,
        'totals' => NULL,
      ],
    ],
    'commerce_subscription' => [
      'render element' => 'elements',
    ],
  ];
}

/**
 * Prepares variables for subscription templates.
 *
 * Default template: commerce-subscription.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - elements: An associative array containing rendered fields.
 *   - attributes: HTML attributes for the containing element.
 */
function template_preprocess_commerce_subscription(array &$variables) {

  /** @var \Drupal\commerce_recurring\Entity\SubscriptionInterface $subscription */
  $subscription = $variables['elements']['#commerce_subscription'];
  $variables['subscription_entity'] = $subscription;
  $variables['subscription'] = [];
  foreach (Element::children($variables['elements']) as $key) {
    $variables['subscription'][$key] = $variables['elements'][$key];
  }
}

/**
 * Implements hook_views_data_alter().
 */
function commerce_recurring_views_data_alter(array &$data) {
  $data['commerce_subscription']['store_id']['field']['id'] = 'commerce_store';
  $data['commerce_subscription']['state']['filter']['id'] = 'state_machine_state';
}

Functions

Namesort descending Description
commerce_recurring_commerce_entity_trait_info_alter Implements hook_commerce_entity_trait_info_alter().
commerce_recurring_cron Implements hook_cron().
commerce_recurring_field_widget_form_alter Implements hook_field_widget_form_alter().
commerce_recurring_form_commerce_order_add_form_alter Implements hook_form_FORM_ID_alter() for 'commerce_order_add_form'.
commerce_recurring_form_commerce_order_item_type_form_alter Implements hook_form_FORM_ID_alter() for 'commerce_order_item_type_form'.
commerce_recurring_form_commerce_order_type_form_alter Implements hook_form_FORM_ID_alter() for 'commerce_order_type_form'.
commerce_recurring_form_commerce_product_variation_type_form_alter Implements hook_form_FORM_ID_alter() for 'commerce_product_variation_type_form'.
commerce_recurring_order_item_types Gets the recurring order item types.
commerce_recurring_order_type_after_build After build callback for the order type radio buttons in the order add form.
commerce_recurring_theme Implements hook_theme().
commerce_recurring_views_data_alter Implements hook_views_data_alter().
template_preprocess_commerce_subscription Prepares variables for subscription templates.