media_from_images
The Media From Images module automatically creates media entities for image files that do not already have corresponding media entities. The use case is if you have a site that was built in Drupal 8 without the media module at first and uses the file insert function in CKEditor. It has accumulated many many file entries, many that are duplicates.
A future version of the module may look at the files outside of styles to see if they have a file entry and if not, add one and also add a related media entry. This would be helpful if the site had the IMCE uploader in the CKEditor that does not track files.
The module provides a comprehensive tracking system using SHA256 file hashes to prevent duplicate media creation and maintain data integrity. It includes both web-based administration tools and Drush commands for batch processing of large numbers of files.
Features
- Automatic Media Creation: Creates media entities for image files without existing media representations
- Hash-Based Tracking: Uses SHA256 file hashes to track media-file relationships and prevent duplicates
- Batch Processing: Processes large numbers of files in manageable batches with progress tracking
- Operation Locking: Prevents concurrent operations that could cause data conflicts
- Existing Media Attachment: Identifies and tracks existing media entities not yet in the tracking system
- Administrative Interface: Web-based interface showing statistics and allowing batch operations
- Drush Integration: Command-line tools for server-side processing
- Automatic Cleanup: Removes tracking entries when media or file entities are deleted
- Comprehensive Logging: Detailed logging of all operations for monitoring and debugging
Requirements
- Drupal 10.0 or later
- Media module (core)
- File module (core)
- Image media bundle configured with field_media_image field
Installation
- Copy the module to your modules/custom directory
- Enable the module through the Drupal administrative interface or using Drush:
drush en media_from_images - The module will automatically create the required database table upon installation
- Navigate to Configuration > Media > Media From Images to access the administrative interface
Configuration
No additional configuration is required. The module works with the standard Drupal media system and assumes:
- An 'image' media bundle exists
- The image media bundle has a 'field_media_image' field for file references
- Image files are managed through Drupal's file system
Usage
Web Interface
Access the administrative interface at Configuration > Media > Media From Images to:
- View statistics about image files and media entities
- See current tracking table entries with media and file information
- Start batch processes to create media entities for untracked files
- Attach existing media entities to the tracking system
- Monitor operation status and progress
Drush Commands
drush media-from-images:processordrush mfi:process- Process all image files and create media entities for those without them
Database Schema
The module creates a custom table media_from_images with the following structure:
- media_image_hash (varchar 255, primary key) - SHA256 hash of the image file
- media_image_id (int 10) - Media entity ID
- image_entity_id (int 10) - File entity ID
API
The module provides a service media_from_images.service implementing MediaFromImagesServiceInterface with the following key methods:
getImageFileIds()- Get all image file entity IDsgetFileHash(FileInterface $file)- Generate SHA256 hash for a filemediaEntityExistsForFile(FileInterface $file)- Check if media entity exists for a filecreateMediaFromFile(FileInterface $file)- Create media entity from filestartBatchProcess()- Start batch process for creating media entitiesstartAttachUnattachedProcess()- Start batch process for attaching existing media entities
Similar projects
- Image to Media Handles a similar conversion on image fields on entities. Image to Media module can be a complement to this module.
- Migrate File to Media. This module does something similar but with image fields only. It doesn't handle file entities that may have been generated in CKEditor.
Troubleshooting
- Button doesn't work on first click: This has been resolved in the current version by fixing the entity save order
- Batch operation stuck: Operation locks expire after 30 minutes automatically
- Missing field_media_image: Ensure your image media bundle has the correct field name
- File not found errors: Check that image files physically exist on the filesystem
- Permission errors: Ensure users have 'administer site configuration' permission
Uninstallation
When uninstalling the module:
- Disable the module:
drush pmu media_from_images - The custom database table will be automatically removed during uninstallation
- Media entities created by this module will remain in your system
Technical Notes
- Uses Drupal's batch API for processing large numbers of files
- Implements operation locking to prevent concurrent batch processes
- Automatically cleans up tracking entries when entities are deleted
- Processes files in configurable batch sizes (default: 50 files per batch)
- Includes comprehensive error handling and logging