You are here

function media_acquiadam_get_helper in Media: Acquia DAM 7

Asset/folder get helper that can load and store assets to cache.

Parameters

string $type: The type of asset to try and load (folder, asset).

int|array $id: An asset ID or the asset data.

array $depends: An array of dependencies to pass to the class constructors.

bool $skipCache: TRUE to skip the cache mechanism.

Return value

AcquiaDAM_Assets_AbstractAsset|false A AcquiaDAM_Assets_AbstractAsset object or FALSE.

5 calls to media_acquiadam_get_helper()
AcquiaDAM_Assets_Folder::getAssets in src/AcquiaDAM/AcquiaDAM_Assets_Folder.inc
Get assets located in the folder.
media_acquiadam_asset_load in ./media_acquiadam.module
Load an Acquia DAM asset.
media_acquiadam_get_asset in includes/media_acquiadam.helpers.inc
Load an Asset.
media_acquiadam_get_folder in includes/media_acquiadam.helpers.inc
Load a Folder asset.
theme_media_acquiadam_browser_trail in modules/media_acquiadam_browser/includes/media_acquiadam_browser.theme.inc
Theme implementation for the media browser folder trail.

File

includes/media_acquiadam.helpers.inc, line 59
Helper functions for working with Acquia DAM within Drupal.

Code

function media_acquiadam_get_helper($type, $id, array $depends = [], $skipCache = FALSE) {

  // We only want to try and cache when we are given an id instead of an array.
  // We also need to ignore caching the root folder.
  $use_cache = empty($skipCache) && is_numeric($id) && $id != AcquiaDAM_Assets_Folder::ROOT_FOLDER_ID;
  if ($use_cache) {
    $cached_item = media_acquiadam_cache_get($id);
  }
  $create_arg = empty($cached_item) ? $id : $cached_item;
  if (empty($depends['client']) && user_is_anonymous()) {

    // Force the background user mode when serving files to anonymous users.
    $depends['client'] = AcquiaDAM_API::getClient('acquiadam-user-auth');
  }
  $result = FALSE;
  try {
    if ('folder' == $type) {
      $result = new AcquiaDAM_Assets_Folder($create_arg, $depends);
    }
    elseif ('asset' == $type) {
      $result = new AcquiaDAM_Assets_Asset($create_arg, $depends);
    }
    else {
      watchdog('media_acquiadam', 'Unhandled asset type: @type', [
        '@type' => $type,
      ], WATCHDOG_DEBUG);
    }
  } catch (Exception $x) {
    watchdog_exception('media_acquiadam', $x);
  }
  $can_cache_result = !empty($result) && empty($cached_item);
  if ($use_cache && $can_cache_result) {
    media_acquiadam_cache_set($result);
  }
  return $result;
}