0

I am trying to set a custom theme suggestion for the item-list template. It does work but I am getting an error message:

User error: "attributes" is an invalid render array key in Drupal\Core\Render\Element::children() (line 97 of core/lib/Drupal/Core/Render/Element.php).

Function:

function MYTHEME_theme_suggestions_item_list_alter(array &$suggestions, array $variables) { $moduleclass = str_replace('-', '_', $variables['attributes']['class']); $suggestions[] = 'item_list__' . $moduleclass; } 

Not sure what the probleme is here?

1 Answer 1

0

Every time it calls the hook, the variables may or may not have the array key 'attributes' - so the error happens on certain hook call, but not others. It's easy to fix, just check if it exists

function MYTHEME_theme_suggestions_item_list_alter(array &$suggestions, array $variables) { if (!empty($variables['attributes'])) { $moduleclass = str_replace('-', '_', $variables['attributes']['class']); $suggestions[] = 'item_list__' . $moduleclass; } } 
1
  • I tried this and with isset() too, but the error stays the same... Commented Jun 12, 2018 at 4:13

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.