You are here

function settings_tray_toolbar_alter in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/settings_tray/settings_tray.module \settings_tray_toolbar_alter()
  2. 9 core/modules/settings_tray/settings_tray.module \settings_tray_toolbar_alter()

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/settings_tray.module, line 141
Allows configuring blocks and other configuration from the site front-end.

Code

function settings_tray_toolbar_alter(&$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';
      }
    }
  }
}