function commerce_file_commerce_license_access in Commerce File 8.2
Implements hook_ENTITY_TYPE_access() for licenses.
File
- ./
commerce_file.module, line 61 - Extends Commerce License with the ability to sell access to files.
Code
function commerce_file_commerce_license_access(EntityInterface $entity, $operation, AccountInterface $account) {
// Note this logic should probably live in Commerce License, but since we
// limit our logic to file licenses, it's probably fine to keep the logic here
// for the time being.
assert($entity instanceof LicenseInterface);
if ($operation !== 'view' || $entity
->bundle() !== 'commerce_file' || $entity
->getState()
->getId() !== 'active') {
return AccessResult::neutral();
}
// Grand access to file licenses if they're referenced by an order item
// the user has access to.
$order_item_storage = \Drupal::entityTypeManager()
->getStorage('commerce_order_item');
$result = $order_item_storage
->getQuery()
->condition('license', $entity
->id())
->accessCheck(FALSE)
->execute();
if (!$result) {
return AccessResult::neutral();
}
$order_item = $order_item_storage
->load(reset($result));
return AccessResult::allowedIf($order_item
->access('view', $account))
->addCacheableDependency($order_item);
}