Implement duplicate detection as a service

Created on 14 June 2024, 5 months ago
Updated 19 June 2024, 5 months ago

It would be useful if the duplicateFileDetection and duplicateMediaImageDetection methods were implemented as services. That would allow you to create a custom event subscriber on the MigrateEvents::PRE_IMPORT event to automatically run duplicate file detection before a migration runs.

Feature request
Status

Needs review

Version

2.0

Component

Code

Created by

🇪🇸Spain Peacog

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

Comments & Activities

  • Issue created by @Peacog
  • 🇪🇸Spain Peacog

    Peacog changed the visibility of the branch 3454659-duplicate-detection-service to hidden.

  • 🇪🇸Spain Peacog

    Peacog changed the visibility of the branch 3454659-duplicate-detection-service to active.

  • 🇪🇸Spain Peacog

    Peacog changed the visibility of the branch 3454659-duplicate-detection-service to hidden.

  • 🇪🇸Spain Peacog

    Peacog changed the visibility of the branch 3454659-implement-duplicate-detection to hidden.

  • 🇪🇸Spain Peacog

    Peacog changed the visibility of the branch 3454659-implement-duplicate-detection to active.

  • 🇪🇸Spain Peacog

    Peacog changed the visibility of the branch 2.0.x to hidden.

  • @peacog opened merge request.
  • Status changed to Needs review 5 months ago
  • 🇪🇸Spain Peacog

    Here's an example of the custom pre-import event that uses the service in this patch.

    <?php
    
    namespace Drupal\my_custom_migrate\EventSubscriber;
    
    use Drupal\migrate\Event\MigrateImportEvent;
    use Drupal\migrate_file_to_media\Services\DuplicateDetectionService;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    use Drupal\migrate\Event\MigrateEvents;
    
    /**
     * Custom migrate event subscriber.
     */
    class MyCustomMigrateSubscriber implements EventSubscriberInterface {
      /**
       * The duplicate file detection service.
       *
       * @var \Drupal\migrate_file_to_media\Services\DuplicateDetectionService
       */
      protected DuplicateDetectionService $duplicateDetectionService;
    
      /**
       * Constructs a MyCustomMigrateSubscriber object.
       *
       * @param \Drupal\migrate_file_to_media\Services\DuplicateDetectionService $duplicateDetectionService
       *   The search links service.
       */
      public function __construct(DuplicateDetectionService $duplicateDetectionService) {
        $this->duplicateDetectionService = $duplicateDetectionService;
      }
    
      /**
       * Run migrate:duplicate-file-detection before migrate_file_to_media
       * migrations.
       **
       * @param \Drupal\migrate\Event\MigrateImportEvent $event
       *   Response event.
       */
      public function onPreImport(MigrateImportEvent $event) {
        $config = $event->getMigration()->getPluginDefinition();
    
        if (($config['source']['plugin'] ?? '') !== 'media_entity_generator_d7') {
          return;
        }
    
        // Run the migrate:duplicate-file-detection command for the migration.
        $this->duplicateDetectionService->duplicateFileDetection($config['id'], [FALSE, TRUE]);
      }
    
      /**
       * {@inheritdoc}
       */
      public static function getSubscribedEvents() {
        return [
          MigrateEvents::PRE_IMPORT => ['onPreImport'],
        ];
      }
    
    }
    
    
Production build 0.71.5 2024