You are here

public function AssetFileEntityHelper::getDestinationFromEntity in Media: Acquia DAM 8

Get a destination uri from the given entity and field combo.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity to check the field configuration on.

string $fileField: The name of the file field.

Return value

string The uri to use. Defaults to public://acquiadam_assets

File

src/Service/AssetFileEntityHelper.php, line 167

Class

AssetFileEntityHelper
Class AssetFileEntityHelper.

Namespace

Drupal\media_acquiadam\Service

Code

public function getDestinationFromEntity(EntityInterface $entity, $fileField) {
  $scheme = $this->configFactory
    ->get('system.file')
    ->get('default_scheme');
  $file_directory = 'acquiadam_assets';

  // Load the field definitions for this bundle.
  $field_definitions = $this->entityFieldManager
    ->getFieldDefinitions($entity
    ->getEntityTypeId(), $entity
    ->bundle());
  if (!empty($field_definitions[$fileField])) {
    $definition = $field_definitions[$fileField]
      ->getItemDefinition();
    $scheme = $definition
      ->getSetting('uri_scheme');
    $file_directory = $definition
      ->getSetting('file_directory');
  }

  // Replace the token for file directory.
  if (!empty($file_directory)) {
    $file_directory = $this->token
      ->replace($file_directory);
  }
  return sprintf('%s://%s', $scheme, $file_directory);
}