Problem/Motivation
The uid field on media entities is not set to required, but the database field is set to NOT NULL.
To reproduce:
- go to /media/add/image
- fill in the required form elements
- delete the contents of the Authored by form element
- save the form
The site crashes with a storage exception because the uid database field may not be NULL.
Steps to reproduce
Proposed resolution
Ideally, deal with #3260173: EntityOwnerTrait does not define the owner field as required, but the DB field is NOT NULL so it's fixed for all entities.
As an interim, copy the code from Node::preSave() to Media::preSave():
// If no owner has been set explicitly, make the anonymous user the owner.
if (!$translation->getOwner()) {
$translation->setOwnerId(0);
}
}
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #17 | core-3260175-17.patch | 2 KB | nod_ |
| #3 | Saving_media_result.png | 99.51 KB | andregp |
| #3 | Saving_media.png | 97.55 KB | andregp |
Issue fork drupal-3260175
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
andregp commentedI'll work on this issue.
Comment #3
andregp commentedHere is the patch and some prints of the result.
Comment #4
joachim commentedI am wondering whether we can handle this at the field level, the same way that ChangedItem does this:
- check the target entity type
- if it's 'user' and the value is empty, set it to 0
Comment #5
andregp commentedI asked for help about this issue on Drupal #support channel on slack and @james.williams explained me about it. I tried to find a way to identify the field for the verification (inside EntityReferenceItem) but I couldn't. =/
I'm just posting the thread link here so others can consult it too https://drupal.slack.com/archives/C1BB308HH/p1643989304582169
Comment #7
jsricardo commentedI Will Work on it
Comment #8
jsricardo commentedHi!
I checked the dialog quoted in comment #5 by @andregp, however I couldn't implement something that solved it.
Otherwise, I followed the steps to reproduce as mentioned in the description of the issue and the upload of files without owner usually occurs by setting anonymous.
Comment #9
jsricardo commentedComment #11
joachim commentedPatch matches the way it's handled in node module, so LGTM.
I'll maybe open a follow-up for my idea in #4.
Comment #12
alexpottThis is the same behaviour we have for nodes so I think this makes sense. I considered the security implications for media being owned by user 0 and I think they are the same as nodes - ie. don't give the anonymous users permissions.
I think we should have test coverage of this.
Comment #13
joachim commentedAdded test cribbed from NodeEditFormTest which tests the same thing.
(Functional tests really need a reloadEntity method BTW!)
Comment #15
joachim commentedNO idea why the MR is all crazy with commits -- locally the branch is 3 commits ahead of 10.1.x and that's up to date :/
Comment #16
joachim commentedFiled a follow-up: #3318564: entity owner field should handle default value at the field level.
Comment #17
nod_Patch that correspond to the code in the MR
Comment #18
nod_If test pass all good for me
Comment #19
catchCommitted/pushed to 10.1.x, cherry-picked back through to 9.4.x, thanks!
Comment #21
joachim commentedThanks everyone!
We now need a follow-up issue, to fix this more comprehensively for *all* entity types that use this trait -- see #4.