Currently, it is possible to use the 'Image with sizes' display formatter to render 'img' tags with a 'srcset' attribute instead of using the 'picture' element. The image is rendered with theme_image_srcset instead of theme_picture. This functionality is not currently available to inline images (i.e. images inserted with CKEditor). This issue seeks to extend this functionality to inline images.
Description of Problem
When the 'Images with sizes' display formatter is used, no breakpoints or picture mappings are involved. The site builder sets the 'sizes' attribute and selects the desired image styles.
Extending this behavior to the CKEditor integration for inline images presents some challenges. The CKEditor integration is tied to picture mappings, which are tied to breakpoints. (Again, neither breakpoints nor picture mappings are needed for an 'img' tag rendered with 'srcset' and 'sizes' attributes.)
Initially, I tried modifying the '_picture_filter_prepare_image' function to use all of the image styles selected by the selected picture mapping in the 'srcset' attibute; however, that became unwieldy almost immediately. This solution really required bastardizing picture mappings. Additionally, a 'sizes' attribute would need to be set somewhere.
Proposed Solution
In addition to making picture mappings available to CKEditor (current behavior), we also allow site builders to create 'Images with sizes' definitions to make available to CKEditor (requested behavior). These definitions are peers with picture mappings. This happens on the Picture settings page (admin/config/media/picture/settings).
'Images with sizes' Definition
- Title (is validated to ensure no conflict with picture mapping machine name)
- Sizes
- Image styles
- Weight
- Fallback image style
- Picture lazyload (not sure this is applicable here)
- Keep aspect ratio (not sure this is applicable here)
The 'picture_ckeditor_mappings' variable is modified so that each 'picture mapping'/'images with sizes definition' is identified as either 'picture_element' or 'img_srcset' by an 'output_method' value. This mirrors existing behavior found elsewhere in the Picture module.
The '_picture_filter_prepare_image' function is modified to render an image as either 1) a picture element or 2) an image with sizes - depending on the 'output_method' value defined in the 'picture_ckeditor_mappings' variable. The global '"Image with sizes" output method' setting still applies to inline images.