Hi,

I'm running into problems when writing a custom module that uses the MediaUpload.

Specifically, files uploaded deleted after a while. Trying to debug, i've found that:

  1. Uploaded files are marked as temporary.
  2. File uploaded via MediaUpload go to Drupal\gutenberg\Controller->upload, which explicitly marks the file as temporary
  3. images uploaded via the built-in image block(under Common Blocks) seemingly go to the same URL, but are marked as permanent(and usage count is increased). However, the module marked is editor, so i'm guessing I missed something

I've now edited the file upload as follows, and will see in 6 hours what goes:

      - $file->setTemporary();
      + $file_usage = \Drupal::service('file.usage');
      + $file_usage->add($file, 'gutenberg', 'node', 18);

Files uploaded are indeed marked as permanent and usage count is updated.
(18 is the nid of an existing node, but not the currently edited node – not sure this info is part of the request).

I understand that this prevents the file from being cleaned, as it is not attached to the right node

Am I missing something?

Related issue: https://www.drupal.org/project/gutenberg/issues/3030767 (couldn't find it in the Related Issues field 🤷🏼‍♂️)

PS: code to the block's js, if relevant: https://gist.github.com/walidvb/e30c0ee63c4981d54a72957d70b1709b

CommentFileSizeAuthor
#4 image_with_text.zip6.23 KBwalidvb

Comments

walidvb created an issue. See original summary.

walidvb’s picture

Title: uploaded images are marked as temporary » images uploaded via MediaUpload are marked as temporary
marcofernandes’s picture

Any uploaded image will be marked first as temporary. Only when saving the node that Drupal checks any used image and mark it as permanent.
You can test this with the regular CKEditor - insert an image, removed it and insert another image and save the node. The result will be one temporary image and one permanent image.
We're just using Drupal's standard behavior regarding uploaded images via editor.

walidvb’s picture

StatusFileSize
new6.23 KB

@marcofernandes, thanks for your reply.

I now understand better the flow. However, as far as I saw, the image file was not marked as permanent even

Steps to reproduce:

- enable image_with_text module
- add image block to a gutenberg'ed node
- save node

Expected Result:
- file in admin/files should be marked as permanent, and have their usage count = 1

Observed results:
- File is still marked as temporary, and have a 0 usage count

Am i doing something wrong? Maybe saving the node doesn't include the fileID uploaded with the MediaUpload js component?

marcofernandes’s picture

Ah, of course! It's a custom module.
All you have to do is set the data-* attributes to the div that holds the image as background.

const image = <div>{full_width_mobile && <div data-entity-uuid={uuid} data-entity-file-type={file_type} class=" hidden-sm cover-image" style={{ backgroundImage: `url(${full_width_mobile.source_url})` }} />}

uuid and file_type can be retrieved at onSelect callback function.

This way, when rendering, Drupal will know that the image file is being used.

walidvb’s picture

Yes! Cool thanks that makes sense.
I couldn't really find docs about making your own module, maybe this should/could be documented somewhere(along with a link to find the available blocks, which also isn't easy to find!)

Last question: How does drupal fetch the data-entity-uuid? From what I read, it reads it from the <img /> tag, not sure about when read from background-image?

marcofernandes’s picture

Status: Active » Closed (works as designed)

maybe this should/could be documented somewhere

After the stable release (hopefully soon), we'll focus more on dev docs.

Didn't actually check but I think it works also for other tags. I've tested with <div>.
In case I'm wrong you can always render a hidden image :D

walidvb’s picture

- will test on another website, this one is live, and changing this would break my blocks :/
- Agreed with hidden image(although it wouldn't be ideal in my case, considering i use 2 sizes of the image within a <style> and media query, but works)
- noted reg the stable release

Thanks for the answers and good work!