commerce_amws-8.x-1.x-dev/modules/shipping/src/Hook/EntityField.php

modules/shipping/src/Hook/EntityField.php
<?php

namespace Drupal\commerce_amws_shipping\Hook;

use Drupal\commerce_amws\MachineName\Field\ShippingMethod as ShippingMethodField;

use Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;

/**
 * Provides additional field definitions for shipping methods.
 *
 * When we export shipments to Amazon MWS via Feeds we need to include the
 * shipping carrier and method. There is no easy way to programmatically know
 * which Drupal shipping method corresponds to which Amazon MWS shipping
 * method. We therefore add fields so that the merchants can configure the
 * mapping.
 */
class EntityField {

  use StringTranslationTrait;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The entity definition update manager.
   *
   * @var \Drupal\Core\Entity\EntityDefinitionUpdaterManagerInterface
   */
  protected $updateManager;

  /**
   * Constructs a new EntityField object.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface $update_manager
   *   The entity definition update manager.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   */
  public function __construct(
    ModuleHandlerInterface $module_handler,
    EntityDefinitionUpdateManagerInterface $update_manager,
    TranslationInterface $string_translation
  ) {
    $this->moduleHandler = $module_handler;
    $this->updateManager = $update_manager;

    // Dependency injection for properties defined by traits.
    $this->stringTranslation = $string_translation;
  }

  /**
   * Returns the additional field definitions.
   *
   * @return \Drupal\Core\Field\FieldDefinitionInterface[]
   *   An array containing the field definitions.
   */
  public function getShippingMethodBaseFieldDefinitions() {
    // The following fields are only required if we have the Feeds sub-module
    // enabled. They are used to send carrier details to Amazon MWS via feeds
    // and they are not needed if that functionality is not enabled.
    if (!$this->moduleHandler->moduleExists('commerce_amws_feed')) {
      return [];
    }

    $fields = [];

    $fields[ShippingMethodField::CARRIER_CODE] = BaseFieldDefinition::create('string')
      ->setLabel($this->t('Amazon MWS carrier code'))
      ->setDescription($this->t(
        'The identifier of the shipping method in the Amazon MWS systems.'
      ))
      // We make the field required by default. Trying to export shipments with
      // shipping methods that do not have a mapping defined will lead to
      // errors.
      ->setRequired(TRUE)
      ->setSettings([
        'default_value' => '',
        'max_length' => 255,
      ])
      ->setDisplayOptions(
        'form',
        [
          'type' => 'string_textfield',
          'weight' => 0,
        ]
      )
      ->setDisplayConfigurable('view', TRUE)
      ->setDisplayConfigurable('form', TRUE);

    $fields[ShippingMethodField::SHIPPING_METHOD] = BaseFieldDefinition::create('string')
      ->setLabel($this->t('Amazon MWS shipping method label'))
      ->setDescription($this->t(
        'The label of the shipping method in the Amazon MWS systems.'
      ))
      // We make the field required by default. Trying to export shipments with
      // shipping methods that do not have a mapping defined will lead to
      // errors.
      ->setSettings([
        'default_value' => '',
        'max_length' => 255,
      ])
      ->setRequired(TRUE)
      ->setDisplayOptions(
        'form',
        [
          'type' => 'string_textfield',
          'weight' => 0,
        ]
      )
      ->setDisplayConfigurable('view', TRUE)
      ->setDisplayConfigurable('form', TRUE);

    return $fields;
  }

  /**
   * Installs the additional field definitions on the shipping method entity.
   */
  public function installShippingMethodBaseFieldDefinitions() {
    $definitions = $this->getShippingMethodBaseFieldDefinitions();

    foreach ($definitions as $name => $definition) {
      $this->updateManager->installFieldStorageDefinition(
        $name,
        'commerce_shipping_method',
        'commerce_amws_shipping',
        $definition
      );
    }
  }

}

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

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