You are here

public function Role::grantLicense in Commerce License 8.2

Reacts to the license being activated.

The license's privileges should be granted to its user. This is called during preSave(), so values may be set on the license entity.

Parameters

\Drupal\commerce_license\Entity\LicenseInterface $license: The license entity.

Overrides LicenseTypeInterface::grantLicense

File

src/Plugin/Commerce/LicenseType/Role.php, line 47

Class

Role
Provides a license type which grants one or more roles.

Namespace

Drupal\commerce_license\Plugin\Commerce\LicenseType

Code

public function grantLicense(LicenseInterface $license) {

  // Get the role ID that this license grants.
  $role_id = $license->license_role->target_id;

  // Get the owner of the license and grant them the role.
  $owner = $license
    ->getOwner();
  if (!$owner
    ->isAnonymous()) {
    $owner
      ->addRole($role_id);
    $owner
      ->save();
  }

  // TODO: Log this, as it's something admins should see?
}