You are here

public function Toolbar::toolbarTabs in Simplifying 8

Add changes for toolbar.

File

src/Services/Toolbar.php, line 189

Class

Toolbar
Class Toolbar.

Namespace

Drupal\simplifying\Services

Code

public function toolbarTabs(&$items) {
  $design = $this->settingsactions
    ->getSettings('design');
  $route_name = $this->currentroutematch
    ->getRouteName();
  if ($this->modulehandler
    ->moduleExists('webform') || $this->modulehandler
    ->moduleExists('comment')) {
    $class = [
      'toolbar-icon',
      'toolbar-icon-simplifying_unread',
    ];
    $tab_name = $this
      ->t('Submissions');
    $items['simplifying_unread'] = [
      '#type' => 'toolbar_item',
      '#cache' => [
        'max-age' => 0,
      ],
      '#wrapper_attributes' => [
        'class' => [
          'toolbar_simplifying_unread',
        ],
      ],
      'tray' => [
        '#heading' => $tab_name,
        'unread_menu' => [
          '#theme' => 'item_list',
          '#items' => [],
          '#attributes' => [
            'class' => [
              'toolbar-menu',
              'simplifying-unread-menu',
            ],
          ],
        ],
      ],
      '#weight' => 1000,
      'tab' => [
        '#type' => 'html_tag',
        '#tag' => 'button',
        '#value' => $tab_name,
        '#attributes' => [
          'class' => $class,
        ],
      ],
    ];
    $unread_count = 0;
    if ($this->modulehandler
      ->moduleExists('webform')) {
      $webforms = \Drupal::entityTypeManager()
        ->getStorage('webform')
        ->loadMultiple();
      foreach ($webforms as $webform) {
        $query = \Drupal::database()
          ->select('simplifying_entity_unread', 'u');
        $query
          ->condition('u.entity_type', 'webform_submission');
        $query
          ->condition('u.bundle', $webform
          ->id());
        $count = $query
          ->countQuery()
          ->execute()
          ->fetchField();
        $unread_count += $count;
        $class = [];
        if ($route_name == 'entity.webform.results_submissions') {
          $route_webform = $this->currentroutematch
            ->getParameter('webform');
          if (isset($route_webform) && $route_webform
            ->id() == $webform
            ->id()) {
            $class[] = 'is-active';
          }
        }
        $items['simplifying_unread']['tray']['unread_menu']['#items'][$webform
          ->id()] = [
          '#type' => 'link',
          '#title' => $webform
            ->get('title') . (!empty($count) ? ' (' . $count . ')' : ''),
          '#url' => Url::fromRoute('entity.webform.results_submissions', [
            'webform' => $webform
              ->id(),
          ]),
          '#attributes' => [
            'class' => $class,
          ],
          '#wrapper_attributes' => [
            'class' => [
              'menu-item',
              'menu-item--' . $webform
                ->id(),
            ],
          ],
        ];
      }
      ksort($items['simplifying_unread']['tray']['unread_menu']['#items']);
    }
    if ($this->modulehandler
      ->moduleExists('comment')) {
      $query = \Drupal::database()
        ->select('simplifying_entity_unread', 'u');
      $query
        ->condition('u.entity_type', 'comment');
      $count = $query
        ->countQuery()
        ->execute()
        ->fetchField();
      $unread_count += $count;
      $class = [];
      if ($route_name == 'comment.admin') {
        $class[] = 'is-active';
      }
      $items['simplifying_unread']['tray']['unread_menu']['#items']['comment'] = [
        '#type' => 'link',
        '#title' => $this
          ->t('Comments') . (!empty($count) ? ' (' . $count . ')' : ''),
        '#url' => Url::fromRoute('comment.admin'),
        '#attributes' => [
          'class' => $class,
        ],
        '#wrapper_attributes' => [
          'class' => [
            'menu-item',
            'menu-item--comment',
          ],
        ],
      ];
    }
    if (!empty($unread_count)) {
      $items['simplifying_unread']['tab']['#value'] .= ' (' . $unread_count . ')';
    }
  }
  $class = [
    'toolbar-icon',
    'toolbar-icon-simplifying_switch',
  ];
  $tab_name = $this
    ->t('Glyanec');
  $items['simplifying'] = [
    '#type' => 'toolbar_item',
    '#cache' => [
      'max-age' => 0,
    ],
    '#wrapper_attributes' => [
      'class' => [
        'toolbar_simplifying',
        'toolbar_simplifying_switch',
      ],
    ],
    'tray' => [
      '#heading' => $tab_name,
      'simplifying_menu' => [
        '#theme' => 'item_list',
        '#items' => [],
        '#attributes' => [
          'class' => [
            'toolbar-menu',
            'simplifying-menu',
          ],
        ],
      ],
    ],
    '#attached' => [
      'library' => [
        'simplifying/simplifying',
      ],
    ],
    '#weight' => 1001,
    'tab' => [
      '#type' => 'html_tag',
      '#tag' => 'button',
      '#value' => $tab_name,
      '#attributes' => [
        'class' => $class,
      ],
    ],
  ];
  if (!empty($design['small_button'])) {
    $items['simplifying']['tab']['#attributes']['class'][] = 'small_button';
    $items['simplifying']['tab']['#attributes']['title'] = $tab_name;
  }
  $items['simplifying']['#attached']['drupalSettings']['simplifying_menu_links'] = $this->settingsactions
    ->getSettings('menu_links');
  $toolbar_tabs = $this
    ->getToolbarTabs();

  // Alter to hide the toolbar tabs.
  $this->modulehandler
    ->alter('simplifying_hide_toolbar_tabs', $toolbar_tabs);
  $items['simplifying']['#attached']['drupalSettings']['simplifying_menu_tabs'] = $toolbar_tabs;
  $styles = [];
  if (!empty($design['top_background'])) {
    $styles[] = '#toolbar-bar{ background-color: ' . $design['top_background'] . '; }';
  }
  if (!empty($design['top_color'])) {
    $styles[] = '.toolbar .toolbar-bar .toolbar-item{ color: ' . $design['top_color'] . '; }';
  }
  if (!empty($design['submenu_background'])) {
    $styles[] = '#toolbar-item-administration-tray{ background-color: ' . $design['submenu_background'] . '; }';
  }
  if (!empty($styles)) {
    $items['simplifying']['tab']['#suffix'] = Markup::create('<style>' . implode($styles) . '</style>');
  }
  $class = [
    'toolbar-icon',
    'toolbar-icon-simplifying_minify',
  ];
  $text = $this
    ->t('Full administration');
  if (!empty($_COOKIE['simplifying'])) {
    $class[] = 'is-active';

    // $text = $this->t('Small administration');
    $text .= ' (' . $this
      ->t('on') . ')';
  }
  else {
    $text .= ' (' . $this
      ->t('off') . ')';
  }
  $items['simplifying']['tray']['simplifying_menu']['#items']['minify'] = [
    '#type' => 'link',
    '#title' => $text,
    '#url' => Url::fromUserInput('#'),
    '#attributes' => [
      'class' => $class,
      'onclick' => 'SimplifyingSwitch(event, this)',
    ],
    '#wrapper_attributes' => [
      'class' => [
        'menu-item',
        'menu-item--minify',
      ],
    ],
  ];
  $class = [
    'toolbar-icon',
    'toolbar-icon-simplifying_services',
  ];
  if ($route_name == 'simplifying.services') {
    $class[] = 'is-active';
  }
  $items['simplifying']['tray']['simplifying_menu']['#items']['services'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('Order additional services'),
    '#url' => Url::fromRoute('simplifying.services'),
    '#attributes' => [
      'class' => $class,
    ],
    '#wrapper_attributes' => [
      'class' => [
        'menu-item',
        'menu-item--services',
      ],
    ],
  ];
  $class = [
    'toolbar-icon',
    'toolbar-icon-simplifying_training',
  ];
  if ($route_name == 'simplifying.training') {
    $class[] = 'is-active';
  }
  $items['simplifying']['tray']['simplifying_menu']['#items']['training'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('Training'),
    '#url' => Url::fromRoute('simplifying.training'),
    '#attributes' => [
      'class' => $class,
    ],
    '#wrapper_attributes' => [
      'class' => [
        'menu-item',
        'menu-item--training',
      ],
    ],
  ];
}