Problem/Motivation
The default_value_callback of uid base fields, defined on core.base_field_override.media.*.uid is pointing to the deprecated funcion Drupal\media\Entity\Media::getCurrentUserId. This function is replaced by getDefaultEntityOwner() implemented on \Drupal\user\EntityOwnerTrait.
This causes a fatal error when trying on upload a new media entity throw media block on layout builder, and provably by other behaviors.
Deprecated
The ::getCurrentUserId method is deprecated in 8.6.x and will be removed before 9.0.0.
Steps to reproduce
- Install varbase_media on a D9 site.
- Create a landing page (layout builder)
- Insert a media block
- Try to upload a new image
- You will get the following error:
Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'uid' cannot be null: INSERT INTO {media_field_data} ("mid", "vid", "bundle", "langcode", "status", "uid", "name", "thumbnail__target_id", "thumbnail__alt", "thumbnail__title", "thumbnail__width", "thumbnail__height", "created", "changed", "default_langcode", "revision_translation_affected") VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11, :db_insert_placeholder_12, :db_insert_placeholder_13, :db_insert_placeholder_14, :db_insert_placeholder_15); Array ( [:db_insert_placeholder_0] => 10 [:db_insert_placeholder_1] => 10 [:db_insert_placeholder_2] => image [:db_insert_placeholder_3] => es [:db_insert_placeholder_4] => 1 [:db_insert_placeholder_5] => [:db_insert_placeholder_6] => arboles_portada.jpg [:db_insert_placeholder_7] => 13 [:db_insert_placeholder_8] => Bosque [:db_insert_placeholder_9] => [:db_insert_placeholder_10] => 1920 [:db_insert_placeholder_11] => 1297 [:db_insert_placeholder_12] => 1600237152 [:db_insert_placeholder_13] => 1600237152 [:db_insert_placeholder_14] => 1 [:db_insert_placeholder_15] => 1 ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 810 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php). Drupal\Core\Database\Statement->execute(Array, Array) (Line: 740) Drupal\Core\Database\Connection->query('INSERT INTO {media_field_data} ("mid", "vid", "bundle", "langcode", "status", "uid", "name", "thumbnail__target_id", "thumbnail__alt", "thumbnail__title", "thumbnail__width", "thumbnail__height", "created", "changed", "default_langcode", "revision_translation_affected") VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11, :db_insert_placeholder_12, :db_insert_placeholder_13, :db_insert_placeholder_14, :db_insert_placeholder_15)', Array, Array) (Line: 91) Drupal\Core\Database\Driver\mysql\Connection->query('INSERT INTO {media_field_data} ("mid", "vid", "bundle", "langcode", "status", "uid", "name", "thumbnail__target_id", "thumbnail__alt", "thumbnail__title", "thumbnail__width", "thumbnail__height", "created", "changed", "default_langcode", "revision_translation_affected") VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11, :db_insert_placeholder_12, :db_insert_placeholder_13, :db_insert_placeholder_14, :db_insert_placeholder_15)', Array, Array) (Line: 32) Drupal\Core\Database\Driver\mysql\Insert->execute() (Line: 1011) Drupal\Core\Entity\Sql\SqlContentEntityStorage->saveToSharedTables(Object) (Line: 949) Drupal\Core\Entity\Sql\SqlContentEntityStorage->doSaveFieldItems(Object) (Line: 622) Drupal\Core\Entity\ContentEntityStorageBase->doSave(NULL, Object) (Line: 452) Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 801) Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object) (Line: 29) Drupal\media\MediaStorage->save(Object) (Line: 339) Drupal\Core\Entity\EntityBase->save() (Line: 677) Drupal\media_library\Form\AddFormBase->submitForm(Array, Object) call_user_func_array(Array, Array) (Line: 113) Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 51) Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 593) Drupal\Core\Form\FormBuilder->processForm('media_library_add_form_upload', Array, Object) (Line: 321) Drupal\Core\Form\FormBuilder->buildForm('Drupal\media_library\Form\FileUploadForm', Object) (Line: 310) Drupal\media_library\MediaLibraryUiBuilder->buildMediaTypeAddForm(Object) (Line: 161) Drupal\media_library\MediaLibraryUiBuilder->buildLibraryContent(Object) (Line: 117) Drupal\media_library\MediaLibraryUiBuilder->buildUi(Object) call_user_func_array(Array, Array) (Line: 123) Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 573) Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124) Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97) Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 158) Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80) Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57) Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47) Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106) Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85) Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47) Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52) Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23) Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 705) Drupal\Core\DrupalKernel->handle(Object) (Line: 19) - Debugging you could find a captured exception with message "Calling undefined function Drupal\media\Entity\Media::getCurrentUserId"
Proposed resolution
Fix the configuration provided by this module on core.base_field_override.media.*.uid config entities, replacing Drupal\media\Entity\Media::getCurrentUserId by Drupal\media\Entity\Media::getDefaultEntityOwner
Remaining tasks
Test and review
User interface changes
None
API changes
None
Data model changes
core.base_field_override.media.*.uid config entities changing default_value_callback because the current callback is deprecated and has been removed on Drupal 9.x
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | varbase_media-3171237-2.patch | 4.73 KB | akalam |
Comments
Comment #2
akalam commentedComment #3
akalam commentedAn upgrade path is needed.
Comment #4
rajab natshahThanks David for reporting and patching
A hook update function is needed to change all core.base_field_override.media.*.uid
From
default_value_callback: 'Drupal\media\Entity\Media::getCurrentUserId'To
default_value_callback: 'Drupal\media\Entity\Media::getDefaultEntityOwner'Thank you again for helping.
Comment #5
rajab natshahComment #8
rajab natshahComment #9
rajab natshahComment #10
rajab natshahComment #11
rajab natshahThanks David for reporting the issue
Fixing all other
Deprecated The ::getCurrentUserId method is deprecated in 8.6.x and will be removed before 9.0.0.
like:
#3175756: Fix deprecated function getCurrentUserId replaced by getDefaultEntityOwner in core.base_field_override.node.landing_page_lb.uid config
Comment #14
rajab natshah#3175792: Fix deprecated function getCurrentUserId replaced by getDefaultEntityOwner for all content types and media types
#3175810: Fix deprecated function getCurrentUserId replaced by getDefaultEntityOwner in core.base_field_override.node.varbase_heroslider_media.uid config for Drupal 9 compatibility
#3175817: Fix deprecated function getCurrentUserId replaced by getDefaultEntityOwner in core.base_field_override.node.landing_page.uid config for Drupal 9 compatibility
Comment #15
rajab natshahComment #16
rajab natshah