Synchronizing Default Images for Image Fields in Drupal 8
Submitted by Anthony Bouch on 13 April, 2016
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:
# From the dev or staging area
drush config-export # Export configuration changes since last run
git add -all # Stage all the changes
git commit -a # Commit (with a decent commit message)
cap production deploy # Deploy with capistrano
# On the target server
drush 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.
Add a default image to your image field as you normally would and save the updated content type definition.
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.
Go ahead and re-create/re-add the default images to the 'live' or 'target site' - just once
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.
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.
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;
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.