You are here

function bynder_delete_asset_usage_information in Bynder 7

Deletes asset usage information from Bynder.

Parameters

$fid:

null $nid:

3 calls to bynder_delete_asset_usage_information()
bynder_file_delete in ./bynder.module
Implements hook_file_delete().
bynder_node_delete in ./bynder.module
Implements hook_node_delete().
bynder_update_media_usage in ./bynder.module
Updates the media usage entry in the database with the node information.

File

./bynder.module, line 346

Code

function bynder_delete_asset_usage_information($fid, $nid = null) {
  try {
    $mediaUsageEntryQuery = db_select('bynder_media_usage', 'b')
      ->fields('b')
      ->condition('b.fid', $fid, '=');
    if (isset($nid)) {
      $mediaUsageEntryQuery
        ->condition('nid', $nid);
    }
    $mediaUsageEntry = $mediaUsageEntryQuery
      ->execute()
      ->fetchAll();
    if (!empty($mediaUsageEntry)) {
      $delete_query = db_delete('bynder_media_usage')
        ->condition('fid', $fid);
      $usage_url = '';
      if (isset($nid)) {
        $usageEntryId = $mediaUsageEntry[0]->id;
        $nodeUri = drupal_get_path_alias("node/" . $nid);
        $usage_url = url($nodeUri, [
          'fragment' => $usageEntryId,
          'absolute' => true,
        ]);
        $delete_query
          ->condition('nid', $nid)
          ->condition('uri', $usage_url);
      }
      $delete_query
        ->execute();
      BynderMediaApi::getBynderApi()
        ->removeAssetUsage($mediaUsageEntry[0]->bynder_id, $usage_url);
    }
  } catch (Exception $e) {
    drupal_set_message(t('An error ocurred when deleting the asset usage entry for file with id @file_id.', [
      '@file_id' => $fid,
    ]), 'error');
    watchdog('bynder', $e
      ->getMessage());
  }
}