TypeError: Cannot access offset of type string on string

Created on 2 August 2021, over 3 years ago
Updated 15 March 2023, over 1 year ago

Problem/Motivation

Using Drupal 9 in XAMPP with PHP 8.

I have a local environment set up. The plugin has been great overall and I'd like to continue to use it. However, it keeps breaking the front end of my site.

The website encountered an unexpected error. Please try again later.

I've noticed it happens for various reasons, such as creating content or changing the main menu structure in Admin. I've confirmed that this plugin is causing issues - for two reasons. One, I disabled the module and the site works again. Two, the error that I'm getting when the site crashes has to do with the WeMegaMenuBuilder.php file.

Here's the error:

TypeError: Cannot access offset of type string on string in Drupal\we_megamenu\WeMegaMenuBuilder::compareItem() (line 899 of C:\xampp\htdocs\drupal_9\modules\we_megamenu\src\WeMegaMenuBuilder.php)
#0 C:\xampp\htdocs\drupal_9\modules\we_megamenu\src\WeMegaMenuBuilder.php(958): Drupal\we_megamenu\WeMegaMenuBuilder::compareItem(Array, 0, Array, Object(stdClass))
#1 C:\xampp\htdocs\drupal_9\modules\we_megamenu\we_megamenu.module(228): Drupal\we_megamenu\WeMegaMenuBuilder::updateMegamenuFromDrupalMenu(Object(stdClass), Array, 0)
#2 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\Theme\ThemeManager.php(287): template_preprocess_we_megamenu_frontend(Array, 'we_megamenu_fro...', Array)
#3 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\Render\Renderer.php(436): Drupal\Core\Theme\ThemeManager->render('we_megamenu_fro...', Array)
#4 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\Render\Renderer.php(201): Drupal\Core\Render\Renderer->doRender(Array, false)
#5 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\Template\TwigExtension.php(450): Drupal\Core\Render\Renderer->render(Array)
#6 C:\xampp\htdocs\drupal_9\sites\default\files\php\twig\6108458973378_block.html.twig_pdcrmDGeH9CKm3I-8bYAYuaDq\iawWns3hX-9E8PIR0jMn_ps3bzRO5-ngY92TBnWJcD0.php(78): Drupal\Core\Template\TwigExtension->escapeFilter(Object(Drupal\Core\Template\TwigEnvironment), Array, 'html', NULL, true)
#7 C:\xampp\htdocs\drupal_9\vendor\twig\twig\src\Template.php(182): __TwigTemplate_f2ab20f6df919080aa72ef9ce657e354cb9ec5ffd4e02a8bf90455da646ef700->block_content(Array, Array)
#8 C:\xampp\htdocs\drupal_9\sites\default\files\php\twig\6108458973378_block.html.twig_pdcrmDGeH9CKm3I-8bYAYuaDq\iawWns3hX-9E8PIR0jMn_ps3bzRO5-ngY92TBnWJcD0.php(65): Twig\Template->displayBlock('content', Array, Array)
#9 C:\xampp\htdocs\drupal_9\vendor\twig\twig\src\Template.php(405): __TwigTemplate_f2ab20f6df919080aa72ef9ce657e354cb9ec5ffd4e02a8bf90455da646ef700->doDisplay(Array, Array)
#10 C:\xampp\htdocs\drupal_9\vendor\twig\twig\src\Template.php(378): Twig\Template->displayWithErrorHandling(Array, Array)
#11 C:\xampp\htdocs\drupal_9\vendor\twig\twig\src\Template.php(390): Twig\Template->display(Array)
#12 C:\xampp\htdocs\drupal_9\core\themes\engines\twig\twig.engine(65): Twig\Template->render(Array)
#13 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\Theme\ThemeManager.php(384): twig_render_template('themes/tara/tem...', Array)
#14 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\Render\Renderer.php(436): Drupal\Core\Theme\ThemeManager->render('block', Array)
#15 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\Render\Renderer.php(449): Drupal\Core\Render\Renderer->doRender(Array)
#16 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\Render\Renderer.php(201): Drupal\Core\Render\Renderer->doRender(Array, false)
#17 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\Template\TwigExtension.php(450): Drupal\Core\Render\Renderer->render(Array)
#18 C:\xampp\htdocs\drupal_9\sites\default\files\php\twig\6108458973378_header.html.twig_6I5QODE7c7GQ4Ms0hECp0seYw\HrlxfrZzPUOG2twW8Pj1S4iX8S7TeRQ9wK4YrJB1IWE.php(86): Drupal\Core\Template\TwigExtension->escapeFilter(Object(Drupal\Core\Template\TwigEnvironment), Array, 'html', NULL, true)
#19 C:\xampp\htdocs\drupal_9\vendor\twig\twig\src\Template.php(405): __TwigTemplate_b316dbdc67f0fe61b60c59c4b52509f126ded77eaa278cfbb1d5a1d7f1243552->doDisplay(Array, Array)
#20 C:\xampp\htdocs\drupal_9\vendor\twig\twig\src\Template.php(378): Twig\Template->displayWithErrorHandling(Array, Array)
#21 C:\xampp\htdocs\drupal_9\sites\default\files\php\twig\6108458973378_page--front.html.twig_NDpeI80VDMcUACXlgS22uBjoV\4DGTDsv7X9cB_AB8CX1v6iaptBniinaUEDx1xtmXjFM.php(39): Twig\Template->display(Array)
#22 C:\xampp\htdocs\drupal_9\vendor\twig\twig\src\Template.php(405): __TwigTemplate_733e6301606c43823cd436b9a4c7f99ac0b96c65d4c1f006df656d7721a1e246->doDisplay(Array, Array)
#23 C:\xampp\htdocs\drupal_9\vendor\twig\twig\src\Template.php(378): Twig\Template->displayWithErrorHandling(Array, Array)
#24 C:\xampp\htdocs\drupal_9\vendor\twig\twig\src\Template.php(390): Twig\Template->display(Array)
#25 C:\xampp\htdocs\drupal_9\core\themes\engines\twig\twig.engine(65): Twig\Template->render(Array)
#26 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\Theme\ThemeManager.php(384): twig_render_template('themes/tara/tem...', Array)
#27 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\Render\Renderer.php(436): Drupal\Core\Theme\ThemeManager->render('page', Array)
#28 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\Render\Renderer.php(201): Drupal\Core\Render\Renderer->doRender(Array, false)
#29 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\Template\TwigExtension.php(450): Drupal\Core\Render\Renderer->render(Array)
#30 C:\xampp\htdocs\drupal_9\sites\default\files\php\twig\6108458973378_html.html.twig_oKEnPiC_fnGTN4LEu-gAyCAUf\QyYIju3S6r3EfV4LGZVWx8Iu_Gnq6gfmCdEsifmgucI.php(110): Drupal\Core\Template\TwigExtension->escapeFilter(Object(Drupal\Core\Template\TwigEnvironment), Array, 'html', NULL, true)
#31 C:\xampp\htdocs\drupal_9\vendor\twig\twig\src\Template.php(405): __TwigTemplate_d26ae87dabf1c26bcce041e5c599e21212e424efe55a10dc9bc12c04add0c90e->doDisplay(Array, Array)
#32 C:\xampp\htdocs\drupal_9\vendor\twig\twig\src\Template.php(378): Twig\Template->displayWithErrorHandling(Array, Array)
#33 C:\xampp\htdocs\drupal_9\vendor\twig\twig\src\Template.php(390): Twig\Template->display(Array)
#34 C:\xampp\htdocs\drupal_9\core\themes\engines\twig\twig.engine(65): Twig\Template->render(Array)
#35 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\Theme\ThemeManager.php(384): twig_render_template('themes/tara/tem...', Array)
#36 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\Render\Renderer.php(436): Drupal\Core\Theme\ThemeManager->render('html', Array)
#37 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\Render\Renderer.php(201): Drupal\Core\Render\Renderer->doRender(Array, false)
#38 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\Render\MainContent\HtmlRenderer.php(162): Drupal\Core\Render\Renderer->render(Array)
#39 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\Render\Renderer.php(578): Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}()
#40 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\Render\MainContent\HtmlRenderer.php(163): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#41 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\EventSubscriber\MainContentViewSubscriber.php(90): Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\CurrentRouteMatch))
#42 [internal function]: Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object(Symfony\Component\HttpKernel\Event\ViewEvent), 'kernel.view', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#43 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher.php(142): call_user_func(Array, Object(Symfony\Component\HttpKernel\Event\ViewEvent), 'kernel.view', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#44 C:\xampp\htdocs\drupal_9\vendor\symfony\http-kernel\HttpKernel.php(163): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object(Symfony\Component\HttpKernel\Event\ViewEvent), 'kernel.view')
#45 C:\xampp\htdocs\drupal_9\vendor\symfony\http-kernel\HttpKernel.php(80): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#46 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\StackMiddleware\Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#47 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\StackMiddleware\KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#48 C:\xampp\htdocs\drupal_9\core\modules\page_cache\src\StackMiddleware\PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#49 C:\xampp\htdocs\drupal_9\core\modules\page_cache\src\StackMiddleware\PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#50 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\StackMiddleware\ReverseProxyMiddleware.php(47): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#51 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\StackMiddleware\NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#52 C:\xampp\htdocs\drupal_9\vendor\stack\builder\src\Stack\StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#53 C:\xampp\htdocs\drupal_9\core\lib\Drupal\Core\DrupalKernel.php(716): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#54 C:\xampp\htdocs\drupal_9\index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#55 {main}

Steps to reproduce

Change the weight of submenu items. It might break your site, as it has mine.
Check recent log messages to see if you are getting a similar error.

Proposed resolution

I may try to revert my version of PHP. Other than that, I'm not sure what to do.

Thanks in advance.

πŸ› Bug report
Status

Needs review

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States rraney

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ͺπŸ‡ΈSpain marthinal

    We can reproduce this issue by updating the main menu (/admin/structure/menu/manage/main). The only change in the DB is that menu_update_flag is 1.

    And we are saving it again from template_preprocess_we_megamenu_frontend()

        if(isset($data_config->menu_update_flag) && $data_config->menu_update_flag)  {
          // drupal_flush_all_caches();
          $theme_name = $block_theme;
          $menu_items = WeMegaMenuBuilder::getMenuTreeOrder($menu_name, FALSE);
          WeMegaMenuBuilder::updateMegamenuFromDrupalMenu($data_config, $menu_items, 0);
          $data_config->menu_update_flag = 0;
          WeMegaMenuBuilder::saveConfig($menu_name, $theme_name, json_encode($data_config));
          // drupal_flush_all_caches();      
        }
    

    I think 110c5e22 is the commit where the bug was introduced.

  • πŸ‡ͺπŸ‡¨Ecuador jwilson3

    Agree with #16. It is really unclear why we're resaving the menu from a frontend preprocess function (which is incidentally a copy of the same code from the backend preprocess). This seems out of place without any code comments. Amazing that the approach in #16 cleans up the code smell and actually works. I don't claim to understand the refactoring in 110c5e22, there is no issue and nothing clear here. It feels like this module is being adapted over time for the maintainers use case without taking a standard Drupal.org coding practices into account.

  • Status changed to Needs work over 1 year ago
  • πŸ‡ͺπŸ‡ΈSpain marthinal

    Yep, same issue from backend.

  • First commit to issue fork.
  • πŸ‡¬πŸ‡§United Kingdom rjjakes

    Due to all the recent merges to 8.x-1.x this needs rebasing but has conflicts

  • First commit to issue fork.
  • πŸ‡­πŸ‡ΊHungary szato

    Rebased

  • Status changed to RTBC over 1 year ago
  • Status changed to Fixed over 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom rjjakes

    Merged to 8.x-1.x

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

  • Status changed to Fixed about 2 months ago
  • This has not been fixed, it still happens as of 1.16

Production build 0.71.5 2024