You are here

commerce_purchase_order.module in Commerce Purchase Order 8

Same filename and directory in other branches
  1. 7 commerce_purchase_order.module

File

commerce_purchase_order.module
View source
<?php

/**
 * @file
 * Contains commerce_purchase_order.module..
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\HasPaymentInstructionsInterface;

/**
 * Implements hook_help().
 */
function commerce_purchase_order_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the commerce_purchase_order module.
    case 'help.page.commerce_purchase_order':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Adds purchase order payment method') . '</p>';
      return $output;
    default:
      return '';
  }
}

/**
 * Implements hook_entity_field_access().
 */
function commerce_purchase_order_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
  if ($field_definition
    ->getName() == 'field_purchase_orders_authorized') {
    if ($account
      ->hasPermission('authorize user purchase orders')) {
      return AccessResult::allowed();
    }
    else {
      return AccessResult::forbidden();
    }
  }
  else {
    return AccessResult::neutral();
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function commerce_purchase_order_preprocess_commerce_order_receipt(&$variables) {
  $variables['payment_instructions'] = '';

  /** @var Drupal\commerce_order\Entity\OrderInterface $order */
  $order = $variables['order_entity'];
  if ($order
    ->get('payment_gateway')
    ->isEmpty()) {
    return;
  }

  /** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
  $payment_gateway = $order
    ->get('payment_gateway')->entity;

  /** @var \Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\HasPaymentInstructionsInterface $payment_gateway_plugin */
  $payment_gateway_plugin = $payment_gateway
    ->getPlugin();
  if ($payment_gateway_plugin instanceof HasPaymentInstructionsInterface) {
    $payment_storage = \Drupal::entityTypeManager()
      ->getStorage('commerce_payment');
    $payments = $payment_storage
      ->loadMultipleByOrder($order);
    $payments = array_filter($payments, function ($payment) use ($payment_gateway) {
      return $payment
        ->getPaymentGatewayId() == $payment_gateway
        ->id();
    });
    $payment = reset($payments);
    if ($payment) {
      $variables['payment_instructions'] = $payment_gateway_plugin
        ->buildPaymentInstructions($payment);
    }
  }
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function commerce_purchase_order_theme_suggestions_commerce_order_receipt_alter(array &$suggestions, array $variables) {

  /** @var Drupal\commerce_order\Entity\OrderInterface $order */
  $order = $variables['order_entity'];
  $original = 'commerce_order_receipt';
  $suggestions[] = $original . '__' . 'purchase_order_gateway';
  if ($order instanceof OrderInterface) {
    $suggestions[] = $original . '__' . $order
      ->bundle() . '__' . 'purchase_order_gateway';
  }
}

/**
 * Implements hook_theme() for commerce_purchase_order().
 */
function commerce_purchase_order_theme($existing, $type, $theme, $path) {
  return [
    'commerce_order_receipt__purchase_order_gateway' => [
      'template' => 'commerce-order-receipt--purchase-order-gateway',
      'base hook' => 'commerce_order_receipt',
    ],
  ];
}