You are here

protected function File::getFile in Feeds 8.3

Returns a file id given a url.

Parameters

string $value: A URL file object.

Return value

int The file id.

Throws

\Drupal\feeds\Exception\EmptyFeedException In case an empty file url is given.

2 calls to File::getFile()
File::prepareValue in src/Feeds/Target/File.php
Prepares a single value.
Image::prepareValue in src/Feeds/Target/Image.php
Prepares a single value.

File

src/Feeds/Target/File.php, line 176

Class

File
Defines a file field mapper.

Namespace

Drupal\feeds\Feeds\Target

Code

protected function getFile($value) {
  if (empty($value)) {

    // No file.
    throw new EmptyFeedException('The given file url is empty.');
  }

  // Perform a lookup agains the value using the configured reference method.
  if (FALSE !== ($fid = $this
    ->findEntity($this->configuration['reference_by'], $value))) {
    return $fid;
  }

  // Compose file path.
  $filepath = $this
    ->getDestinationDirectory() . '/' . $this
    ->getFileName($value);
  switch ($this->configuration['existing']) {
    case FileSystemInterface::EXISTS_ERROR:
      if (file_exists($filepath) && ($fid = $this
        ->findEntity('uri', $filepath))) {
        return $fid;
      }
      if ($file = file_save_data($this
        ->getContent($value), $filepath, FileSystemInterface::EXISTS_REPLACE)) {
        return $file
          ->id();
      }
      break;
    default:
      if ($file = file_save_data($this
        ->getContent($value), $filepath, $this->configuration['existing'])) {
        return $file
          ->id();
      }
  }

  // Something bad happened while trying to save the file to the database. We
  // need to throw an exception so that we don't save an incomplete field
  // value.
  throw new TargetValidationException($this
    ->t('There was an error saving the file: %file', [
    '%file' => $filepath,
  ]));
}