This site, my glorified blog, started out on Drupal 4. It wasn't until Drupal 8 that there was finally a single official way to handle putting media into nodes, with a media reference field and a media embed filter. Between then and now I used first the venerable img_assist and then the image_filter module, both of which fell by the wayside after D7. I replaced them both using php custom filters, but php in the database is now deprecated for security reasons. In the interest of doing things the right way, I wanted to switch to using the now-official media entity references.
This turned out to be both a lot simpler and a lot more complicated than I had hoped. My use case is relatively simple, in that my images were all in image fields on nodes of type image, and only a single image was attached to each node. My Image nodes and all associated images survived the trip from D7 to D9 intact and with their original nids. I was able to use image_field_to_media module to clone my image fields to media reference fields, and then I deleted the image field. This all went swimmingly, but now I had to decide what to do with the embedded tags.
I found hope in a module called Convert Media Tags to Markup. The original's purpose is to read the JSON-based tags from the Drupal 7 media module and turn them into img tags, but what I wanted was to read the pipe-and-equals sign-delimited img_assist tags. Long story short, I modified the module to do this job instead. What's interesting about the module is that it's a text filter, so you can put it into place and then view the page to verify that the tags come out correctly. Then you use drush ev(al) to both preview and then finally perform a modify-in-place using the filter.
Up next, I will track down the nodes which have image filter tags, which are extremely similar to img_assist tags, and twiddle the module again. I will likely just create another, separate module to perform the conversion, both because it will take the least work and because I hope not to need this module again in any case. It is also not strictly necessary to convert anything — just include this filter before the Embed media filter and the combination should result in your nodes with img_assist being displayed correctly.
Instructions
- Unpack the module to modules/custom in your drupal web dir, and enable it.
- Optionally use the Convert img_assist Image Tags to Media Text Filter to preview changes interactively, with or without the Embed media filter. You will need to view source to see the media tags without the latter.
- Optionally convert text fields' content from using img_assist to media:
- Make a database backup, in case something goes badly wrong.
- Make sure that the settings for the content type you plan to convert include new revision, so you can revert the changes later if you change your mind, without having to restore the whole database.
- Use drush ev to preview the conversion - you will want to capture the output to a file, or pipe it through less or similar. Example:
drush ev "Drupal\\convert_img_assist_tags_to_media\\Convertimg_assistTagsToMedia\\DbReplacer::instance()->replaceAll('node', 'book', TRUE);" | less
The "TRUE" at the end specifies a dry run. - If you are happy with the results, change the TRUE to FALSE and run the command again (and probably pipe it through tee /tmp/logfile.txt or something similar)
Attachment | Size |
---|---|
convert_img_assist_tags_to_media-9.x-0.1a.tar_.gz (15.94 KB) | 15.94 KB |
P.S. This module is…
P.S. This module is definitely not finished, but it works here
Oh my. It looks like I also…
Oh my. It looks like I also used acidfree module at one point. This truly illustrates how necessary it was for Drupal to include an official image embedding solution. Acidfree tags look like [acidfree:3745 align=left size=80x163] and will also be easy enough to whip into shape. Instead of exploding on '|', I use ' ', and I also have to s/acidfree:/nid=/, and my existing filter should otherwise do the job satisfactorily.
I am simply ignoring image sizes at this point, because I know that I really didn't use them. Their only function has been to save some database lookups for image sizes.
Image to Media Filters module
I have worked up a new D8/D9 module to do this job. I have not coded up the changes necessary to make the database replacer work with my new filters, but here it is anyway. There are currently filters for img_assist (probably complete), acidfree (maybe complete), and image_filter (not complete, but basically functioning.)
OK, now you can process your…
OK, now you can process your database, instructions are in the README.md. It's basically like the instructions in the blog post above, except that you also need to specify a filter in between the bundle and the simulation flag.
image_to_media_filters is…
image_to_media_filters is now a published module on Drupal.org, with a[n alpha] release and everything.