MediaFileCopy should rely on ModuleHandlerInterface, not ModuleHandler

Created on 16 August 2023, 11 months ago
Updated 14 September 2023, 10 months ago

Problem/Motivation

Since MediaFileCopy is expecting a ModuleHandler, it throws an error when used with the hook_event_dispatcher module. Because hook_event_dispatcher overrides the "module_handler" service with a Drupal\hook_event_dispatcher\HookEventDispatcherModuleHandler.

Steps to reproduce

Enable hook_event_dispatcher.

Run this migration:

# File to Media Migration configuration for  migration

id: hcpss_gallery_media_step1
label:  Media
migration_group: media
source:
  key: migrate

  plugin: media_entity_generator_d7
  entity_type: node
  bundle: gallery
  d7_file_url: 'http://localhost:8080/sites/default/files/'
  langcode: null
  source_langcode: en
  field_names:
    - field_gallery_image

destination:
  plugin: entity:media

process:
  bundle:
    plugin: default_value
    default_value: image

  # Using the alt tag if available, else the file name.
  name:
    plugin: media_name
    source: file_name

  # Map the field image
  field_media_image/alt: alt
  field_media_image/title: title
  field_media_image/target_id:
    plugin: media_file_copy
    move: FALSE
    reuse: TRUE
    path: 'public://media/galleries/'
    source: file_path
  uid:
    plugin: default_value
    default_value: 1
dependencies:
  module:
    - hcpss_migration
  enforced:
    module:
      - hcpss_migration

and you will get this error:

[error]  TypeError: Drupal\migrate_file_to_media\Plugin\migrate\process\MediaFileCopy::__construct(): Argument #7 ($module_handler) must be of type Drupal\Core\Extension\ModuleHandler, Drupal\hook_event_dispatcher\HookEventDispatcherModuleHandler given, called in /var/www/drupal/web/modules/contrib/migrate_file_to_media/src/Plugin/migrate/process/MediaFileCopy.php on line 93 in Drupal\migrate_file_to_media\Plugin\migrate\process\MediaFileCopy->__construct() (line 73 of /var/www/drupal/web/modules/contrib/migrate_file_to_media/src/Plugin/migrate/process/MediaFileCopy.php) #0 /var/www/drupal/web/modules/contrib/migrate_file_to_media/src/Plugin/migrate/process/MediaFileCopy.php(93): Drupal\migrate_file_to_media\Plugin\migrate\process\MediaFileCopy->__construct(Array, 'media_file_copy', Array, Object(Drupal\Core\StreamWrapper\StreamWrapperManager), Object(Drupal\Core\File\FileSystem), Object(Drupal\migrate\Plugin\migrate\process\Download), Object(Drupal\hook_event_dispatcher\HookEventDispatcherModuleHandler), Object(Drupal\file\FileRepository), Object(Drupal\Core\Entity\EntityTypeManager))
#1 /var/www/drupal/web/core/modules/migrate/src/Plugin/MigratePluginManager.php(57): Drupal\migrate_file_to_media\Plugin\migrate\process\MediaFileCopy::create(Object(Drupal\Core\DependencyInjection\Container), Array, 'media_file_copy', Array, Object(Drupal\migrate\Plugin\Migration))
#2 /var/www/drupal/web/core/modules/migrate/src/Plugin/Migration.php(432): Drupal\migrate\Plugin\MigratePluginManager->createInstance('media_file_copy', Array, Object(Drupal\migrate\Plugin\Migration))
#3 /var/www/drupal/web/core/modules/migrate/src/MigrateExecutable.php(200): Drupal\migrate\Plugin\Migration->getProcessPlugins()
#4 /var/www/drupal/vendor/drush/drush/includes/drush.inc(122): Drupal\migrate\MigrateExecutable->import()
#5 /var/www/drupal/vendor/drush/drush/includes/drush.inc(113): drush_call_user_func_array(Array, Array)
#6 /var/www/drupal/web/modules/contrib/migrate_tools/src/Drush/MigrateToolsCommands.php(1070): drush_op(Array)
#7 /var/www/drupal/web/modules/contrib/migrate_tools/src/Drush/MigrateToolsCommands.php(481): Drupal\migrate_tools\Drush\MigrateToolsCommands->executeMigration(Object(Drupal\migrate\Plugin\Migration), 'hcpss_gallery_m...', Array)
#8 [internal function]: Drupal\migrate_tools\Drush\MigrateToolsCommands->import('hcpss_school_se...', Array)
#9 /var/www/drupal/vendor/consolidation/annotated-command/src/CommandProcessor.php(276): call_user_func_array(Array, Array)
#10 /var/www/drupal/vendor/consolidation/annotated-command/src/CommandProcessor.php(212): Consolidation\AnnotatedCommand\CommandProcessor->runCommandCallback(Array, Object(Consolidation\AnnotatedCommand\CommandData))
#11 /var/www/drupal/vendor/consolidation/annotated-command/src/CommandProcessor.php(176): Consolidation\AnnotatedCommand\CommandProcessor->validateRunAndAlter(Array, Array, Object(Consolidation\AnnotatedCommand\CommandData))
#12 /var/www/drupal/vendor/consolidation/annotated-command/src/AnnotatedCommand.php(391): Consolidation\AnnotatedCommand\CommandProcessor->process(Object(Symfony\Component\Console\Output\ConsoleOutput), Array, Array, Object(Consolidation\AnnotatedCommand\CommandData))
#13 /var/www/drupal/vendor/symfony/console/Command/Command.php(326): Consolidation\AnnotatedCommand\AnnotatedCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#14 /var/www/drupal/vendor/symfony/console/Application.php(1081): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#15 /var/www/drupal/vendor/symfony/console/Application.php(320): Symfony\Component\Console\Application->doRunCommand(Object(Consolidation\AnnotatedCommand\AnnotatedCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#16 /var/www/drupal/vendor/symfony/console/Application.php(174): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#17 /var/www/drupal/vendor/drush/drush/src/Runtime/Runtime.php(124): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#18 /var/www/drupal/vendor/drush/drush/src/Runtime/Runtime.php(51): Drush\Runtime\Runtime->doRun(Array, Object(Symfony\Component\Console\Output\ConsoleOutput))
#19 /var/www/drupal/vendor/drush/drush/drush.php(79): Drush\Runtime\Runtime->run(Array)
#20 /var/www/drupal/vendor/drush/drush/drush(4): require('/var/www/drupal...')
#21 /var/www/drupal/vendor/bin/drush(120): include('/var/www/drupal...')
#22 {main}. 

Proposed resolution

Use ModuleHandlerInterface instead of ModuleHandler

Remaining tasks

User interface changes

none

API changes

Data model changes

πŸ› Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States bander2

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

Comments & Activities

Production build 0.69.0 2024