Drupal 9 image field migration

As you know, when creating image field there is an option to create a default image fallback. Image will be saved in public folder. The issue is that we cant export default image within configs, but that's ok for singlesite instances where you need to manually copy the image to production website from your dev env. Also you need to update the image manually again, if you changed it in configs.

Using https://www.drupal.org/project/config_default_image module will make it possible, but image should be located in public:// folder. Sabe problem again.

My solution is flexible and can use one image for all cases. I had to implement this because of multisite instance, where every website has own files and database, but the same codebase. 
We need to create custom formatter and provide image from module's folder. To achieve this we will use really old ongoing issue to implement additional stream wrappers for D10. Patching the core with https://www.drupal.org/files/issues/2021-12-15/1308152-395.patch will help to achieve the result. Issue url https://www.drupal.org/project/drupal/issues/1308152. It will add additional stream wrappers like module:// and theme://.
So after patching you need to create a custom Formatter to handle default images from your module. Actually it's possible to use an image from custom theme folder but additional settings should be implemented.
Add the image to my_module/images/default-image.png folder and select new formatter for your image.

Code posted on Gists ImageDefaultFallbackFormatter.php

Add new comment

The content of this field is kept private and will not be shown publicly.
  • No HTML tags allowed.
CAPTCHA
 ooooooo  ooooo    oooooooo  ooooo         oooo          .ooooo.                
`8888 d8' dP""""""" `888' `888 d88' `8.
Y888..8P d88888b. 888 888 .oo. Y88.. .8' oooo oooo
`8888' `Y88b 888 888P"Y88b `88888b. `888 `888
.8PY888. ]88 888 888 888 .8' ``88b 888 888
d8' `888b o. .88P 888 o 888 888 `8. .88P 888 888
o888o o88888o `8bd88P' o888ooooood8 o888o o888o `boood8' `V88V"V8P'


Enter the code depicted in ASCII art style.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.