Problem/Motivation

After trying to update to SVG Image Field 2.3.6 I get the following error:

PHP Fatal error: Uncaught Error: Class "Drupal\media\Plugin\media\Source\File" not found in /web/modules/contrib/svg_image_field/src/Plugin/media/Source/ScalableVectorGraphic.php:21

I do not have the Media module installed.

Details:

→ drush cr
PHP Fatal error:  Uncaught Error: Class "Drupal\media\Plugin\media\Source\File" not found in /web/modules/contrib/svg_image_field/src/Plugin/media/Source/ScalableVectorGraphic.php:21
Stack trace:
#0 /vendor/composer/ClassLoader.php(576): include()
#1 /vendor/composer/ClassLoader.php(427): Composer\Autoload\{closure}()
#2 [internal function]: Composer\Autoload\ClassLoader->loadClass('Drupal\\svg_imag...')
#3 /web/modules/contrib/svg_image_field/svg_image_field.module(14): class_alias('\\Drupal\\svg_ima...', '\\Drupal\\svg_ima...')
#4 /web/core/lib/Drupal/Core/Extension/Extension.php(153): include_once()
#5 /web/core/lib/Drupal/Core/Extension/ModuleHandler.php(128): Drupal\Core\Extension\Extension->load()
#6 /web/core/lib/Drupal/Core/Extension/ModuleHandler.php(141): Drupal\Core\Extension\ModuleHandler->load('svg_image_field')
#7 /web/core/lib/Drupal/Core/DrupalKernel.php(615): Drupal\Core\Extension\ModuleHandler->loadAll()
#8 /web/core/includes/utility.inc(35): Drupal\Core\DrupalKernel->preHandle(Object(Symfony\Component\HttpFoundation\Request))
#9 /vendor/drush/drush/src/Commands/core/CacheRebuildCommands.php(70): drupal_rebuild(Object(Composer\Autoload\ClassLoader), Object(Symfony\Component\HttpFoundation\Request))
#10 [internal function]: Drush\Commands\core\CacheRebuildCommands->rebuild(Array)
#11 /vendor/consolidation/annotated-command/src/CommandProcessor.php(276): call_user_func_array(Array, Array)
#12 /vendor/consolidation/annotated-command/src/CommandProcessor.php(212): Consolidation\AnnotatedCommand\CommandProcessor->runCommandCallback(Array, Object(Consolidation\AnnotatedCommand\CommandData))
#13 /vendor/consolidation/annotated-command/src/CommandProcessor.php(175): Consolidation\AnnotatedCommand\CommandProcessor->validateRunAndAlter(Array, Array, Object(Consolidation\AnnotatedCommand\CommandData))
#14 /vendor/consolidation/annotated-command/src/AnnotatedCommand.php(389): Consolidation\AnnotatedCommand\CommandProcessor->process(Object(Symfony\Component\Console\Output\ConsoleOutput), Array, Array, Object(Consolidation\AnnotatedCommand\CommandData))
#15 /vendor/symfony/console/Command/Command.php(326): Consolidation\AnnotatedCommand\AnnotatedCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#16 /vendor/symfony/console/Application.php(1088): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#17 /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))
#18 /vendor/symfony/console/Application.php(175): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#19 /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))
#20 /vendor/drush/drush/src/Runtime/Runtime.php(40): Drush\Runtime\Runtime->doRun(Array, Object(Symfony\Component\Console\Output\ConsoleOutput))
#21 /vendor/drush/drush/drush.php(139): Drush\Runtime\Runtime->run(Array)
#22 /vendor/drush/drush/drush(4): require()
#23 /vendor/bin/drush(119): include()
#24 {main}

thrown in /web/modules/contrib/svg_image_field/src/Plugin/media/Source/ScalableVectorGraphic.php on line 21

[warning] Drush command terminated abnormally.

It is caused by the class_alias() call in svg_image_field.module. That apparently caused the file ScalableVectorGraphic.php to be loaded and if the Media module is not installed it cannot find \Drupal\media\Plugin\media\Source\File.

Steps to reproduce

  1. Have a Drupal site that does not have the Media module installed.
  2. Install svg_image_field 2.3.6

Proposed resolution

Check if the class \Drupal\media\Plugin\media\Source\File exists before making the class_alias() call.

Remaining tasks

  • Add an automated test that demonstrates the problem.
  • Implement the proposed resolution.
  • Review.
  • Merge.

User interface changes

None.

API changes

None.

Data model changes

None.

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:

Comments

megachriz created an issue. See original summary.

megachriz’s picture

Issue summary: View changes
Status: Active » Needs review

I've provided a test and a possible fix in the MR.

abdulaziz zaid’s picture

Status: Needs review » Reviewed & tested by the community

Tested; the changes work correctly with and without the Media modules enabled.

jwilson3’s picture

Assigned: Unassigned » jwilson3

Thanks Youri and Abdulaziz.

I've tested the functional test (THANK YOU!) and confirmed that without the fix, the test fails, and with the fix, the tests pas.

Merge and release on the way.

$ git log --graph --oneline
* 504b479 2025-12-02  6:56:25 (MegaChriz) (HEAD -> 3561016-fatal-error-without-media-module, svg_image_field-3561016/3561016-fatal-error-without-media-module) Added fix.
* 3c7c974 2025-12-02  6:56:11 (MegaChriz) Added test coverage.

$ git checkout 3c7c974
HEAD is now at 3c7c974 Added test coverage.

$ ddev phpunit tests/src/Functional/InstallTest.php
PHPUnit 10.5.45 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.27

E.                                                                  2 / 2 (100%)

Time: 00:11.931, Memory: 6.00 MB

There was 1 error:

1) Drupal\Tests\svg_image_field\Functional\InstallTest::testInstallation
Exception: Error: Class "Drupal\media\Plugin\media\Source\File" not found
include()() (Line: 21)


/var/www/html/web/core/lib/Drupal/Core/Test/HttpClientMiddleware/TestHttpClientMiddleware.php:55
/var/www/html/vendor/guzzlehttp/promises/src/Promise.php:209
/var/www/html/vendor/guzzlehttp/promises/src/Promise.php:158
/var/www/html/vendor/guzzlehttp/promises/src/TaskQueue.php:52
/var/www/html/vendor/guzzlehttp/promises/src/Promise.php:251
/var/www/html/vendor/guzzlehttp/promises/src/Promise.php:227
/var/www/html/vendor/guzzlehttp/promises/src/Promise.php:272
/var/www/html/vendor/guzzlehttp/promises/src/Promise.php:229
/var/www/html/vendor/guzzlehttp/promises/src/Promise.php:69
/var/www/html/vendor/guzzlehttp/guzzle/src/Client.php:189
/var/www/html/web/core/tests/Drupal/Tests/DrupalTestBrowser.php:139
/var/www/html/vendor/symfony/browser-kit/AbstractBrowser.php:378
/var/www/html/vendor/behat/mink-browserkit-driver/src/BrowserKitDriver.php:122
/var/www/html/vendor/behat/mink/src/Session.php:175
/var/www/html/web/core/tests/Drupal/Tests/UiHelperTrait.php:273
/var/www/html/tests/src/Functional/InstallTest.php:56

ERRORS!
Tests: 2, Assertions: 8, Errors: 1.
Failed to run phpunit tests/src/Functional/InstallTest.php: exit status 2

$ git checkout -
Previous HEAD position was 3c7c974 Added test coverage.
Switched to branch '3561016-fatal-error-without-media-module'
Your branch is up to date with 'svg_image_field-3561016/3561016-fatal-error-without-media-module'.

$ ddev phpunit tests/src/Functional/InstallTest.php
PHPUnit 10.5.45 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.27

..                                                                  2 / 2 (100%)

Time: 00:10.027, Memory: 4.00 MB

OK (2 tests, 8 assertions)

  • jwilson3 committed d70250b8 on 2.3.x authored by megachriz
    Issue #3561016 by megachriz: Avoid fatal error without media module
    
jwilson3’s picture

Assigned: jwilson3 » Unassigned
Status: Reviewed & tested by the community » Fixed

This issue has been fixed and released in 2.3.7.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

megachriz’s picture

Thanks for merging!

Just so you know, on the MR pipeline there is now an option to run tests without the fix. It also runs only the tests in files that were modified.

jwilson3’s picture

Yes! I just saw that AFTER I had finished testing locally. Great addition.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.