commerce_api-8.x-1.x-dev/commerce_api.module

commerce_api.module
<?php

/**
 * @file
 * Provides hooks.
 */

use Drupal\commerce_api\Plugin\DataType\FormattedPrice;
use Drupal\commerce_api\Plugin\Field\ComputedResolvedPrice;
use Drupal\commerce_api\Plugin\openapi\OpenApiGenerator\AdjustedJsonApiGenerator;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Implements hook_entity_field_access().
 */
function commerce_api_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, ?FieldItemListInterface $items = NULL) {
  $field_access = \Drupal::getContainer()->get('commerce_api.field_access');
  return $field_access->handle($operation, $field_definition, $account, $items);
}

/**
 * Implements hook_entity_base_field_info_alter().
 */
function commerce_api_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
  /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
  if ($entity_type->id() === 'commerce_order') {
    $fields['order_number']->setRequired(FALSE);
    $fields['billing_profile']->setRequired(TRUE);

    if (isset($fields['payment_gateway'])) {
      // @todo add a constraint which marks it required if order total > 0;
    }
  }
}

/**
 * Implements hook_entity_base_field_info().
 */
function commerce_api_entity_base_field_info(EntityTypeInterface $entity_type) {
  if ($entity_type->id() === 'commerce_order') {
    $fields['billing_information'] = BaseFieldDefinition::create('order_profile')
      ->setLabel(t('Billing information'))
      // @note this will change after https://www.drupal.org/project/commerce/issues/2992281.
      ->setSetting('profile_bundle', 'customer')
      ->setSetting('profile_type', 'billing')
      ->setComputed(TRUE);
    $module_handler = \Drupal::moduleHandler();

    if ($module_handler->moduleExists('commerce_shipping')) {
      $fields['shipping_information'] = BaseFieldDefinition::create('order_profile')
        ->setLabel(t('Shipping information'))
        // @note this will change after https://www.drupal.org/project/commerce/issues/2992281.
        ->setSetting('profile_bundle', 'customer')
        ->setSetting('profile_type', 'shipping')
        ->setComputed(TRUE);
      $fields['shipping_method'] = BaseFieldDefinition::create('shipping_method')
        ->setLabel(t('Shipping method'))
        ->setComputed(TRUE);
    }

    if ($module_handler->moduleExists('commerce_payment')) {
      $fields['payment_instrument'] = BaseFieldDefinition::create('payment_instrument')
        ->setLabel(t('Payment instrument'))
        ->setComputed(TRUE);
    }

    $fields['order_total'] = BaseFieldDefinition::create('order_total')
      ->setLabel(t('Order total'))
      ->setComputed(TRUE);
    return $fields;
  }
  if ($entity_type->id() === 'commerce_product_variation') {
    $fields['resolved_price'] = BaseFieldDefinition::create('commerce_price')
      ->setLabel(t('Resolved price'))
      ->setReadOnly(TRUE)
      ->setComputed(TRUE)
      ->setCardinality(1)
      ->setSetting('source_field', 'price')
      ->setClass(ComputedResolvedPrice::class);
    return $fields;
  }
  if ($entity_type->id() === 'commerce_payment') {
    // @note: Drupal core's JSON:API module does not support `meta` properties
    // on resource objects, even though they are part of the JSON:API spec. To
    // support `capture` options for payments, we need a computed boolean field
    // on payment entities.
    $fields['capture'] = BaseFieldDefinition::create('boolean')
      ->setLabel(new TranslatableMarkup('Capture'))
      ->setDescription('Flag if this payment should be immediately captured.')
      ->setDefaultValue(TRUE)
      ->setComputed(TRUE)
      ->setCardinality(1);
    return $fields;
  }
  return [];
}

/**
 * Implements hook_openapi_generator_alter().
 *
 * Prevents Cart API resources from crashing OpenAPI.
 */
function commerce_api_openapi_generator_alter(array &$definitions) {
  if (isset($definitions['jsonapi'])) {
    $definitions['jsonapi']['class'] = AdjustedJsonApiGenerator::class;
  }
}

/**
 * Implements hook_data_type_info_alter().
 */
function commerce_api_data_type_info_alter(&$data_types) {
  if (isset($data_types['formatted_price'])) {
    $data_types['formatted_price']['class'] = FormattedPrice::class;
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc