function SettingsTrayHooks::toolbarAlter

Implements hook_toolbar_alter().

Alters the 'contextual' toolbar tab if it exists (meaning the user is allowed to use contextual links) and if they can administer blocks.

@todo Remove the "administer blocks" requirement in https://www.drupal.org/node/2822965.

See also

contextual_toolbar()

File

core/modules/settings_tray/src/Hook/SettingsTrayHooks.php, line 108

Class

SettingsTrayHooks
Hook implementations for settings_tray.

Namespace

Drupal\settings_tray\Hook

Code

public function toolbarAlter(&$items) {
    $items['contextual']['#cache']['contexts'][] = 'user.permissions';
    if (isset($items['contextual']['tab']) && \Drupal::currentUser()->hasPermission('administer blocks')) {
        $items['contextual']['#weight'] = -1000;
        $items['contextual']['#attached']['library'][] = 'settings_tray/drupal.settings_tray';
        $items['contextual']['tab']['#attributes']['data-drupal-settingstray'] = 'toggle';
        // Set a class on items to mark whether they should be active in edit mode.
        // @todo Create a dynamic method for modules to set their own items.
        //   https://www.drupal.org/node/2784589.
        $edit_mode_items = [
            'contextual',
        ];
        foreach ($items as $key => $item) {
            if (!in_array($key, $edit_mode_items) && (!isset($items[$key]['#wrapper_attributes']['class']) || !in_array('hidden', $items[$key]['#wrapper_attributes']['class']))) {
                $items[$key]['#wrapper_attributes']['class'][] = 'edit-mode-inactive';
            }
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.