Error on hook_theme_suggestions_HOOK_alter() 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 &amp;$suggestions, array $variables) {
  $moduleclass = str_replace('-', '_', $variables['attributes']['class']);
  $suggestions[] = 'item_list__' . $moduleclass;
}


Not sure what the probleme is here?
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 &amp;$suggestions, array $variables) {
  if (!empty($variables['attributes'])) {
    $moduleclass = str_replace('-', '_', $variables['attributes']['class']);
    $suggestions[] = 'item_list__' . $moduleclass;
  }
}