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:
- Uploaded files are marked as temporary.
- File uploaded via
MediaUploadgo toDrupal\gutenberg\Controller->upload, which explicitly marks the file as temporary - 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
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | image_with_text.zip | 6.23 KB | walidvb |
Comments
Comment #2
walidvb commentedComment #3
marcofernandes commentedAny 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.
Comment #4
walidvb commented@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
MediaUploadjs component?Comment #5
marcofernandes commentedAh, 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.
uuidandfile_typecan be retrieved at onSelect callback function.This way, when rendering, Drupal will know that the image file is being used.
Comment #6
walidvb commentedYes! 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 frombackground-image?Comment #7
marcofernandes commentedAfter 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
Comment #8
walidvb commented- 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!