Synchronizing Default Images for Image Fields in Drupal 8

Submitted on Apr 13, 2016, 2:38 p.m.

The new Configuration Management system in Drupal 8 is excellent. When combined with our current Git/Capistrano workflow, it means that all content type and view definition changes can be exported to the configuration 'sync' directory, committed to source control, and pushed to the target deployment site (staging, production etc.).

In our case settings.php for the development and target sites have been updated to use a sync directory that's outside of the site root.

A typical commit and deployment cycle looks like this:

1# From the dev or staging area
2drush config-export # Export configuration changes since last run
3git add -all # Stage all the changes
4git commit -a # Commit (with a decent commit message)
5cap production deploy # Deploy with capistrano
7# On the target server
8drush config-import -y # Import configuration changes for this deploy

It all works very well and is a huge improvement over previous solutions, including using the Features module, or manually exporting and importing views.

However, one gotcha I recently discovered, was 'default images' on image fields. You could argue that these are part of configuration, but the images themselves are stored using unique UUIDs via the content management system's file management subsystem.

And so here's a recipe for 'fixing up' default images.

  1. Add a default image to your image field as you normally would and save the updated content type definition.
  2. Export the configuration changes and push these to your target site. You'll notice that the default images don't appear on the target site, because they're not present in the managed file system.
  3. Go ahead and re-create/re-add the default images to the 'live' or 'target site' - just once
  4. Once the default image(s) have been added, you'll need to fixup the UUIDs of the images to match the UUIDs of your development setup. On the local development site execute the following MySQL query: select * from main_file_managed \G; and make a note of the UUID and fid for the default image.
  5. On the target site, execute the same MySQL query select * from main_file_managed \G; and make a note of the fid and UUID for the default images there.
  6. On the target site update the UUID for the default image to match the source development site. For example update main_file_managed set uuid='6b78ffde-3ede-4c88-af45-53ab2c20a362' where fid=27;
  7. Here's the important part. Now go ahead and run the drush config-import -y again on the 'target' site - and all should be well. You only have to do this once and all future configuration synchronizations will be fine, and the default images will remain in place.

Hope this helps...