You are here

admin_menu_toolbar.module in Administration menu 8.3

Renders Administration menu like Toolbar (core) module.

@todo Separate shortcut functionality into own module/widget.

File

admin_menu_toolbar/admin_menu_toolbar.module
View source
<?php

/**
 * @file
 * Renders Administration menu like Toolbar (core) module.
 *
 * @todo Separate shortcut functionality into own module/widget.
 */

/**
 * Implements hook_library_info().
 */
function admin_menu_toolbar_library_info() {
  $path = drupal_get_path('module', 'admin_menu_toolbar');
  $options = [
    'every_page' => TRUE,
  ];

  // @todo D8: every_page is broken for System libraries.
  // @see http://drupal.org/node/1805452
  $options = [];
  $libraries['drupal.admin'] = [
    'title' => 'Administration menu toolbar',
    'version' => '1.0',
    'css' => [
      $path . '/admin_menu_toolbar.css' => $options,
    ],
    'js' => [
      $path . '/admin_menu_toolbar.js' => $options,
    ],
    'dependencies' => [
      [
        'admin_menu',
        'drupal.admin',
      ],
    ],
  ];

  // @todo Stop-gap fix until cached rendering is resolved.
  // @see http://drupal.org/node/1567622
  if (0 && module_exists('shortcut')) {
    $shortcut_path = drupal_get_path('module', 'shortcut');
    $libraries['drupal.admin']['css'][$shortcut_path . '/shortcut.base.css'] = $options;
    $libraries['drupal.admin']['css'][$shortcut_path . '/shortcut.theme.css'] = $options;
  }
  return $libraries;
}

/**
 * Implements hook_form_FORMID_alter().
 */
function admin_menu_toolbar_form_admin_menu_admin_settings_form_alter(&$form) {

  // Add the shortcut links as component on behalf of Shortcut module.
  $form['plugins']['admin_menu_components']['#options']['shortcut.links'] = t('Shortcuts');

  // The shortcut bar consists of two elements, so we target their class names
  // instead of cluttering the markup with additional IDs.
  $form['plugins']['admin_menu_components']['shortcut.links']['#attributes']['rel'] = '.shortcut-toggle, .shortcut-toolbar';
}

/**
 * Implementation of hook_page_build().
 */
function admin_menu_toolbar_page_build(&$page) {
  if (!isset($page['page_bottom']['admin_menu'])) {
    return;
  }
  $attached =& $page['page_bottom']['admin_menu']['#attached'];
  $attached['library'][] = [
    'admin_menu_toolbar',
    'drupal.admin',
  ];
  $settings = [];

  // Add current path to support menu item highlighting.
  // @todo Compile real active trail here?
  $args = arg();
  if ($args[0] == 'admin' && !empty($args[1])) {
    $settings['activeTrail'] = url($args[0] . '/' . $args[1]);
  }
  elseif (drupal_is_front_page()) {
    $settings['activeTrail'] = url('<front>');
  }
  $attached['js'][] = [
    'data' => [
      'admin_menu' => [
        'toolbar' => $settings,
      ],
    ],
    'type' => 'setting',
  ];
}

/**
 * Implements hook_admin_menu_output_build().
 */
function admin_menu_toolbar_admin_menu_output_build(&$content) {
  if (empty($content['#components']['shortcut.links']) && !$content['#complete']) {
    return;
  }

  // Add shortcuts toggle.
  $content['shortcut-toggle'] = [
    '#access' => module_exists('shortcut'),
    '#weight' => -200,
    '#type' => 'link',
    '#title' => t('Show shortcuts'),
    '#href' => '',
    '#options' => [
      'attributes' => [
        'class' => [
          'shortcut-toggle',
        ],
      ],
    ],
  ];

  // Add shortcuts bar.
  $content['shortcut'] = [
    '#access' => module_exists('shortcut'),
    '#weight' => 200,
    '#prefix' => '<div class="shortcut-toolbar">',
    '#suffix' => '</div>',
  ];
  $content['shortcut']['shortcuts'] = [
    // Shortcut module's CSS relies on Toolbar module's markup.
    // @see http://drupal.org/node/1217038
    '#prefix' => '<div id="toolbar">',
    '#suffix' => '</div>',
    // @todo Links may contain .active-trail classes.
    '#pre_render' => [
      'shortcut_toolbar_pre_render',
    ],
  ];
}

/**
 * Implements hook_admin_menu_output_alter().
 */
function admin_menu_toolbar_admin_menu_output_alter(&$content) {

  // Add a class to top-level items for styling.
  if (isset($content['menu'])) {
    foreach (element_children($content['menu']) as $link) {
      $content['menu'][$link]['#attributes']['class'][] = 'admin-menu-toolbar-category';
    }
  }

  // Alter icon.
  if (isset($content['icon'])) {
    unset($content['icon']['icon']['#theme']);
    $content['icon']['icon']['#title'] = '<span>' . t('Home') . '</span>';
    $content['icon']['icon']['#attributes']['class'][] = 'admin-menu-toolbar-category';
  }

  // Alter user account link.
  if (isset($content['account'])) {
    $content['account']['account']['#title'] = t('Hello <strong>@username</strong>', [
      '@username' => $content['account']['account']['#title'],
    ]);
    $content['account']['account']['#options']['html'] = TRUE;
  }
}