Closed (fixed)
Project:
Media Directories
Version:
8.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
3 Mar 2020 at 09:41 UTC
Updated:
22 Apr 2020 at 22:19 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
rang501 commentedComment #3
ytsurkI have not seen problems with uploading to the root directory? How to reproduce?
Anyway I would not use the formstate directly, but adjust the method getDirectory, like
as I see no sense for the NULL return!
Comment #4
rang501 commentedI noticed it one of my projects (the site was reinstalled from scratch multiple times to exclude config related issues), but you are correct that it doesn't seem to show up on simple Drupal installation, I didn't find any custom code that should affect that.
The media entities are created but the directory field value was set to 0 and not NULL as it should be saved. If I tried to debug it I found that the getDirectory() returned NULL, which is not correct to use in request argument (active_directory field), after selecting files I think the empty value was typecasted to zero (on the same method). This NULL is used on saving entity (the createMediaFromValue() or prepareEntities()). I don't know why it does work clean installs :)
I think we really should use -1 as a root directory id value (and set it as the default value on media entity pre-save hook), this probably allows to clean up some extra code (I think I made some views related changes to load empty values), entity reference field shouldn't break if we use this value.
Comment #5
rang501 commentedFound the cause of the problem - if the directory field is hidden from the form (form display config), then this can be reproduced, if the field is visible then the browser will send value _empty which will set correct value (NULL). Backend seems to set wrong value by default (0).
Comment #6
ytsurkSo here my previous comment as real patch.
Comment #7
rang501 commentedDid you test it? -1 doesn't work because it throws an exception: Drupal\Core\Entity\EntityStorageException: SQLSTATE[22003]: Numeric value out of range: 1264 Out of range value for column 'directory' at row 1
That was the reason I was using NULL value to determine root directory. I think what we need to do here is to add presave hook that ensures the correct value is set, like when field value is -1 then change it to empty. That way we could use -1 here or keep the previous logic and set it empty when 0 is set as a value. Third option - use my first patch :)
Comment #8
rang501 commentedComment #9
ytsurkNo -- did not test that .. Sorry. Thought this was a quickie .. Need to dive in deeper ..
I would keep the method and not directly add the form_state.
I come back with a better patch.
Comment #10
rang501 commentedYes, you can keep that method but I think the best way is to create a presave hook and set value empty if the value is -1 (maybe include 0 here as well, it is an invalid value too), hopefully, it doesn't throw an exception because it doesn't try to save it yet.
Anyway, thanks for your time :)
Comment #11
ytsurkYou're right. A presave hook makes it reliable. Still I would use the -1 if empty code.
It would go too far to change all ROOT_DIRECTORY logic...
Comment #12
ytsurkAdded another if
Comment #13
rang501 commentedNice work! It's working well and I don't see any issues.
Thanks!
Comment #15
ytsurk