Skip to main content

Introducing the File To Media module

A new contrib module that makes it easy for content editors to create Media Entities from existing Files

by lee.rowlands /

We're pleased to announce the first release of the File To Media module, a relatively simple module that adds a much-requested content-editor feature - namely the ability to easily create a Media entity from an existing File.

The modules adds a new form for creating media entities from existing files, and a handy views plugin that gives you a drop-button of compatible media-types for a file.

To configure the module, edit the view provided by core at admin/content/files and add the File to Media field to the table and save the view.

Now when a user with the access files overview permission visits admin/content/files the get a handy drop-button link for any eligible files.

Screenshot showing the drop-button links allowing a content editor to create new media from an existing file

This link will show if:

  • The file has no existing Media entity association
  • The user has permission to create media of the given type
  • The media-type supports files; and
  • The file extension matches the field configuration for the media-type source field

After clicking on one of the links, the user is taken to a form to create new media of that type, with the file reference and media name pre-filled based on the given file.

Screenshot showing the 'create new image from file' form

A relatively small module, but a very useful feature for content-editors.

And of course, backed with the test-coverage you'd expect from any of the modules we maintain.

Give it a whirl and let us know what you think in the issue queue.

Watch this space for an announcement of a soon to be released related module that handles the opposite content-editor pain-point - deleting files from disk when deleting the associated media item.

Posted by lee.rowlands
Senior Developer

Dated

Comments

Comment by phenaproxima

Dated

Genius. Thank you PreviousNext for doing this!!

Comment by 2pha

Dated

Nice, that will come in handy

Comment by Nicola

Dated

This is exactly what I needed for my small website, great work!
I am testing the module and it looks like the link to create the media is visible only when the "Used in" counter is zero. This is what I have tested:

1. create a new Drupal install with ddev;
2. install and enable the modules media, media_library and file_to_media
3. modify the files view to add the file_to_media field
4. add an image the the image field of the article content type

Now the "Used in" counter is greater than zero and the link to create a media from the image is not visible. At this point the image is not used in any media.

5. remove the image from the article, delete the old revisions, run the cron job and clear the cache

Now the "used in" counter is zero and the link to create the media form the image is visible.

Is this the expected behavior?

Comment by lee.rowlands

Dated

Yes, but I'd be open to making it so that we filtered the 'used in' to only consider usages via media fields - can you open a feature request in the issue queue?

Comment by Nicola

Dated

Feature request created, thank you.

Comment by Adam E.

Dated

I'm not sure what need in site management this module covers. Is it when you move from D7 to D8 and all your previous files were stoved away in file fields and you now need to adapt to Media Library, or is it when you need a file that's in Drupal, but not in Media Library? Or both? Or neither?

Comment by lee.rowlands

Dated

In our case, there was a migration involved, but there are other reasons you might have files without media - eg you previously used the CKeditor image embed plugin, and have since transitioned to Media.

We were using the excellent Audit Files module which allows you to created managed file entities from files that exist on disk, but not in the database. This is the next step, taking them from file entities to media entities so they can be used in WYSIWYG and entity-reference fields

Comment by jonathan1055

Dated

This is useful, nice work. I will test it alongside my enhancements for the Scheduler module to cover non-node entities via a plugin, the first of which is Media. The issue is https://www.drupal.org/project/scheduler/issues/2096585 if any users want to scheduler their newly created Media from files.

Pagination