Score:1

Reference private file?

in flag

I'm making migration plugin which I'm using in first migration and it is creating private file like this:

if ($final_destination) {
  // Create a file entity.
  $file = File::create([
    'uri' => $final_destination,
    'uid' => $uid,
    'status' => FILE_STATUS_PERMANENT,
  ]);

  $uri = $file->getFileUri();

  $stream = new S3fsStream();
  $stream->writeUriToCache($uri);

  if ($stream->waitUntilFileExists($uri)) {
    $file->save();
    return $file->getFileUri();
  }
}

Then, second migration should create media entities, by using those files (with migration lookup plugin).

However, when I run second migration I get error message:

media: field_image.0=You do not have access to the referenced entity file: 46

If file is public then it works, but how can I reference private files? The error is triggered in ReferenceAccessConstraintValidator.php.

  // We check that the current user had access to view any newly added
  // referenced entity.
  if ($check_permission && !$referenced_entity->access('view')) {
    $type = $value->getFieldDefinition()->getSetting('target_type');
    $this->context->addViolation($constraint->message, ['%type' => $type, '%id' => $id]);
  }
br flag
Make sure that your user has access to that private file, otherwise you can't refer nor access it.
br flag
On the other hand, when I did the migration, I never had to create plugin to create media. But you have to set the media field to save files to the private directory (obviously). If you have an URL (even with external) you just need to set it to the file filed and the provided plugin will download it for you - if I remember well as it was long time ago.
Score:0
in flag

Found out that this was the known issue and is fixed in Drupal 9.3. With previous versions patch should be applied:

https://www.drupal.org/project/drupal/issues/3134470

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.