public function LicenseFileManager::getActiveLicenses in Commerce File 8.2
Returns active licenses for the given file and the given user, optionally restricted to licenses referencing the given purchasable entity.
A file could be sold from multiple products. The user's active licenses for all of them are loaded, and the first eligible one is returned.
Parameters
\Drupal\file\FileInterface $file: The file.
\Drupal\Core\Session\AccountInterface|null $account: The account to check for. If null, the current user is used instead.
\Drupal\commerce\PurchasableEntityInterface|null $purchasable_entity: (optional) The purchasable entity.
Return value
\Drupal\commerce_license\Entity\LicenseInterface[] The active licenses for the given file if found, an empty array otherwise.
Overrides LicenseFileManagerInterface::getActiveLicenses
File
- src/
LicenseFileManager.php, line 115
Class
- LicenseFileManager
- Provides a service for managing licensed files.
Namespace
Drupal\commerce_fileCode
public function getActiveLicenses(FileInterface $file, AccountInterface $account = NULL, PurchasableEntityInterface $purchasable_entity = NULL) {
$account = $account ?: $this->currentUser;
// Static cache key of active licenses.
$cache_key_components = [
$file
->id(),
$account
->id(),
];
if ($purchasable_entity) {
$cache_key_components[] = $purchasable_entity
->id();
}
$cache_key = implode(':', $cache_key_components);
if (array_key_exists($cache_key, $this->licenses)) {
return $this->licenses[$cache_key];
}
$this->licenses[$cache_key] = [];
if ($purchasable_entity) {
$product_variation_ids = [
$purchasable_entity
->id(),
];
}
else {
/** @var \Drupal\commerce_product\ProductVariationStorageInterface $product_variation_storage */
$product_variation_storage = $this->entityTypeManager
->getStorage('commerce_product_variation');
// First, look for product variations referencing the given file.
$results = $product_variation_storage
->getQuery()
->accessCheck(FALSE)
->condition('commerce_file.target_id', $file
->id())
->execute();
if (!$results) {
return [];
}
$product_variation_ids = array_keys($results);
}
/** @var \Drupal\commerce_license\LicenseStorageInterface $license_storage */
$license_storage = $this->entityTypeManager
->getStorage('commerce_license');
$results = $license_storage
->getQuery()
->condition('type', 'commerce_file')
->condition('state', 'active')
->condition('product_variation', $product_variation_ids, 'IN')
->condition('uid', $account
->id())
->accessCheck(FALSE)
->sort('license_id')
->execute();
if (!$results) {
return [];
}
/** @var \Drupal\commerce_license\Entity\LicenseInterface[] $licenses */
$this->licenses[$cache_key] = array_values($license_storage
->loadMultiple(array_keys($results)));
return $this->licenses[$cache_key];
}