Problem/Motivation

Drupal core: 8.7.0-beta1
Bootstrap: 3.17.0
fontawesome_menu_icons: 1.5.0

When switching to non-bootstrap themes, such as Bartick, sub-theme of AdaptiveTheme, the errors as blew are gone. So I am wondering this is a bug of the Bootstrap theme.

Warning: assert(): $attributes, if set, must be an array or object of type \Drupal\Core\Template\Attribute failed in Drupal\Core\Template\TwigExtension->getLink() (line 285 of /var/www/html/web/core/lib/Drupal/Core/Template/TwigExtension.php) #0 /var/www/html/web/core/includes/bootstrap.inc(587): _drupal_error_handler_real(2, 'assert(): $attr...', '/var/www/html/w...', 285, Array) #1 [internal function]: _drupal_error_handler(2, 'assert(): $attr...', '/var/www/html/w...', 285, Array) #2 /var/www/html/web/core/lib/Drupal/Core/Template/TwigExtension.php(285): assert(false, '$attributes, if...') #3 /var/www/html/web/sites/default/files/php/twig/5ca0db9f2cd99_menu.html.twig_VUQ6iE4VQToBSVI9Bfo_dn2WZ/hy8u3Eo9C2k_NpuyYBd6kh3wsa0qpyBsqI3EjezVbAw.php(145): Drupal\Core\Template\TwigExtension->getLink('Expense Request...', Object(Drupal\Core\Url), NULL) #4 /var/www/html/vendor/twig/twig/src/Template.php(673): __TwigTemplate_1cdc7ede551542f24a1d35b3ce5dabeb1d4fde609ae1e6babc47569282368a5d->getmenu_links(Array, Object(Drupal\Core\Template\Attribute), 1, Array, Array) #5
...

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jungle created an issue. See original summary.

jungle’s picture

Assigned: jungle » Unassigned
Status: Active » Needs review
FileSize
851 bytes
jungle’s picture

Status: Needs review » Needs work

Sorry, the patch not working

imclean’s picture

src/templates/menu.html.twig looks like a good place to start.

imclean’s picture

imclean’s picture

This occurs with menu_level > 0. Parameter 3 "attributes" is null when being passed to Drupal\Core\Template\TwigExtension->getLink() in the compiled twig template:

$this->env->getExtension('Drupal\Core\Template\TwigExtension')->escapeFilter
  ($this->env, $this->env->getExtension('Drupal\Core\Template\TwigExtension')
    ->getLink(
      $this->sandbox->ensureToStringAllowed(($context["link_title"] ?? null)),
      $this->sandbox->ensureToStringAllowed($this->getAttribute($context["item"], "url", [])), 
      $this->sandbox->ensureToStringAllowed($this->getAttribute(($context["link_attributes"] ?? null), "addClass", [0 => (($this->getAttribute($context["item"], "in_active_trail", [])) ? ("active-trail") : (""))], "method")))
   , "html", null, true);
imclean’s picture

Status: Needs work » Needs review
FileSize
1.14 KB

This happens because the prepropress method isn't executed for the submenus so the attributes aren't added. This is a proof of concept which should resolve the issue.

jungle’s picture

Issue summary: View changes
FileSize
1.13 KB

Thanks Ian, the patch works for me.

removed trailing whitespaces:

diff menu_items_encounter_null_attributes-3045621-7.patch menu_items_encounter_null_attributes-3045621-8.patch
13c13
< +  protected function addAttributes(array &$items) {
---
> +  protected function addAttributes(array &$items) {
markhalliwell’s picture

Status: Needs review » Needs work
+++ b/src/Plugin/Preprocess/Menu.php
@@ -20,7 +20,11 @@ class Menu extends PreprocessBase implements PreprocessInterface {
+  protected function addAttributes(array &$items) {

Missing docblock, also method name is misleading. It's not actually adding attributes, it's converting them.

imclean’s picture

Status: Needs work » Needs review
FileSize
1.38 KB
imclean’s picture

Missed something in my haste.

imclean’s picture

Docblock doesn't need to be so verbose.

komlenic’s picture

Status: Needs review » Reviewed & tested by the community

I just encountered precisely this issue when upgrading from core 8.6.15 to 8.7.0. The patch in #12 fixes the issue.

rjl’s picture

Just as komlenic (#13), I had the same problem after upgrading to 8.7.0 and the patch in #12 also worked for me. Thanks.

Leo Pitt’s picture

Same as rjl and komlenic. Encountered issue in upgrade to 8.7.0. Patch in #12 resolved it. Thanks.

imclean’s picture

It would be useful to find out why this has changed between 8.6.x and 8.7.x.

ñull’s picture

When switching to non-bootstrap themes, such as Bartick, sub-theme of AdaptiveTheme, the errors as blew are gone. So I am wondering this is a bug of the Bootstrap theme.

I tried Bartik but had the same error there. Would need to try with a clean install to verify if this is not a core issue, but have no time to do so now. #12 fixes it at least for Bootstrap

  • markcarver committed c9203d8 on 8.x-3.x authored by imclean
    Issue #3045621 by imclean, jungle: Menu items encounter NULL attributes
    
markhalliwell’s picture

Status: Reviewed & tested by the community » Fixed
chris5156’s picture

Same issue as others when upgrading to 8.7.0 and using Bootstrap theme. The patch in #12 fixed the issue for me too.

astringer’s picture

@imclean

It would be useful to find out why this has changed between 8.6.x and 8.7.x.

I was curious too.

https://www.drupal.org/project/drupal/issues/2917653

TLDR; Because depending on whether or not Toolbar was enabled, $variables['attributes'] could be an array or an object.

Thanks for the patch.

firfin’s picture

Many people updating to 8.7 will run into this. Maybe time for a new release?

komlenic’s picture

I don't know if it fits into any other plans, but I'll second the notion that this may be a strong case for a release, considering the number of sites that will likely be affected when updating to 8.7.

  • markcarver committed c9203d8 on 8.x-4.x authored by imclean
    Issue #3045621 by imclean, jungle: Menu items encounter NULL attributes
    

Status: Fixed » Closed (fixed)

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

rfletcher73’s picture

I just encountered this error today. I am using the uswds_base theme. I've created a subtheme. I added the user account menu to a region in my page called "header_user_menu".

My site totally bombs when I add that menu to the block layout. Removing the menu, all is good. I need a block that has the login/logout button.

I'm using the latest version of Drupal Version 8.7.6. I'm new to Drupal.

This looks like the same error, please open this issue:

assert(, '$attributes, if set, must be an array or object of type \Drupal\Core\Template\Attribute') (Line: 285)
Drupal\Core\Template\TwigExtension->getLink('Home', Object, NULL) (Line: 145)
__TwigTemplate_ebe5d2d4741926f5c748b2026878fb71a2aed1f6f1eb048f5184f8e93990c1c2->getmenu_links(Array, Object, 0, Array, Array) (Line: 673)
Twig\Template->getAttribute(Object, 'menu_links', Array, 'method') (Line: 58)
__TwigTemplate_ebe5d2d4741926f5c748b2026878fb71a2aed1f6f1eb048f5184f8e93990c1c2->doDisplay(Array, Array) (Line: 427)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 398)
Twig\Template->display(Array) (Line: 406)
Twig\Template->render(Array) (Line: 64)
twig_render_template('themes/uswds_base/templates/navigation/menu.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('menu', Array) (Line: 437)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 501)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 102)
__TwigTemplate_48ac6d792241743d2da6195f3c08ddcfe0c255d3a83554abf0dfed4b05856840->block_content(Array, Array) (Line: 216)
Twig\Template->displayBlock('content', Array, Array) (Line: 91)
__TwigTemplate_48ac6d792241743d2da6195f3c08ddcfe0c255d3a83554abf0dfed4b05856840->doDisplay(Array, Array) (Line: 427)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 398)
Twig\Template->display(Array) (Line: 406)
Twig\Template->render(Array) (Line: 64)
twig_render_template('themes/uswds_base/templates/block/block--system-menu-block.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('block', Array) (Line: 437)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 450)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 501)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 87)
__TwigTemplate_a97695f2bfab3df337136452f60f142df660f462cb5773246abbd963fd2721e7->doDisplay(Array, Array) (Line: 427)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 398)
Twig\Template->display(Array) (Line: 406)
Twig\Template->render(Array) (Line: 64)
twig_render_template('themes/takeheart_subtheme/templates/layout/page--front.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('page', Array) (Line: 437)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 501)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 96)
__TwigTemplate_0e498e72659ab0cc9e742da7ba0caba7c594ee3b01563321a853ca35850828fc->doDisplay(Array, Array) (Line: 427)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 398)
Twig\Template->display(Array) (Line: 406)
Twig\Template->render(Array) (Line: 64)
twig_render_template('themes/takeheart_subtheme/templates/layout/html.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('html', Array) (Line: 437)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 147)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 148)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 156)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
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: 693)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
imclean’s picture

@rfletcher,

I am using the uswds_base theme...

This is the Bootstrap issue queue. Create a new issue in the USWDS Base theme issue queue.

This issue is also closed.