arch-8.x-1.x-dev/modules/checkout/src/Routing/OrderIdParamConverter.php
modules/checkout/src/Routing/OrderIdParamConverter.php
<?php namespace Drupal\arch_checkout\Routing; use Drupal\Component\Utility\Xss; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\ParamConverter\ParamConverterInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Routing\Route; /** * Parameter converter for upcasting order id to order object. */ class OrderIdParamConverter implements ParamConverterInterface, ContainerInjectionInterface { /** * Entity type manager. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityTypeManager; /** * Order storage. * * @var \Drupal\Core\Entity\EntityStorageInterface */ protected $orderStorage; /** * OrderIdParamConverter constructor. * * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * Entity type manager. * * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException */ public function __construct( EntityTypeManagerInterface $entityTypeManager, ) { $this->entityTypeManager = $entityTypeManager; $this->orderStorage = $entityTypeManager->getStorage('order'); } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( $container->get('entity_type.manager') ); } /** * {@inheritdoc} */ public function applies($definition, $name, Route $route) { return isset($definition['type']) && $definition['type'] == 'arch_checkout_order_id'; } /** * {@inheritdoc} */ public function convert($value, $definition, $name, array $defaults) { $order_id = Xss::filter($value); // First, we try loading order by id. $order = $this->orderStorage->load($order_id); if (empty($order)) { // Second, we also try loading order by order number. $order = $this->orderStorage->loadByProperties(['order_number' => $order_id]); } // If no order found, we return NULL. if (empty($order)) { return NULL; } return $order; } }