You are here

class ProductRouteContext in Commerce Core 8.2

Sets the current product as context on commerce_product routes.

@todo Remove once core gets a generic EntityRouteContext.

Hierarchy

Expanded class hierarchy of ProductRouteContext

1 string reference to 'ProductRouteContext'
commerce_product.services.yml in modules/product/commerce_product.services.yml
modules/product/commerce_product.services.yml
1 service uses ProductRouteContext
commerce_product.product_route_context in modules/product/commerce_product.services.yml
Drupal\commerce_product\ContextProvider\ProductRouteContext

File

modules/product/src/ContextProvider/ProductRouteContext.php, line 19

Namespace

Drupal\commerce_product\ContextProvider
View source
class ProductRouteContext implements ContextProviderInterface {
  use StringTranslationTrait;

  /**
   * The route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

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

  /**
   * Constructs a new ProductRouteContext object.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(RouteMatchInterface $route_match, EntityTypeManagerInterface $entity_type_manager) {
    $this->routeMatch = $route_match;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function getRuntimeContexts(array $unqualified_context_ids) {
    $context_definition = new EntityContextDefinition('entity:commerce_product', NULL, FALSE);
    $value = NULL;
    if ($product = $this->routeMatch
      ->getParameter('commerce_product')) {
      $value = $product;
    }
    elseif ($product_type = $this->routeMatch
      ->getParameter('commerce_product_type')) {
      $product_storage = $this->entityTypeManager
        ->getStorage('commerce_product');
      $product_type_id = $product_type instanceof ProductTypeInterface ? $product_type
        ->id() : $product_type;
      $value = $product_storage
        ->createWithSampleValues($product_type_id);
    }
    $cacheability = new CacheableMetadata();
    $cacheability
      ->setCacheContexts([
      'route',
    ]);
    $context = new Context($context_definition, $value);
    $context
      ->addCacheableDependency($cacheability);
    return [
      'commerce_product' => $context,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailableContexts() {
    $context = new Context(new EntityContextDefinition('entity:commerce_product', $this
      ->t('Product from URL')));
    return [
      'commerce_product' => $context,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ProductRouteContext::$entityTypeManager protected property The entity type manager.
ProductRouteContext::$routeMatch protected property The route match.
ProductRouteContext::getAvailableContexts public function Gets all available contexts for the purposes of configuration. Overrides ContextProviderInterface::getAvailableContexts
ProductRouteContext::getRuntimeContexts public function Gets runtime context values for the given context IDs. Overrides ContextProviderInterface::getRuntimeContexts
ProductRouteContext::__construct public function Constructs a new ProductRouteContext object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.