You are here

class ProductVariationCreateAccessCheck in Commerce Core 8.2

Defines an access checker for product variation creation.

Takes the product variation type ID from the product type, since a product is always present in variation routes.

Hierarchy

Expanded class hierarchy of ProductVariationCreateAccessCheck

1 string reference to 'ProductVariationCreateAccessCheck'
commerce_product.services.yml in modules/product/commerce_product.services.yml
modules/product/commerce_product.services.yml
1 service uses ProductVariationCreateAccessCheck
access_check.product_variation_create in modules/product/commerce_product.services.yml
Drupal\commerce_product\Access\ProductVariationCreateAccessCheck

File

modules/product/src/Access/ProductVariationCreateAccessCheck.php, line 18

Namespace

Drupal\commerce_product\Access
View source
class ProductVariationCreateAccessCheck implements AccessInterface {

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

  /**
   * Constructs a new ProductVariationCreateAccessCheck object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Checks access to create the product variation.
   *
   * @param \Symfony\Component\Routing\Route $route
   *   The route to check against.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The currently logged in account.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {

    /** @var \Drupal\commerce_product\Entity\ProductInterface $product */
    $product = $route_match
      ->getParameter('commerce_product');
    if (!$product) {
      return AccessResult::forbidden();
    }
    $access_control_handler = $this->entityTypeManager
      ->getAccessControlHandler('commerce_product_variation');
    $product_type_storage = $this->entityTypeManager
      ->getStorage('commerce_product_type');

    /** @var \Drupal\commerce_product\Entity\ProductTypeInterface $product_type */
    $product_type = $product_type_storage
      ->load($product
      ->bundle());
    $variation_type_id = $product_type
      ->getVariationTypeId();
    return $access_control_handler
      ->createAccess($variation_type_id, $account, [], TRUE);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ProductVariationCreateAccessCheck::$entityTypeManager protected property The entity type manager.
ProductVariationCreateAccessCheck::access public function Checks access to create the product variation.
ProductVariationCreateAccessCheck::__construct public function Constructs a new ProductVariationCreateAccessCheck object.