This happened in my case when a user got deleted, saving any Media Entity originally created by that user resulted in an error.
This also appears to happen when manually removing the Author when creating or editing a Media Entity.
In the UI, this relates to the "Authoring information" tab, "Authored by" field.

Error message:
Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'uid' cannot be null: INSERT INTO {media_field_data}

Comments

olivier.bouwman created an issue. See original summary.

olivier.bouwman’s picture

Status: Active » Needs review
StatusFileSize
new749 bytes
ashleyhazle’s picture

We have seen this when creating new media entities, after updating to 8.7.x (currently on 8.7.2).

Whilst this patch makes sense in the context of a user having been deleted, we find that an author is no longer automatically added when creating media.

Equally, the UID should really be updated when the user account is deleted.

I get that this is taken from Node preSave, but it feels like it's just working around a larger problem.

jcnventura’s picture

Priority: Normal » Major
Issue summary: View changes

Our case is we're using paragraphs with entity references to media entities. When saving the article, we get the same exception being thrown. Adding this patch fixes the issue. No clue why we're saving the media when adding entity references to it.

This site was previously using the media_entity module, but we've upgraded to media-in-core.

This was working fine with core 8.6.14, and after updating to core 8.7.2, we started to get this error. Updating to core 8.7.3 did not fix it.

init90’s picture

The same approach was used in Drupal Commerce to fix similar error:

https://www.drupal.org/project/commerce/issues/3020240

spokje’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

I'm experiencing the exact same as jcnventura in #4, so Paragraphs with entity references to media entities.

Patch #2 does fix the immediate issue, but in my (and jcnventura's) case it seems wrong to set the owner of the media to Anonymous, since in our case, we have an actual owner.

Due to that (and the fact that we never going to get this in without tests) setting it back to Needs Work, but again stressing the patch _does_ fix the immediate issue and will allow Media to be saved.

I hope to get back later today and at least write some tests, but if somebody beats me to that, I would be very grateful.
Leaving this at Major since it involves lost user input.

spokje’s picture

Right...the motto 'Think before you type' applies once again.
Just after submitting the above I realized the Root Cause for sites coming from drupal/media_entity, which lies here: https://www.drupal.org/project/media_entity/issues/2953384#comment-12557186

A telltale sign that the error is caused by a drupal/media_entity to Core Media migration is the presence of this PHP Warning just before the actual fatal Error:

Warning: call_user_func() expects parameter 1 to be a valid callback, class 'Drupal\media_entity\Entity\Media' not found in Drupal\Core\Field\FieldConfigBase->getDefaultValue() (line 397 of \web\core\lib\Drupal\Core\Field\FieldConfigBase.php

@jcnventura Could you search your exported config for default_value_callback: 'Drupal\media_entity\Entity\Media::getCurrentUserId', replace that with default_value_callback: 'Drupal\media\Entity\Media::getCurrentUserId' , import the changed config and remove the patch in #2?

That worked out all fine for me, and now the newly added media will have the correct UserID attached, instead of Anonymous as it would be the case with this patch.

@ashleyhazle: Due to your comment

we find that an author is no longer automatically added when creating media.

it seems to me that your site also has transitioned from drupal/media_entity to Core Media and all your current media is/was having an invalid NULL as uid due to the above (the callback to the non-existing media_entity module).

That would explain all your media ending up needing patch #2 for saving, but with that patch assigning the anonymous uid 0 to them.

If either/both of you are still following this issue and could confirm the above, that would be grand.

So currently the only case for this error to happens seems to be when a User is deleted, but its content is not deleted and not assigned to the anonymous user.

As far as I can think of, that's only possible if the user is deleted programmatically, so this issue might not be a priority Major after all, since it doesn't affect loads of people. I'll let smarter people than me decide on that.

For my penance I'll come back later and write a test for the above. :)

spokje’s picture

Assigned: Unassigned » spokje
phenaproxima’s picture

spokje’s picture

@phenaproxima It most definitely is IMHO.

EDIT: Aaand I stand corrected: This issue is a very specific edge case that ist covered in patch #2 in the related issue.

Steps to reproduce:

- D8.7.x, enabled Media.
- Create user
- Login with that user
- Create a Media Item
- Delete that user in a nasty programmatically way (I used `devel_php`);

// EDIT: No need for nastiness, deleting a User through the admin GUI already triggers this behaviour.
// $user = \Drupal\user\Entity\User::load($userId);
// $user->delete(); 

- Login with another user.
- Try to save Media item through Admin GUI
- CLUNK

Since the mentioned patch in the other issue implements a media_user_predelete which deletes all media entities and revisions owned by the user that is getting deleted, the CLUNK part won't happen, since you can't edit non-existing (since already deleted) media entities.

If the deletion is a good thing is debatable, but it _is_ consistent with all (rather most until the parent issue is fixed) other entities owned by the deleted user, they are also deleted.

spokje’s picture

EDIT: Stand corrected again.

Deleting a Media owning user through the Admin GUI is already triggering this error.

Steps to reproduce:

- D8.7.x, enabled Media.
- Create user
- Login with that user
- Create a Media Item
- Delete that user.
- Login with another user.
- Try to save Media item through Admin GUI
- CLUNK

anas_maw’s picture

Patch in #2 fixed my issue.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.9 was released on November 6 and is the final full bugfix release for the Drupal 8.7.x series. Drupal 8.7.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.8.0 on December 4, 2019. (Drupal 8.8.0-beta1 is available for testing.)

Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

spokje’s picture

Assigned: spokje » Unassigned
mohit_aghera’s picture

Assigned: Unassigned » mohit_aghera
nikunjkotecha’s picture

StatusFileSize
new10.19 KB

After discussion with Mohit and Jitesh we found out that the root cause was the missing implementation of hook_user_cancel() only and we should go ahead with implementation of it. Mohit is working on tests for same.

liampower’s picture

Version: 8.8.x-dev » 9.0.x-dev
Assigned: mohit_aghera » Unassigned
Status: Needs work » Needs review

Patch in #2 fixed the fatal error on saving, this is similar to how nodes work if you remove the author.

The patch in #16 is covered by the related issue #3069291 and I believe further work for media items when users are deleted should be done there.

droodpal’s picture

StatusFileSize
new193.58 KB

Hi, very similar problem, when trying to create native article or a basic page. Other content types work. I've posted details here:
https://drupal.stackexchange.com/questions/294562/cant-create-an-article...

Any ideas what causes it?

The website encountered an unexpected error. Please try again later. Error: Call to a member function getAccountName() on null in Drupal\node\NodeForm->form() (line 155 of core/modules/node/src/NodeForm.php).

Other Content Types, when Warnings enabled show:

call_user_func() expects parameter 1 to be a valid callback, class 'Drupal\node\Entity\Node' does not have a method 'getCurrentUserId' in Drupal\Core\Field\FieldConfigBase->getDefaultValue() (line 397 of core/lib/Drupal/Core/Field/FieldConfigBase.php).

Reinstall didn't help, cache cleaning, update.php - tried all.

norwegian.blue’s picture

I am also seeing this issue on D9.0.1.

The site I am experiencing the issue in did not previously use media - either contrib or core.

The main issue I experience is one that seems to have been mostly ignored from #3 .

When creating media, if no author is specified manually, then this exception is thrown. Manually specifying the author allows the media item to be created.

This appears to have the add-on effect of not allowing media to be created from the library interface.

norwegian.blue’s picture

Update.

I've resolved my issue.

2953384 #4 gave me the clue to resolving this for me.

When I inspected core.base_field_override.media.image.uid.yml exported from my site, it appeared correct. Another site not experiencing this issue did not have that config file exported.

I deleted all .yml's that overide the base field uid (eg core.base_field_override.media.image.uid.yml ,core.base_field_override.media.video.uid.yml )
and reimported the config (drush config:import sync)

This resolved the issue for me.

Environment:
PHP7.3
Drupal 9.0.1
MySQL
Linux
Apache

NOTE - I was also experiencing the same issue as #18 . Deleting the config that overrides the uid field for the affected content types also resolved this.

tanubansal’s picture

StatusFileSize
new218.38 KB

Please check the attached Screenshot of issue that i got in 9.1. Is there any patch available for 9.1?

dww’s picture

Status: Needs review » Needs work
Issue tags: +Bug Smash Initiative, +Needs subsystem maintainer review

Re: "Is there any patch available for 9.1?" -- Both #2 and #16 apply cleanly to the 9.1.x branch. You could have tried that yourself.

Hiding screenshots from the file table in the summary -- we only need to see patches there.

Haven't fully wrapped my head around this issue and given thought to the right way to actually fix it. Patch #16 here is extremely similar to #3069291-2: Quick win: Media entities support user cancel (which was posted 6 months prior), since both are basically just copy/paste renames of node_mass_update() to media_mass_update() (more or less).

Tagging this for a subsystem maintainer review, since it seems like both this and #3069291: Quick win: Media entities support user cancel should be closed as duplicates with #3043725: Provide a Entity Handler for user cancelation, but that's not up to me. ;)

p.s. I moved to "Needs work" for tests, but that's already further along at both of the other issues...

phenaproxima’s picture

Status: Needs work » Closed (duplicate)
Issue tags: +Triaged Media Initiative issue

I think I agree with @dww. This would be solved by #3043725: Provide a Entity Handler for user cancelation, so we should concentrate our efforts there.

danrod’s picture

I can confirm that patch #2 works for the 9.2.x branch.

scot.hubbard’s picture

I started experiencing this with a Drupal 9 site (not sure when it began, but I have latest 9.2 core installed now).

For me, the fix was to look at the core.base_field_override.media.--MEDIA_TYPE--.uid.yml files, and change the following line:

default_value_callback: 'Drupal\media\Entity\Media::getCurrentUserId'

to this:

default_value_callback: 'Drupal\media\Entity\Media::getDefaultEntityOwner'

Once re-imported, there are no log entries for "call_user_func" and when adding new media items, the author field defaults to the current user.

nicolas s.’s picture

I confirm that patch #2 works for the 9.1.x branch.

aleksi_anisimov’s picture

Why do you use "0" as default value, instead of current user id, if it's set?
Would it be more logical to user current user id?

amitajgaonkar’s picture

#25 worked for me

edward.peters’s picture

#25 worked for me too. Thank you for the help.

srihari manepally’s picture

StatusFileSize
new998 bytes

I have modified the patch so that it takes current user id instead of "0"(anonymous user). This patch worked on Drupal 9.2.7 version.

heyehren’s picture

Version: 9.0.x-dev » 9.3.x-dev

Hi there,
I'm experiencing the same error message when trying to create a new media file on the 'Add media item' page (/media/add).

This is the error message:

TypeError: call_user_func(): Argument #1 ($callback) must be a valid callback, class "Drupal\media_entity\Entity\Media" not found in call_user_func() (line 398 of core/lib/Drupal/Core/Field/FieldConfigBase.php).

Originally i migrated from the media module to media core, and everything worked well until a recent upgrade to Drupal 9.

I have applied patch 2, which didn't work for me. Then i tried patch 30, which also didn't make a difference.

Does the patch have to be altered to work with Drupal 9.3.9?

I'd also like to try the suggestion in #25, but i don't know how to re-import the file after it has been altered.

Any help with this would be greatly appreciated.