Add description in file_import

Created on 23 April 2019, over 5 years ago
Updated 26 September 2023, about 1 year ago
🐛 Bug report
Status

Active

Version

1.1

Component

Code

Created by

🇹🇳Tunisia Riadh Rahmi

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.

  • 🇧🇪Belgium michiellucas

    Already available

    field_files:
    plugin: file_import
    source: uri
    destination: file_destination
    uid: uid
    skip_on_missing_source: true
    field_files/description: description

  • 🇮🇹Italy nessunluogo Tremestieri Etneo (CT)

    @michiellucas, can you please format the code? Or add some documentation?

    field_files:
      plugin: file_import
      source: uri
      destination: file_destination
      uid: uid
      skip_on_missing_source: true
    field_files/description: description
    

    Is this the right syntax?

  • 🇮🇹Italy nessunluogo Tremestieri Etneo (CT)

    After a while I made my own solution since #5 won't work with a multi-valued field.

    In my import process section I managed to get two arrays one for remote URLs and one for file descriptions:

    _attachments_urls_array = [
      0 => 'https://oldsite.com/path/to/files/attachment_1.pdf',
      1 => 'https://oldsite.com/path/to/files/attachment_2.zip',
      2 => 'https://oldsite.com/path/to/files/attachment_3.mp3',
    ];
    
    _attachments_labels_array = [
      0: 'My PDF document',
      1: 'My ZIP archive',
      2: 'My song',
    ]
    

    First one is the source of my field_attachments multivalue file field working nice using file_import plugin from Migrate files (extended) module.

    To apply file descriptions I figured how to patch the module but changes were too many and I walked to another solution.

    What I did is an implementation of the PRE_ROW_SAVE event (ref. MigrateEvents).

    Of course you need a custom module but you can use the one where you have your migration configurations.

    First add a custom event subscriber (src/EventSubscriber/YourModuleSubscriber.php):

    namespace Drupal\your_module\EventSubscriber;
    
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    use Drupal\migrate\Event\MigrateEvents;
    use Drupal\migrate\Event\MigrateImportEvent;
    use Drupal\migrate\Event\MigratePreRowSaveEvent;
    
    /**
     * Your Migrations event subscriber.
     */
    class YourModuleSubscriber implements EventSubscriberInterface {
    
      /**
       * {@inheritdoc}
       */
      public static function getSubscribedEvents() {
        return [
          MigrateEvents::PRE_ROW_SAVE => ['onMigratePreRowSave'],
        ];
      }
    
      /**
       * Custom migration comtuting.
       *
       * @param \Drupal\migrate\Event\MigratePreRowSaveEvent $event
       *   The import event object.
       */
      public function onMigratePreRowSave(MigratePreRowSaveEvent $event) {
        $migration_id = $event->getMigration()->getBaseId();
    
        switch ($migration_id) {
          case 'your_migration_id':
            $row = $event->getRow();
            $destination = $row->getDestination();
            if (isset($destination['field_attachments'])) {
              $field_attachments = $destination['field_attachments'];
              foreach ($field_attachments as $index => $attachment) {
                if (isset($destination['_attachments_labels_array'][$index])) {
                  $field_attachments[$index]['description'] = $destination['_attachments_labels_array'][$index];
                }
              }
              $row->setDestinationProperty('field_attachments', $field_attachments);
            }
            break;
          default:
            break;
        }
      }
    
    }
    

    Then add the related service in your module services file (create one if you don't have one) your_module.services.yml:

    services:
      your_module.event_subscriber:
        class: Drupal\your_module\EventSubscriber\YourModuleSubscriber
        tags:
          - { name: event_subscriber }
    

    I hope this helps.

Production build 0.71.5 2024