You are here

public static function MediaHelper::prepareFileDestination in Lightning Media 8

Same name and namespace in other branches
  1. 8.4 src/MediaHelper.php \Drupal\lightning_media\MediaHelper::prepareFileDestination()
  2. 8.2 src/MediaHelper.php \Drupal\lightning_media\MediaHelper::prepareFileDestination()
  3. 8.3 src/MediaHelper.php \Drupal\lightning_media\MediaHelper::prepareFileDestination()

Prepares the destination directory for a file attached to a media entity.

Parameters

\Drupal\media\MediaInterface $entity: The media entity.

Return value

string The destination directory URI.

Throws

\RuntimeException if the destination directory is not writable.

1 call to MediaHelper::prepareFileDestination()
MediaHelper::useFile in src/MediaHelper.php
Attaches a file entity to a media entity.

File

src/MediaHelper.php, line 186

Class

MediaHelper
Provides helper methods for dealing with media entities.

Namespace

Drupal\lightning_media

Code

public static function prepareFileDestination(MediaInterface $entity) {

  /** @var \Drupal\file\Plugin\Field\FieldType\FileItem $item */
  $item = static::getSourceField($entity)
    ->first();
  $dir = $item
    ->getUploadLocation();
  $is_ready = file_prepare_directory($dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  if ($is_ready) {
    return $dir;
  }
  else {
    throw new \RuntimeException('Could not prepare ' . $dir . ' for writing');
  }
}