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

CommentFileSizeAuthor
#2 varbase_media-3171237-2.patch4.73 KBakalam

Comments

akalam created an issue. See original summary.

akalam’s picture

Status: Active » Needs review
StatusFileSize
new4.73 KB
akalam’s picture

Status: Needs review » Needs work

An upgrade path is needed.

rajab natshah’s picture

Title: Integrity constraint violation: 1048 Column 'uid' cannot be null: INSERT INTO {media_field_data}... on Drupal 9 » Fix deprecated function getCurrentUserId() replaced by getDefaultEntityOwner() in core.base_field_override.media.*.uid configs
Assigned: Unassigned » rajab natshah
Status: Needs work » Active

Thanks 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.

rajab natshah’s picture

Title: Fix deprecated function getCurrentUserId() replaced by getDefaultEntityOwner() in core.base_field_override.media.*.uid configs » Fix deprecated function getCurrentUserId replaced by getDefaultEntityOwner in core.base_field_override.media.*.uid configs

  • RajabNatshah committed 11db400 on 8.x-7.x authored by akalam
    Issue #3171237 by akalam: Fix deprecated function getCurrentUserId...

  • RajabNatshah committed f19ae76 on 8.x-7.x
    Issue #3171237 by akalam, RajabNatshah: Fix deprecated function...
rajab natshah’s picture

Assigned: rajab natshah » mohammed j. razem
Status: Active » Needs review
Issue tags: +varbase-8.8.7, +varbase-9.0.0
rajab natshah’s picture

Assigned: mohammed j. razem » Unassigned
rajab natshah’s picture

Status: Needs review » Fixed

  • RajabNatshah committed 3174677 on 9.0.x
    Issue #3171237: Fix deprecated function getCurrentUserId replaced by...

  • RajabNatshah committed d9c878a on 8.x-7.x
    Issue #3171237: Fix deprecated function getCurrentUserId replaced by...
rajab natshah’s picture

Title: Fix deprecated function getCurrentUserId replaced by getDefaultEntityOwner in core.base_field_override.media.*.uid configs » Fix deprecated function getCurrentUserId replaced by getDefaultEntityOwner in core.base_field_override.media.*.uid configs for Drupal 9 compatibility
rajab natshah’s picture

Issue tags: +varbase-9.0.0-alpha1

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.