LogicException: Cannot create key "fields" on non-array value. in Drupal\Component\Utility\NestedArray::setValue()

Created on 29 May 2025, 2 months ago

Problem/Motivation

If a Media entity has a table field attached, it is not possible to save it in the Media Library Overlay.

This is the full error stack from the test environment:

#0 /var/lib/tugboat/stm/web/modules/contrib/tablefield/src/Element/Tablefield.php(382): Drupal\Component\Utility\NestedArray::setValue(Array, Array, Array)
#1 [internal function]: Drupal\tablefield\Element\Tablefield::submitCallbackRebuild(Array, Object(Drupal\Core\Form\FormState))
#2 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/Form/FormSubmitter.php(129): call_user_func_array(Array, Array)
#3 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/Form/FormSubmitter.php(67): Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object(Drupal\Core\Form\FormState))
#4 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/Form/FormBuilder.php(597): Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object(Drupal\Core\Form\FormState))
#5 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/Form/FormBuilder.php(326): Drupal\Core\Form\FormBuilder->processForm('media_library_a...', Array, Object(Drupal\Core\Form\FormState))
#6 /var/lib/tugboat/stm/web/core/modules/media_library/src/MediaLibraryUiBuilder.php(315): Drupal\Core\Form\FormBuilder->buildForm('Drupal\\media_li...', Object(Drupal\Core\Form\FormState))
#7 /var/lib/tugboat/stm/web/core/modules/media_library/src/MediaLibraryUiBuilder.php(162): Drupal\media_library\MediaLibraryUiBuilder->buildMediaTypeAddForm(Object(Drupal\media_library\MediaLibraryState))
#8 /var/lib/tugboat/stm/web/core/modules/media_library/src/MediaLibraryUiBuilder.php(128): Drupal\media_library\MediaLibraryUiBuilder->buildLibraryContent(Object(Drupal\media_library\MediaLibraryState))
#9 [internal function]: Drupal\media_library\MediaLibraryUiBuilder->buildUi(Object(Drupal\media_library\MediaLibraryState))
#10 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
#11 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/Render/Renderer.php(638): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#12 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(121): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#13 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
#14 /var/lib/tugboat/stm/vendor/symfony/http-kernel/HttpKernel.php(181): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#15 /var/lib/tugboat/stm/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#16 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/StackMiddleware/Session.php(53): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#17 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#18 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#19 /var/lib/tugboat/stm/web/core/modules/big_pipe/src/StackMiddleware/ContentLength.php(32): Drupal\Core\StackMiddleware\ContentLength->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#20 /var/lib/tugboat/stm/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(116): Drupal\big_pipe\StackMiddleware\ContentLength->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#21 /var/lib/tugboat/stm/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(90): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#22 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#23 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#24 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#25 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#26 /var/lib/tugboat/stm/web/core/lib/Drupal/Core/DrupalKernel.php(741): Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#27 /var/lib/tugboat/stm/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#28 {main}

Steps to reproduce

* go to https://master-kf1w1npnafnzgumjzoewfikimbmlmonw.tugboatqa.com/user
* login as admin admin
* add an article: https://master-kf1w1npnafnzgumjzoewfikimbmlmonw.tugboatqa.com/node/add/a...
* Try to create a new image via the media library overlay (It is not possible)

Proposed resolution

There appears to be some strange form issue in the meda library overlay, potetnially beciasethe entity is not jet saved at this point of time. It does work via the direct media form: https://master-kf1w1npnafnzgumjzoewfikimbmlmonw.tugboatqa.com/media/add/...

🐛 Bug report
Status

Active

Version

3.0

Component

Code

Created by

🇩🇪Germany Schoenef Unna

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Schoenef
  • 🇩🇪Germany Schoenef Unna

    I did a bit of debugging and for the working /media/add form $triggering_element['#name'] is "tablefield-paste-field_data_values-0"

    It entes in Tablefield.php line 403:

    elseif (isset($triggering_element['#name']) && $triggering_element['#name'] == 'tablefield-paste-' . $id) {
    

    For the media overlay (not working) $triggering_element['#name'] is "tablefield-paste-media-0-fields-field_data_values-0", it also enters line 403.

    The issue occurs in the rebuild call, because media[0] is suddenly a MediaEntity, not an aray
    0 = "media"
    1 = {int} 0 <--- this returns an instance of Drupal\media\Entity\Media
    2 = "fields"
    3 = "field_data_values"
    4 = {int} 0
    5 = "tablefield"
    6 = "rebuild"

    It is of course not an array anymore.

    Sugestion is to check for an entity and pull the fieldvalues into an array in this case.

  • 🇩🇪Germany Schoenef Unna

    Adjusting core appears not be the solution here, I think the second call to

    NestedArray::setValue($form_state->getStorage(), $parents, $imported_tablefield['rebuild']);
    

    Should not use NestedArray but might need a custom implementation, as the storage can contain entities.

  • 🇩🇪Germany Schoenef Unna

    This patch fixed it for me:

Production build 0.71.5 2024