You are here

function media_acquiadam_media_delete in Media: Acquia DAM 8

Implements hook_ENTITY_TYPE_delete().

File

./media_acquiadam.module, line 139
Integrates Drupal with Acquia DAM.

Code

function media_acquiadam_media_delete(MediaInterface $entity) {
  if ($entity
    ->getSource()
    ->getPluginId() !== 'acquiadam_asset') {
    return;
  }

  /** @var \Drupal\media_acquiadam\Service\AssetMediaFactory $asset_media_factory */
  $asset_media_factory = Drupal::service('media_acquiadam.asset_media.factory');
  $asset_id = $asset_media_factory
    ->get($entity)
    ->getAssetId();
  if (!empty($asset_id)) {
    $usages = $asset_media_factory
      ->getAssetUsage($asset_id);

    // If usages is empty then that means no media entities are referencing the
    // asset ID and it should be safe to delete the stored data.
    if (empty($usages)) {
      Drupal::service('media_acquiadam.asset_data')
        ->delete($asset_id);
    }
  }
}