Problem/Motivation

I am performing a migration with the migrate module, I have configured a migration of files that are assigned to another migration of media entities. When migrating the media entities occurs the following error:

[error]  Error: Call to a member function getType() on bool in filefield_paths_filefield_paths_process_file() (line 75 of /var/www/html/web/modules/contrib/filefield_paths/filefield_paths.inc) #0 /var/www/html/web/modules/contrib/hook_event_dispatcher/src/HookEventDispatcherModuleHandler.php(52): filefield_paths_filefield_paths_process_file(Object(Drupal\media\Entity\Media), Object(Drupal\file\Plugin\Field\FieldType\FileFieldItemList), Array)
#1 /var/www/html/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(388): Drupal\hook_event_dispatcher\HookEventDispatcherModuleHandler::Drupal\hook_event_dispatcher\{closure}(Object(Closure), 'filefield_paths')
#2 /var/www/html/web/modules/contrib/hook_event_dispatcher/src/HookEventDispatcherModuleHandler.php(67): Drupal\Core\Extension\ModuleHandler->invokeAllWith('filefield_paths...', Object(Closure))
#3 /var/www/html/web/modules/contrib/hook_event_dispatcher/src/HookEventDispatcherModuleHandler.php(51): Drupal\hook_event_dispatcher\HookEventDispatcherModuleHandler->invokeAllWith('filefield_paths...', Object(Closure))
#4 /var/www/html/web/modules/contrib/filefield_paths/filefield_paths.module(352): Drupal\hook_event_dispatcher\HookEventDispatcherModuleHandler->invokeAll('filefield_paths...', Array)
#5 /var/www/html/web/modules/contrib/filefield_paths/filefield_paths.module(337): filefield_paths_entity_update(Object(Drupal\media\Entity\Media))
#6 /var/www/html/web/modules/contrib/hook_event_dispatcher/src/HookEventDispatcherModuleHandler.php(52): filefield_paths_entity_insert(Object(Drupal\media\Entity\Media))
#7 /var/www/html/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(388): Drupal\hook_event_dispatcher\HookEventDispatcherModuleHandler::Drupal\hook_event_dispatcher\{closure}(Object(Closure), 'filefield_paths')
#8 /var/www/html/web/modules/contrib/hook_event_dispatcher/src/HookEventDispatcherModuleHandler.php(67): Drupal\Core\Extension\ModuleHandler->invokeAllWith('entity_insert', Object(Closure))
#9 /var/www/html/web/modules/contrib/hook_event_dispatcher/src/HookEventDispatcherModuleHandler.php(51): Drupal\hook_event_dispatcher\HookEventDispatcherModuleHandler->invokeAllWith('entity_insert', Object(Closure))
#10 /var/www/html/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php(217): Drupal\hook_event_dispatcher\HookEventDispatcherModuleHandler->invokeAll('entity_insert', Array)
#11 /var/www/html/web/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php(900): Drupal\Core\Entity\EntityStorageBase->invokeHook('insert', Object(Drupal\media\Entity\Media))
#12 /var/www/html/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php(564): Drupal\Core\Entity\ContentEntityStorageBase->invokeHook('insert', Object(Drupal\media\Entity\Media))
#13 /var/www/html/web/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php(781): Drupal\Core\Entity\EntityStorageBase->doPostSave(Object(Drupal\media\Entity\Media), false)
#14 /var/www/html/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php(489): Drupal\Core\Entity\ContentEntityStorageBase->doPostSave(Object(Drupal\media\Entity\Media), false)
#15 /var/www/html/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php(806): Drupal\Core\Entity\EntityStorageBase->save(Object(Drupal\media\Entity\Media))
#16 /var/www/html/web/core/modules/media/src/MediaStorage.php(29): Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object(Drupal\media\Entity\Media))
#17 /var/www/html/web/core/lib/Drupal/Core/Entity/EntityBase.php(354): Drupal\media\MediaStorage->save(Object(Drupal\media\Entity\Media))
#18 /var/www/html/web/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php(237): Drupal\Core\Entity\EntityBase->save()
#19 /var/www/html/web/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php(175): Drupal\migrate\Plugin\migrate\destination\EntityContentBase->save(Object(Drupal\media\Entity\Media), Array)
#20 /var/www/html/web/core/modules/migrate/src/MigrateExecutable.php(248): Drupal\migrate\Plugin\migrate\destination\EntityContentBase->import(Object(Drupal\migrate\Row), Array)
#21 /var/www/html/vendor/drush/drush/includes/drush.inc(62): Drupal\migrate\MigrateExecutable->import()
#22 /var/www/html/vendor/drush/drush/includes/drush.inc(53): drush_call_user_func_array(Array, Array)
#23 /var/www/html/vendor/drush/drush/src/Commands/core/MigrateRunnerCommands.php(421): drush_op(Array)
#24 [internal function]: Drush\Commands\core\MigrateRunnerCommands->executeMigration(Object(Drupal\migrate\Plugin\Migration), 'reina_migrate_a...', Array)
#25 /var/www/html/vendor/drush/drush/src/Commands/core/MigrateRunnerCommands.php(401): array_walk(Array, Array, Array)
#26 [internal function]: Drush\Commands\core\MigrateRunnerCommands->executeMigration(Object(Drupal\migrate\Plugin\Migration), 'reina_migrate_a...', Array)
#27 /var/www/html/vendor/drush/drush/src/Commands/core/MigrateRunnerCommands.php(371): array_walk(Array, Array, Array)
#28 [internal function]: Drush\Commands\core\MigrateRunnerCommands->import('reina_migrate_a...', Array)
#29 /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php(276): call_user_func_array(Array, Array)
#30 /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php(212): Consolidation\AnnotatedCommand\CommandProcessor->runCommandCallback(Array, Object(Consolidation\AnnotatedCommand\CommandData))
#31 /var/www/html/vendor/consolidation/annotated-command/src/CommandProcessor.php(175): Consolidation\AnnotatedCommand\CommandProcessor->validateRunAndAlter(Array, Array, Object(Consolidation\AnnotatedCommand\CommandData))
#32 /var/www/html/vendor/consolidation/annotated-command/src/AnnotatedCommand.php(387): Consolidation\AnnotatedCommand\CommandProcessor->process(Object(Symfony\Component\Console\Output\ConsoleOutput), Array, Array, Object(Consolidation\AnnotatedCommand\CommandData))
#33 /var/www/html/vendor/symfony/console/Command/Command.php(326): Consolidation\AnnotatedCommand\AnnotatedCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#34 /var/www/html/vendor/symfony/console/Application.php(1096): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#35 /var/www/html/vendor/symfony/console/Application.php(324): Symfony\Component\Console\Application->doRunCommand(Object(Consolidation\AnnotatedCommand\AnnotatedCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#36 /var/www/html/vendor/symfony/console/Application.php(175): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#37 /var/www/html/vendor/drush/drush/src/Runtime/Runtime.php(110): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#38 /var/www/html/vendor/drush/drush/src/Runtime/Runtime.php(40): Drush\Runtime\Runtime->doRun(Array, Object(Symfony\Component\Console\Output\ConsoleOutput))
#39 /var/www/html/vendor/drush/drush/drush.php(139): Drush\Runtime\Runtime->run(Array)
#40 /var/www/html/vendor/drush/drush/drush(4): require('/var/www/html/v...')
#41 /var/www/html/vendor/bin/drush(119): include('/var/www/html/v...')
#42 {main}.

Proposed resolution

After checking the line where the error occurs (line 75 of /var/www/html/web/modules/contrib/filefield_paths/filefield_paths.inc), I noticed that the error occurs in the following fragment:

  /** @var \Drupal\file\Entity\File $file */
  foreach ($field->referencedEntities() as $file) {
    /** @var \Drupal\Core\StreamWrapper\StreamWrapperInterface $source_scheme */
    $source_scheme = $stream_wrapper_manager->getViaUri($file->getFileUri());
    $source_scheme_name = key($stream_wrapper_manager->getWrappers($source_scheme->getType()));
    if (in_array($source_scheme_name, $schemas) && !empty($wrappers[$destination_scheme_name])) {

The file's URI is obtained, but when obtaining the Stream Wrapper from that URI, the Stream Wrapper returns NULL. To avoid this (and possible future errors) a Stream Wrapper integrity check can be added.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

enchufe created an issue. See original summary.

enchufe’s picture

Status: Needs review » Needs work
enchufe’s picture

Status: Needs work » Needs review
abelpzl’s picture

I am working on a migration of a site created in Drupal 7 to Drupal 10.
I use this module to create the path to a file with a custom token of type node. This token is responsible for determining in which path the file should be saved depending on the information on the node.
When importing the migrations I get an error as I do not have the files available in this instance.
It is not the same error as described in this issue but I believe it is related.
I think this module should ignore the entities that are being synchronized. It may be best to check it in filefield_paths_entity_insert().
I am attaching a patch

Status: Needs review » Needs work

The last submitted patch, 6: do-not-activate-during-a-synchronization0001.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

chancenyasulu’s picture

FileSize
836 bytes

I have created this patch please review it, I have simple checked if $temporary_scheme is empty if yes am keeping this $temporary_scheme_name as null since on line $temporary_scheme_name = key($stream_wrapper_manager->getWrappers($temporary_scheme->getType())); key returns null if key was not found.

enchufe’s picture

#6
Currently I have the migration development paused, when I resume it I will test the patch.

#8
But, if $temporary_scheme_name is always NULL, wouldn't the "if" logic always be skipped?

chancenyasulu’s picture

Hey #9
I dont think $temporary_scheme will always be NULL, thats why declaring $temporary_scheme_name with NULL value suits incase this $temporary_scheme is NULL in that way its ensures that $temporary_scheme_name always have expected value. but if $temporary_scheme is not NULL which mean here $temporary_scheme_name = key($stream_wrapper_manager->getWrappers($temporary_scheme->getType()));
we will have right value.

In short am just trying to safeguard this line $temporary_scheme_name = key($stream_wrapper_manager->getWrappers($temporary_scheme->getType())); in such away that only if $temporary_scheme is not NULL then line can be executed. at same time making sure that $temporary_scheme_name has other expected value NULL if line didnt execute.

Sorry for later reply. Thanks