Problem/Motivation

A PHP notice can be thrown when convertAttributes() expects the array key below to exist, but it doesn't.
The key might not exist in the edge case where a custom toolbar link is added and it's set to expanded, but it doesn't have any children.

Steps to reproduce

/**
 * Implements hook_toolbar_alter().
 */
function hook_toolbar_alter(&$items) {
  if (!empty($items['administration']['tray']['toolbar_administration']))) {
    $items['administration']['tray']['toolbar_administration']['#pre_render'][] = 'some_function_add_dashboard';
  }
}

function some_function_add_dashboard(array $build) {
  $build['administration_menu']['#items']['dashboard'] = [
    'is_expanded' => TRUE,
    'title' => t('Dashboard'),
    'url' => Url::fromRoute('some_module.dashboard', [], [
        'attributes' => [
          'title' => t('Dashboard'),
          'class' => ['toolbar-icon'],
        ],
      ]
    ),
    'attributes' => new Attribute([
      'class' => [
        'menu-item',
        'menu-item--expanded',
      ],
    ])
  ];
  return $build;
}

If the item is not set to expanded, the notice is not thrown.

Proposed resolution

Add a defensive check around the lines checking the array key in convertAttributes().

Remaining tasks

n/a

User interface changes

n/a

API changes

n/a

Data model changes

n/a

Comments

bramtenhove created an issue. See original summary.

bramtenhove’s picture

And the patch attached.

bandanasharma’s picture

Status: Active » Needs review
abhibhatt’s picture

please add the step for reproduce this issue.

markhalliwell’s picture

Version: 8.x-4.x-dev » 8.x-3.x-dev
Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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