commerce_amws-8.x-1.x-dev/modules/order/src/Plugin/EntitySync/FieldTransformer/OrderItemOrderId.php

modules/order/src/Plugin/EntitySync/FieldTransformer/OrderItemOrderId.php
<?php

namespace Drupal\commerce_amws_order\Plugin\EntitySync\FieldTransformer;

use Drupal\entity_sync\FieldTransformer\PluginBase;

use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

/**
 * Transformer that sets the order ID in the corresponding order item field.
 *
 * We don't have the order entity available in the field transformer. We do have
 * the order's remote ID so we load it from the database.
 *
 * @EntitySyncFieldTransformer(
 *   id = "commerce_amws_order_item_order_id"
 * )
 */
class OrderItemOrderId extends PluginBase {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a \Drupal\Component\Plugin\PluginBase object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Symfony\Component\Validator\Validator\ValidatorInterface $constraint_validator
   *   The constraint validator.
   * @param \Drupal\Component\Plugin\PluginManagerInterface $constraint_manager
   *   The constraint plugin manager.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    ValidatorInterface $constraint_validator,
    PluginManagerInterface $constraint_manager,
    EntityTypeManagerInterface $entity_type_manager
  ) {
    parent::__construct(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $constraint_validator,
      $constraint_manager
    );

    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(
    ContainerInterface $container,
    ValidatorInterface $constraint_validator,
    array $configuration,
    $plugin_id,
    $plugin_definition
  ) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $constraint_validator,
      $container->get('plugin.manager.entity_sync_validation_constraint'),
      $container->get('entity_type.manager')
    );
  }

  /**
   * {@inheritdoc}
   */
  protected function transformImportedValue(
    $value,
    \stdClass $remote_entity,
    ContentEntityInterface $local_entity,
    array $field_info,
    array $context
  ) {
    if (!isset($context['amws_order_id'])) {
      throw new \RuntimeException(
        'No order ID provided in the order item import context.'
      );
    }

    $order_ids = $this->entityTypeManager
      ->getStorage('commerce_order')
      ->getQuery()
      ->accessCheck(FALSE)
      ->condition('amws_remote_id', $context['amws_order_id'])
      ->execute();
    if (!$order_ids) {
      throw new \RuntimeException(
        'No order ID was found for the given Amazon MWS order ID.'
      );
    }
    if (count($order_ids) > 1) {
      throw new \RuntimeException(
        'More than one order was found for the given Amazon MWS order ID.'
      );
    }

    return current($order_ids);
  }

}

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

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