You are here

public function MediaEntityHelper::getFile in Media: Acquia DAM 8

Returns an associated file or creates a new one.

Return value

false|\Drupal\file\FileInterface A file entity or FALSE on failure.

File

src/MediaEntityHelper.php, line 82

Class

MediaEntityHelper
Class MediaEntityHelper.

Namespace

Drupal\media_acquiadam

Code

public function getFile() {

  // If there is already a file on the media entity then we should use that.
  $file = $this
    ->getExistingFile();

  // If we're getting an updated version of the asset we need to grab a new
  // version of the file.
  $asset = $this
    ->getAsset();
  if (empty($asset)) {
    return FALSE;
  }
  $is_different_version = $this->assetData
    ->isUpdatedAsset($asset);
  if (!empty($asset) && (empty($file) || $is_different_version)) {
    $destination_folder = $this
      ->getAssetFileDestination();
    $file = $this->assetFileHelper
      ->createNewFile($asset, $destination_folder);
    if ($file) {
      $this->assetData
        ->set($asset->id, 'version', $asset->version);
    }
  }
  return $file;
}