Reprovision default media thumbnails when not found at icon_base_uri

Created on 9 June 2019, about 5 years ago
Updated 10 November 2023, 8 months ago

Problem/Motivation

The media module’s hook_install() takes the default icons that ship with core and copies them to public://media-icons/generic (overridable in settings). By doing this, it immediately takes files that are shipped with core and typically checked into version control and places them into the files directory where user-generated content ends up and is not version-controlled.

If you are in the early stages of a site and have not yet created any content, you might assume that when moving the codebase and database to a new environment, you don't need to move the files directory since you have not created any content, but due to this behavior, that assumption is not true.

Are there other examples in Drupal core that operate in the way that media’s default icons do? In contrast, the image module ships with a sample.png (hot air balloons), and this image is not manipulated at all in image_install().

Steps to Reproduce

  1. Install Drupal.
  2. Enable Media module.
  3. Get database dump.
  4. Clone site in a new environment.
  5. Import database.
  6. Skip transferring files directory because no content has been created.
  7. Media icons are missing.

Proposed resolution

Reprovision the files on hook_rebuild().

Workaround

Re-run the media install hook using drush:

$ drush php:eval "include 'core/modules/media/media.install'; media_install();"
πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
MediaΒ  β†’

Last updated about 16 hours ago

Created by

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • Needs framework manager review

    It is used to alert the framework manager core committer(s) that an issue significantly impacts (or has the potential to impact) multiple subsystems or represents a significant change or addition in architecture or public APIs, and their signoff is needed (see the governance policy draft for more information). If an issue significantly impacts only one subsystem, use Needs subsystem maintainer review instead, and make sure the issue component is set to the correct subsystem.

  • Usability

    Makes Drupal easier to use. Preferred over UX, D7UX, etc.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • There's not even a patch. Needs work is misleading.

  • I bet the reason for copying these images over to the public files dir is that as #3060509-23: Media module treats default thumbnails as user content, allowing them to be lost when syncing files across environments β†’ points out Drupal needs to create styled derivatives from them. Still, they are not actually content because you ain't even able to simply change them from anywhere in the UI.

    It would be cool if Media would out of the box every time it requests these images simply check if the file copies already exist at the specified icon_base_uri and if not, copy them over on the fly. While at the same time you ideally also can specify an icon_source_uri where it should copy the files from. And that the icon_source_uri can be any custom or contrib module's or theme's subdirectory.

    Actually it makes no sense to me you can specify an icon_base_uri at all. This could simply be hard-coded while a configurable icon source URI would be much more interesting.

  • Rephrase title into a fixable task

  • πŸ‡ΊπŸ‡ΈUnited States markdorison
  • πŸ‡§πŸ‡¬Bulgaria pfrenssen Sofia
  • Status changed to Needs review 8 months ago
  • πŸ‡§πŸ‡¬Bulgaria pfrenssen Sofia

    Let's kick this off. Here's a first simple implementation following the suggestion of @jonathanshaw in #3060509-28: Reprovision default media thumbnails when not found at icon_base_uri β†’ to reprovision the icons on hook_rebuild().

  • Status changed to Needs work 8 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Was tagged for tests which are still needed.

    As patches are phased out it's encouraged to use MRs. Patches no longer auto run.

  • πŸ‡§πŸ‡·Brazil filipe82sp

    I leveraged the patch from #53, but did it simpler and direct to point.

  • last update 8 months ago
    Patch Failed to Apply
  • πŸ‡§πŸ‡¬Bulgaria pfrenssen Sofia

    Thanks @filipe82sp!

    --- core/modules/media/media.module	2023-11-06 21:17:57.000000000 -0300
    +++ core/modules/media/media.module	2023-11-06 21:22:08.000000000 -0300
    @@ -528,3 +528,12 @@
    
    +function media_rebuild(): void {
    +  // Loads media install file and execute hook_install.
    +  \Drupal::moduleHandler()->loadInclude('media', 'install');
    +  media_install();
    +}
    

    Calling the entire install hook might be risky since there is a possibility that more functionality will be added to the install hook in the future. To ensure we only execute the code we want to execute, it is better to refactor it in a dedicated function, like is done in patch #53.

    --- core/modules/media/media.module	2023-11-06 21:17:57.000000000 -0300
    +++ core/modules/media/media.module	2023-11-06 2
    @@ -528,3 +528,12 @@
    
    +function media_rebuild(): void {
    +  // Loads media install file and execute hook_install.
    

    This documentation says _what_ is being done but it would be better to explain _why_ it is needed, like is done in patch #53.

    I know it is my own patch, but would respectfully argue that patch #53 is safer and better documented, even though I agree your version is more compact.

  • πŸ‡ΊπŸ‡ΈUnited States markdorison

    @pfrenssen Well said and +1. Thank you for laying out the pros/cons so clearly. Should we create a merge request so that GitLab CI tests can run against this?

  • πŸ‡§πŸ‡·Brazil filipe82sp

    Hi @pfrenssen, thanks for your feedback!

    I agree with you too! Since in my case there's no issue related to permissions, once we didn't change it, then worked satisfactory and as expected. It would be good to proceed with MR of your changes as @markdorison suggested.

Production build 0.69.0 2024