You are here

function gc_gc_process_files_field in GatherContent 8.3

Same name and namespace in other branches
  1. 8 gathercontent.module \gc_gc_process_files_field()

Processing function for file type of field.

Parameters

\Drupal\node\NodeInterface $node: Object of node.

string $local_field_name: Local field name.

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 gc_gc_process_files_field()
gc_gc_process_content_pane in ./gathercontent.module
Processing function for content panes.

File

./gathercontent.module, line 738
Main module file for GatherContent module.

Code

function gc_gc_process_files_field(NodeInterface &$node, $local_field_name, $gc_field_name, $is_translatable, $language, array $files) {
  $found_files = [];

  /** @var \Drupal\field\Entity\FieldConfig $translatable_file_config */
  $translatable_file_config = $node
    ->getFieldDefinition($local_field_name);
  $translatable_file = $translatable_file_config
    ->get('third_party_settings')['content_translation']['translation_sync']['file'];
  foreach ($files as $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)) {
        if (!($node
          ->language()
          ->getId() !== $language && $translatable_file === '0')) {
          $file_dir = $node
            ->getFieldDefinition($local_field_name)
            ->getSetting('file_directory');
          $file_dir = PlainTextOutput::renderFromHtml(\Drupal::token()
            ->replace($file_dir, []));
          $uri_scheme = $node
            ->getFieldDefinition($local_field_name)
            ->getFieldStorageDefinition()
            ->getSetting('uri_scheme') . '://';
          $create_dir = \Drupal::service('file_system')
            ->realpath($uri_scheme) . '/' . $file_dir;
          file_prepare_directory($create_dir, FILE_CREATE_DIRECTORY);
          $local_file = file_save_data(file_get_contents($file->url), $uri_scheme . $file_dir . '/' . $file->filename);
          $local_file
            ->set('gc_id', $file->id)
            ->set('langcode', $language)
            ->set('filesize', $file->size);
          $found_files[] = [
            'target_id' => $local_file
              ->id(),
          ];
        }
      }
      else {
        $drupal_file = reset($drupal_files);
        $found_files[] = [
          'target_id' => $drupal_file,
        ];
      }
    }
  }
  if ($is_translatable) {
    $node
      ->getTranslation($language)
      ->set($local_field_name, $found_files);
  }
  else {
    $node
      ->set($local_field_name, $found_files);
  }
}