You are here

public function LicenseStorage::createFromProductVariation in Commerce License 8.2

Creates a new license from a product variation.

Parameters

\Drupal\commerce_product\Entity\ProductVariationInterface $variation: The product variation. Values for the license will be taken from the license_type and license_expiration fields.

int $uid: The uid for whom the license will be created.

Return value

\Drupal\commerce_license\Entity\LicenseInterface A new, unsaved license entity, whose state is 'new'.

Overrides LicenseStorageInterface::createFromProductVariation

1 call to LicenseStorage::createFromProductVariation()
LicenseStorage::createFromOrderItem in src/LicenseStorage.php
Creates a new license from an order item.

File

src/LicenseStorage.php, line 38

Class

LicenseStorage
Defines the storage handler class for License entities.

Namespace

Drupal\commerce_license

Code

public function createFromProductVariation(ProductVariationInterface $variation, $uid) {

  // TODO: throw an exception if the variation doesn't have this field.
  $license_type_plugin = $variation
    ->get('license_type')
    ->first()
    ->getTargetInstance();
  $license = $this
    ->create([
    'type' => $license_type_plugin
      ->getPluginId(),
    'state' => 'new',
    'product_variation' => $variation
      ->id(),
    'uid' => $uid,
    // Take the expiration type configuration from the product variation
    // expiration field.
    'expiration_type' => $variation->license_expiration,
  ]);

  // Set the license's plugin-specific configuration from the
  // product variation's license_type field plugin instance.
  $license
    ->setValuesFromPlugin($license_type_plugin);
  return $license;
}