You are here

admin_toolbar_search.module in Admin Toolbar 3.x

Same filename and directory in other branches
  1. 8.2 admin_toolbar_search/admin_toolbar_search.module

Functionality for search of Admin Toolbar.

File

admin_toolbar_search/admin_toolbar_search.module
View source
<?php

/**
 * @file
 * Functionality for search of Admin Toolbar.
 */
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;

/**
 * Implements hook_help().
 */
function admin_toolbar_search_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help.
    case 'help.page.admin_toolbar_search':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Admin Toolbar Search module add a search option to the toolbar for site administrative tasks.') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_toolbar_alter().
 */
function admin_toolbar_search_toolbar_alter(&$items) {
  if (!\Drupal::currentUser()
    ->hasPermission('use admin toolbar search')) {
    return;
  }
  $admin_toolbar_tools_enabled = \Drupal::service('module_handler')
    ->moduleExists('admin_toolbar_tools');
  $config = \Drupal::config('admin_toolbar_search.settings');
  $display_menu_item = $config
    ->get('display_menu_item');
  if (!$display_menu_item) {
    $items['administration_search'] = [
      "#type" => "toolbar_item",
      '#weight' => 100,
      'tab' => [
        'search' => [
          '#title' => t('Search'),
          '#title_display' => 'invisible',
          '#type' => 'search',
          '#size' => 30,
          '#attributes' => [
            'placeholder' => new TranslatableMarkup('Admin Toolbar quick search'),
          ],
          '#id' => 'admin-toolbar-search-input',
        ],
      ],
      '#attached' => [
        'library' => [
          'admin_toolbar_search/search',
        ],
        'drupalSettings' => [
          'adminToolbarSearch' => [
            'loadExtraLinks' => $admin_toolbar_tools_enabled,
          ],
        ],
      ],
      '#wrapper_attributes' => [
        'id' => 'admin-toolbar-search-tab',
      ],
      '#cache' => [
        'contexts' => [
          'user.permissions',
        ],
        'tags' => [
          'config:admin_toolbar_search.settings',
        ],
      ],
    ];
  }
  else {
    $items['administration_search'] = [
      "#type" => "toolbar_item",
      'tab' => [
        '#type' => 'link',
        '#title' => new TranslatableMarkup('Search'),
        '#url' => URL::fromRoute('system.admin'),
        '#attributes' => [
          'class' => [
            'toolbar-icon',
          ],
        ],
      ],
      'tray' => [
        'search' => [
          '#title' => t('Search'),
          '#type' => 'search',
          '#size' => 60,
          '#id' => 'admin-toolbar-search-input',
        ],
      ],
      '#attached' => [
        'library' => [
          'admin_toolbar_search/search',
        ],
        'drupalSettings' => [
          'adminToolbarSearch' => [
            'loadExtraLinks' => $admin_toolbar_tools_enabled,
          ],
        ],
      ],
      '#wrapper_attributes' => [
        "id" => "admin-toolbar-search-tab",
      ],
      '#cache' => [
        'contexts' => [
          'user.permissions',
        ],
        'tags' => [
          'config:admin_toolbar_search.settings',
        ],
      ],
    ];
  }
}