You are here

function bynder_update_media_usage_information in Bynder 7

Auxiliary function ran during cron to update all media usage information.

1 call to bynder_update_media_usage_information()
bynder_cron in ./bynder.module
Implements hook_cron().

File

./bynder.module, line 428

Code

function bynder_update_media_usage_information() {
  $mediaUsageEntryQuery = db_select('bynder_media_usage', 'b')
    ->fields('b')
    ->isNotNull('b.nid');
  $mediaUsageEntries = $mediaUsageEntryQuery
    ->execute()
    ->fetchAll();
  if (!empty($mediaUsageEntries)) {
    foreach ($mediaUsageEntries as $media) {
      $nid = $media->nid;
      $mediaId = $media->bynder_id;
      $nodeUri = url(drupal_get_path_alias("node/{$nid}"));
      $usageUrl = url($nodeUri, [
        'fragment' => $nid,
        'absolute' => TRUE,
      ]);
      try {
        $usages = BynderMediaApi::getBynderApi()
          ->getAssetUsages($mediaId);
        foreach ($usages as $usage_info) {
          $old_usage_url = $usage_info['uri'];
          $url_parts = explode('#', $old_usage_url);
          $node_info = isset($url_parts[1]) ? $url_parts[1] : null;
          if ($node_info == $nid && $usage_info['uri'] !== $usageUrl && $usage_info['integration']['id'] && BynderMediaApi::getIntegrationId()) {
            BynderMediaApi::getBynderApi()
              ->removeAssetUsage($mediaId, $usage_info['uri']);
            BynderMediaApi::getBynderApi()
              ->addAssetUsage($mediaId, $usageUrl, $usage_info['additional'], $usage_info['timestamp']);
          }
        }
      } catch (\Exception $exception) {
        watchdog('bynder', $exception
          ->getMessage());
      }
    }
  }
}