You are here

protected function ContentProcessor::processFilesField in GatherContent 8.4

Processing function for file type of field.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: Object of node.

\Drupal\field\Entity\FieldConfig $field_info: Local field Info object.

string $gc_field_name: Name of field in GatherContent.

bool $is_translatable: Indicator if node is translatable.

string $language: Language of translation if applicable.

array $files: Array of remote files.

1 call to ContentProcessor::processFilesField()
ContentProcessor::processContentPane in src/Import/ContentProcess/ContentProcessor.php
Processing function for content panes.

File

src/Import/ContentProcess/ContentProcessor.php, line 704

Class

ContentProcessor
The ContentProcessor sets the necessary fields of the entity.

Namespace

Drupal\gathercontent\Import\ContentProcess

Code

protected function processFilesField(EntityInterface &$entity, FieldConfig $field_info, $gc_field_name, $is_translatable, $language, array $files) {
  $found_files = [];
  $local_field_name = $field_info
    ->getName();

  /** @var \Drupal\field\Entity\FieldConfig $translatable_file_config */
  $translatable_file_config = $entity
    ->getFieldDefinition($local_field_name);
  $third_party_settings = $translatable_file_config
    ->get('third_party_settings');
  if (isset($third_party_settings['content_translation'])) {
    $translatable_file = $third_party_settings['content_translation']['translation_sync']['file'];
  }
  else {
    $translatable_file = NULL;
  }
  foreach ($files as $key => $file) {
    if ($file->field === $gc_field_name) {
      $drupal_files = \Drupal::entityQuery('file')
        ->condition('gc_id', $file->id)
        ->condition('filename', $file->fileName)
        ->execute();
      if (!empty($drupal_files)) {
        $drupal_file = reset($drupal_files);
        $found_files[] = [
          'target_id' => $drupal_file,
        ];
        unset($files[$key]);
      }
    }
    else {
      unset($files[$key]);
    }
  }
  if (!($entity
    ->language()
    ->getId() !== $language && $translatable_file === '0') && !empty($files)) {
    $file_dir = $translatable_file_config
      ->getSetting('file_directory');
    $file_dir = PlainTextOutput::renderFromHtml(\Drupal::token()
      ->replace($file_dir, []));
    $uri_scheme = $translatable_file_config
      ->getFieldStorageDefinition()
      ->getSetting('uri_scheme') . '://';
    $create_dir = \Drupal::service('file_system')
      ->realpath($uri_scheme) . '/' . $file_dir;
    file_prepare_directory($create_dir, FILE_CREATE_DIRECTORY);
    $imported_files = $this->client
      ->downloadFiles($files, $uri_scheme . $file_dir, $language);
    if (!empty($imported_files)) {
      foreach ($imported_files as $file) {
        $found_files[] = [
          'target_id' => $file,
        ];
      }
      if ($is_translatable) {
        $entity
          ->getTranslation($language)
          ->set($local_field_name, end($found_files));
      }
      else {
        $entity
          ->set($local_field_name, end($found_files));
      }
    }
  }
}