You are here

function bynder_save_image in Bynder 7

1 call to bynder_save_image()
bynder_add_submit in includes/bynder.browser.inc

File

includes/bynder.browser.inc, line 121
Manages the Bynder media browser functionality

Code

function bynder_save_image($id, $form = null) {
  try {
    $transaction = db_transaction('bynder_save_image');
    $selected_media = BynderMediaApi::getBynderApi()
      ->getObjectById($id);
    if ($selected_media['type'] == 'image') {
      $idHash = $selected_media['idHash'];
      $uri = file_stream_wrapper_uri_normalize("bynder://f/{$id}/i/{$idHash}");
      $file = file_uri_to_object($uri, TRUE);
      $file->filemime = 'image/bynder';
      $file->filesize = 0;
      $file->filename = $selected_media['name'];
      if (isset($file) && !property_exists($file, 'fid')) {
        $file = file_save($file);
        field_attach_submit('media', $file, $form, $form_state);
        $file->file = [];
        $file->file[LANGUAGE_NONE] = [];
        $file->file[LANGUAGE_NONE][0] = (array) $file + [
          'display' => TRUE,
        ];
        $file->is_new = TRUE;
        field_attach_insert('media', $file);
        bynder_create_media_entry($selected_media, $idHash, $file->fid);
        return [
          'success' => 1,
          'message' => t('Successfully added Bynder media item to Library'),
          'fid' => $file->fid,
        ];
      }
      else {
        bynder_create_media_usage_entry($selected_media, $file->fid);
        return [
          'success' => 1,
          'message' => t('Bynder media item already exists in Library'),
          'fid' => $file->fid,
        ];
      }
    }
  } catch (Exception $e) {
    $transaction
      ->rollback();
    watchdog('bynder', $e
      ->getMessage());
  }
  return [
    'message' => t("There was an unexpected error when trying to save the Bynder media item."),
  ];
}