You are here

function commerce_file_entity_presave in Commerce File 8.2

Implements hook_entity_presave().

File

./commerce_file.module, line 181
Extends Commerce License with the ability to sell access to files.

Code

function commerce_file_entity_presave(EntityInterface $entity) {
  $entity_type_id = $entity
    ->getEntityTypeId();
  if (!in_array($entity_type_id, [
    'commerce_license',
    'commerce_product_variation',
  ])) {
    return;
  }
  assert($entity instanceof ContentEntityInterface);
  $clear_cache = FALSE;

  // When a product variation or a file license gets saved, clear the license
  // file manager static cache.
  if ($entity_type_id === 'commerce_product_variation' && $entity
    ->hasField('commerce_file')) {
    $clear_cache = TRUE;
  }
  elseif ($entity_type_id === 'commerce_license' && $entity
    ->bundle() === 'commerce_file') {
    $clear_cache = TRUE;
  }
  if ($clear_cache) {

    /** @var \Drupal\commerce_file\LicenseFileManagerInterface $license_file_manager */
    $license_file_manager = \Drupal::service('commerce_file.license_file_manager');
    $license_file_manager
      ->resetCache();
  }
}