You are here

private function ContextCreatorTrait::getContextDetails in Commerce Stock 8

Get context details.

Parameters

\Drupal\commerce\PurchasableEntityInterface $entity: The purchasable entity.

Return value

\Drupal\commerce\Context The Stock service context.

Throws

\Exception When the entity can't be purchased from the current store.

Original logic comes from this function.

See also

\Drupal\commerce_cart\Form\AddToCartForm::selectStore()

2 calls to ContextCreatorTrait::getContextDetails()
ContextCreatorTrait::getContext in src/ContextCreatorTrait.php
Returns the active commerce context.
ContextCreatorTrait::isValidContext in src/ContextCreatorTrait.php
Checks that the context returned is valid for $entity.

File

src/ContextCreatorTrait.php, line 67

Class

ContextCreatorTrait
Provides trait to create a commerce context object from a purchasable entity.

Namespace

Drupal\commerce_stock

Code

private function getContextDetails(PurchasableEntityInterface $entity) {

  // Make sure the current store is in the entity stores.
  $stores = $entity
    ->getStores();
  if (count($stores) === 1) {
    $store = reset($stores);
  }
  elseif (count($stores) === 0) {

    // Malformed entity.
    throw new \Exception('The given entity is not assigned to any store.');
  }
  else {
    foreach ($stores as $store) {
      $store_ids[] = $store
        ->id();
    }

    /** @var \Drupal\commerce_store\CurrentStore $currentStore */
    $currentStore = \Drupal::service('commerce_store.current_store');
    $store = $currentStore
      ->getStore();
    if (!in_array($store
      ->id(), $store_ids)) {

      // Indicates that the site listings are not filtered properly.
      throw new \Exception("The given entity can't be purchased from the current store.");
    }
  }
  $currentUser = \Drupal::currentUser();
  return new Context($currentUser, $store);
}