You are here

function bynder_file_update in Bynder 7

Implements hook_file_update().

File

./bynder.module, line 391

Code

function bynder_file_update($file) {
  if ($file->type == 'image' && isset($file->original) && $file->original->filemime == 'image/bynder') {
    try {
      db_merge('file_managed')
        ->key([
        'fid' => $file->fid,
      ])
        ->fields([
        'filemime' => 'image/bynder',
      ])
        ->execute();
      if ($file->filename !== $file->original->filename) {
        $usageEntities = db_select('bynder_media_entity', 'b')
          ->fields('b')
          ->condition('b.fid', $file->fid, '=')
          ->execute();
        foreach ($usageEntities as $usageEntity) {
          db_merge('bynder_media_entity')
            ->key([
            'id' => $usageEntity->id,
          ])
            ->fields([
            'name' => $file->filename,
          ])
            ->execute();
        }
      }
    } catch (Exception $e) {
      drupal_set_message(t("There was an error updating the Bynder file with id: @file_id", [
        '@file_id' => $file->fid,
      ]), FALSE);
      watchdog('bynder', $e
        ->getMessage());
    }
  }
}