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...
    – scar
    Jun 12, 2018 at 4:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.