Fix: Warning: foreach() argument must be of type array|object, null given in menu_link_attributes_menu_link_content_form_entity_builder()

Created on 3 November 2023, about 1 year ago

Problem/Motivation

There is an issue Warning: foreach() argument must be of type array|object, null given in menu_link_attributes_menu_link_content_form_entity_builder() (line 147 of modules/contrib/menu_link_attributes/menu_link_attributes.module). when adding a new menu item without providing any attribute value

Steps to reproduce

1. Create a new menu item for any menu and do not provide any attribute value for the item
2. Click the save button
3. There will be shown a warning message: Warning: foreach() argument must be of type array|object, null given in menu_link_attributes_menu_link_content_form_entity_builder() (line 147 of modules/contrib/menu_link_attributes/menu_link_attributes.module).

Proposed resolution

1. Prepare a proper patch in order to fix the issue
2. Prepare an MR for review

Remaining tasks

Write the fix and do the mentioned above.

User interface changes

No.

API changes

No.

Data model changes

No.

πŸ› Bug report
Status

Active

Version

1.3

Component

Code

Created by

πŸ‡²πŸ‡©Moldova andrei.vesterli Chisinau

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

Merge Requests

Comments & Activities

  • Issue created by @andrei.vesterli
  • πŸ‡²πŸ‡©Moldova andrei.vesterli Chisinau

    Here is the attached patch with the fix

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.4 + Environment: PHP 7.4 & MySQL 5.7
    last update about 1 year ago
    Composer require failure
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.4 + Environment: PHP 8.1 & MySQL 5.7 w/Bullseye Upgrades
    last update about 1 year ago
    1 pass
  • πŸ‡²πŸ‡©Moldova andrei.vesterli Chisinau
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.4 + Environment: PHP 7.4 & MySQL 5.7
    last update about 1 year ago
    Composer require failure
  • Issue was unassigned.
  • Status changed to Needs review about 1 year ago
  • πŸ‡²πŸ‡©Moldova andrei.vesterli Chisinau
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.4 + Environment: PHP 7.4 & MySQL 5.7
    last update about 1 year ago
    Composer require failure
  • πŸ‡¦πŸ‡ΉAustria agoradesign

    With the given patch from #2, there's the risk of running into an error:

    TypeError: array_merge(): Argument #2 must be of type array, null given in array_merge() (line 191

    This seems to happen, when the user does not have the 'use menu link attributes' permission set

    I've updated the patch to ensure an empty array, if the value is not set

  • πŸ‡¨πŸ‡ΏCzech Republic milos.kroulik

    Based on the patch in #6 it looks like this is actually a duplicate of https://www.drupal.org/project/menu_link_attributes/issues/3331630 πŸ› Compatibility with other modules, options being overwritten. Fixed , which is already RTBC. Can we close this one?

  • Status changed to Closed: duplicate 9 months ago
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    πŸ› Compatibility with other modules, options being overwritten. Fixed has already been fixed. Closing this as duplicate.

  • πŸ‡§πŸ‡¬Bulgaria alexrayu

    Sadly the other mentioned issue did not resolve this specific error - variables passed to NestedArray::MergeDeep() can still be NULL, which causes a warning. Reworked the previous patch to also check the second variable passed. If someone still has this error in ^1.5.0, you can check this patch.

  • First commit to issue fork.
  • πŸ‡§πŸ‡ͺBelgium dtfabio Ninove

    Hi Anybody,

    I am also still experiencing the same problem that the code gives a warning if the arrays passed to NestedArray::mergeDeep() contain a NULL value.

    In my case, this happened logged in as webmaster, given the webmaster had no permissions to add attributes to the menu link item.

    I have included alexrayu's code in an MR with one more check in case the user has no attribute add permissions, given that at this place a NULL was still passed to the mergeDeep function later in the code.

    Behaviour without patch:

    After adding the patch:

    Would it be possible to review the change, so that a solution to this problem can be included in a future release?

    Greetings,

    Fabio

  • Pipeline finished with Success
    2 months ago
    Total: 163s
    #351927
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    Thank you, yes, let's fix that.

  • Pipeline finished with Skipped
    2 months ago
    #351947
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    Fixed in dev!

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

  • Status changed to Fixed 16 days ago
  • πŸ‡ΊπŸ‡ΈUnited States alphex Atlanta, GA USA

    Drupal 10.4, applying this to the stable release, I'm still seeing the errors...

    Warning: foreach() argument must be of type array|object, null given in menu_link_attributes_menu_link_content_form_entity_builder() (line 155 of /app/web/modules/contrib/menu_link_attributes/menu_link_attributes.module)
    #0 /app/web/core/includes/bootstrap.inc(166): _drupal_error_handler_real(2, 'foreach() argum...', '/app/web/module...', 155)
    #1 /app/web/modules/contrib/menu_link_attributes/menu_link_attributes.module(155): _drupal_error_handler(2, 'foreach() argum...', '/app/web/module...', 155)
    #2 [internal function]: menu_link_attributes_menu_link_content_form_entity_builder('menu_link_conte...', Object(Drupal\menu_link_content\Entity\MenuLinkContent), Array, Object(Drupal\Core\Form\FormState))
    #3 /app/web/core/lib/Drupal/Core/Entity/EntityForm.php(307): call_user_func_array('menu_link_attri...', Array)
    #4 /app/web/core/lib/Drupal/Core/Entity/ContentEntityForm.php(155): Drupal\Core\Entity\EntityForm->buildEntity(Array, Object(Drupal\Core\Form\FormState))
    #5 /app/web/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php(130): Drupal\Core\Entity\ContentEntityForm->buildEntity(Array, Object(Drupal\Core\Form\FormState))
    #6 /app/web/core/lib/Drupal/Core/Entity/EntityForm.php(286): Drupal\menu_link_content\Form\MenuLinkContentForm->buildEntity(Array, Object(Drupal\Core\Form\FormState))
    #7 /app/web/core/lib/Drupal/Core/Entity/ContentEntityForm.php(145): Drupal\Core\Entity\EntityForm->submitForm(Array, Object(Drupal\Core\Form\FormState))
    #8 [internal function]: Drupal\Core\Entity\ContentEntityForm->submitForm(Array, Object(Drupal\Core\Form\FormState))
    #9 /app/web/core/lib/Drupal/Core/Form/FormSubmitter.php(129): call_user_func_array(Array, Array)
    #10 /app/web/core/lib/Drupal/Core/Form/FormSubmitter.php(67): Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object(Drupal\Core\Form\FormState))
    #11 /app/web/core/lib/Drupal/Core/Form/FormBuilder.php(597): Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object(Drupal\Core\Form\FormState))
    #12 /app/web/core/lib/Drupal/Core/Form/FormBuilder.php(326): Drupal\Core\Form\FormBuilder->processForm('menu_link_conte...', Array, Object(Drupal\Core\Form\FormState))
    #13 /app/web/core/lib/Drupal/Core/Entity/EntityFormBuilder.php(48): Drupal\Core\Form\FormBuilder->buildForm(Object(Drupal\menu_link_content\Form\MenuLinkContentForm), Object(Drupal\Core\Form\FormState))
    #14 /app/web/modules/contrib/menu_item_extras/src/Controller/MenuController.php(62): Drupal\Core\Entity\EntityFormBuilder->getForm(Object(Drupal\menu_link_content\Entity\MenuLinkContent))
    #15 [internal function]: Drupal\menu_item_extras\Controller\MenuController->addLink(Object(Drupal\system\Entity\Menu))
    #16 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
    #17 /app/web/core/lib/Drupal/Core/Render/Renderer.php(638): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
    #18 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(121): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
    #19 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
    #20 /app/vendor/symfony/http-kernel/HttpKernel.php(181): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
    #21 /app/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
    #22 /app/web/core/lib/Drupal/Core/StackMiddleware/Session.php(53): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #23 /app/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #24 /app/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #25 /app/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(116): Drupal\Core\StackMiddleware\ContentLength->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #26 /app/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(90): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #27 /app/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #28 /app/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #29 /app/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #30 /app/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #31 /app/web/core/lib/Drupal/Core/DrupalKernel.php(741): Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #32 /app/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
    #33 {main}
    
    Warning: foreach() argument must be of type array|object, null given in menu_link_attributes_menu_link_content_form_entity_builder() (line 173 of /app/web/modules/contrib/menu_link_attributes/menu_link_attributes.module)
    #0 /app/web/core/includes/bootstrap.inc(166): _drupal_error_handler_real(2, 'foreach() argum...', '/app/web/module...', 173)
    #1 /app/web/modules/contrib/menu_link_attributes/menu_link_attributes.module(173): _drupal_error_handler(2, 'foreach() argum...', '/app/web/module...', 173)
    #2 [internal function]: menu_link_attributes_menu_link_content_form_entity_builder('menu_link_conte...', Object(Drupal\menu_link_content\Entity\MenuLinkContent), Array, Object(Drupal\Core\Form\FormState))
    #3 /app/web/core/lib/Drupal/Core/Entity/EntityForm.php(307): call_user_func_array('menu_link_attri...', Array)
    #4 /app/web/core/lib/Drupal/Core/Entity/ContentEntityForm.php(155): Drupal\Core\Entity\EntityForm->buildEntity(Array, Object(Drupal\Core\Form\FormState))
    #5 /app/web/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php(130): Drupal\Core\Entity\ContentEntityForm->buildEntity(Array, Object(Drupal\Core\Form\FormState))
    #6 /app/web/core/lib/Drupal/Core/Entity/EntityForm.php(286): Drupal\menu_link_content\Form\MenuLinkContentForm->buildEntity(Array, Object(Drupal\Core\Form\FormState))
    #7 /app/web/core/lib/Drupal/Core/Entity/ContentEntityForm.php(145): Drupal\Core\Entity\EntityForm->submitForm(Array, Object(Drupal\Core\Form\FormState))
    #8 [internal function]: Drupal\Core\Entity\ContentEntityForm->submitForm(Array, Object(Drupal\Core\Form\FormState))
    #9 /app/web/core/lib/Drupal/Core/Form/FormSubmitter.php(129): call_user_func_array(Array, Array)
    #10 /app/web/core/lib/Drupal/Core/Form/FormSubmitter.php(67): Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object(Drupal\Core\Form\FormState))
    #11 /app/web/core/lib/Drupal/Core/Form/FormBuilder.php(597): Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object(Drupal\Core\Form\FormState))
    #12 /app/web/core/lib/Drupal/Core/Form/FormBuilder.php(326): Drupal\Core\Form\FormBuilder->processForm('menu_link_conte...', Array, Object(Drupal\Core\Form\FormState))
    #13 /app/web/core/lib/Drupal/Core/Entity/EntityFormBuilder.php(48): Drupal\Core\Form\FormBuilder->buildForm(Object(Drupal\menu_link_content\Form\MenuLinkContentForm), Object(Drupal\Core\Form\FormState))
    #14 /app/web/modules/contrib/menu_item_extras/src/Controller/MenuController.php(62): Drupal\Core\Entity\EntityFormBuilder->getForm(Object(Drupal\menu_link_content\Entity\MenuLinkContent))
    #15 [internal function]: Drupal\menu_item_extras\Controller\MenuController->addLink(Object(Drupal\system\Entity\Menu))
    #16 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
    #17 /app/web/core/lib/Drupal/Core/Render/Renderer.php(638): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
    #18 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(121): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
    #19 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
    #20 /app/vendor/symfony/http-kernel/HttpKernel.php(181): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
    #21 /app/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
    #22 /app/web/core/lib/Drupal/Core/StackMiddleware/Session.php(53): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #23 /app/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #24 /app/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #25 /app/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(116): Drupal\Core\StackMiddleware\ContentLength->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #26 /app/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(90): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #27 /app/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #28 /app/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #29 /app/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #30 /app/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #31 /app/web/core/lib/Drupal/Core/DrupalKernel.php(741): Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #32 /app/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
    #33 {main}
    .
    
    
Production build 0.71.5 2024