MediaFileCopy plugin 'path' (destination) vary per row/entry processed

Created on 6 May 2019, over 5 years ago
Updated 20 May 2023, over 1 year ago

I was trying to work through an example migration using the MediaFileCopy plugin.

Use case is that file will have different destination path based on where they were saved in the previous site (see JSON data example at bottom).

When configuring the the 'path' property it was not allowing dynamic values (this maybe by original design).

  # Example Code Provided:
  # Map the field image
  field_image_media/target_id: target_id
  field_media_image/target_id:
    plugin: media_file_copy
    move: FALSE
    reuse: TRUE
    path: 'rokka://'
    source: target_id

  # What I want to do
  field_media_image/target_id:
    plugin: media_file_copy
    move: FALSE
    reuse: TRUE
    path: path_destination
    source: target_id 

Original Module Code

class MediaFileCopy extends FileCopy implements ContainerFactoryPluginInterface {

  /**
   * {@inheritdoc}
   */
  public function transform($source_id, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    // If we're stubbing a file entity, return a URI of NULL so it will get
    // stubbed by the general process.
    if ($row->isStub()) {
      return NULL;
    }

    $destination_folder = $this->configuration['path'] ?? 'public://media/';

    $destination = $destination_folder . $row->getSourceProperty('file_name');

How I ended up patching it to get it to work:

   if(empty($destination_folder = $row->getSourceProperty('path_destination'))){
      $destination_folder = $this->configuration['path'] ?? 'public://media/';
    }
  # Removed path since I am adding a lookup if the source row has path_destination
  field_media_image/target_id:
    plugin: media_file_copy
    move: FALSE
    reuse: TRUE
    source: target_id 

Source Data Example Entry (Psued:

 [
    {
      "file_name": "myimage.png",
      "image_path": "public://images/sub_path/my_image.png",
      "alt_text": "",
      "media_folder": "sub_path",
      "image_url": "https://example.com/sites/default/files/images/sub_path/my_image.png",
      "uuid": "a0003e97-1234-4dbd-a705-123cccbdfaea",
      "path_destination": "public://images/sub_path/"
    },
    {
      "file_name": "myimage2.png",
      "image_path": "public://images/sub_path2/my_image2.png",
      "alt_text": "",
      "media_folder": "sub_path2",
      "image_url": "https://example.com/sites/default/files/images/sub_path2/my_image2.png",
      "uuid": "a0003e97-1234-4dbd-a705-123cccbdfa22",
      "path_destination": "public://images/sub_path2/"
    },
   ...

I haven't worked through enough of the migration plugin if there would be a better way to handle this possibly passing a 'path_key' or some sort of process.

It is also possible I could be not groking process plugins correctly -- I have only dug into migrations a couple days ago.

✨ Feature request
Status

Closed: outdated

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States RecursiveMeta

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024