You are here

function moderated_content_bulk_publish_toolbar in moderated content bulk publish 1.0.x

Same name and namespace in other branches
  1. 8 moderated_content_bulk_publish.module \moderated_content_bulk_publish_toolbar()
  2. 2.0.x moderated_content_bulk_publish.module \moderated_content_bulk_publish_toolbar()

Implements hook_toolbar() (Display a language switcher for available languages on admin toolbar if site has more than one language).

File

./moderated_content_bulk_publish.module, line 61
moderated_content_bulk_publish module.

Code

function moderated_content_bulk_publish_toolbar() {

  // Get languages
  $current_language = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId();
  $languages = \Drupal::languageManager()
    ->getLanguages();

  // Check if Language module is enabled and there is more than one language
  $moduleHandler = \Drupal::service('module_handler');
  if (count($languages) > 1 && $moduleHandler
    ->moduleExists('language')) {

    // Get current route.
    $route = \Drupal::service('path.matcher')
      ->isFrontPage() ? '<front>' : '<current>';

    // Get links.
    $links = [];
    foreach ($languages as $language) {
      $url = new Url($route, [], [
        'language' => $language,
      ]);
      $links[] = [
        '#markup' => Link::fromTextAndUrl($language
          ->getName(), $url)
          ->toString(),
      ];
    }

    // Set cache.
    $items['admin_toolbar_langswitch'] = [
      '#cache' => [
        'contexts' => [
          'languages:language_interface',
          'url',
        ],
      ],
    ];

    // Build toolbar item and tray.
    $items['admin_toolbar_langswitch'] += [
      '#type' => 'toolbar_item',
      '#weight' => 999,
      'tab' => [
        '#type' => 'link',
        '#url' => Url::fromRoute('entity.configurable_language.collection'),
        '#title' => t('Language') . ': ' . strtoupper($current_language),
        '#attributes' => [
          'class' => [
            'toolbar-item-admin-toolbar-langswitch',
          ],
          'title' => t('Admin Toolbar Langswitch'),
        ],
      ],
      'tray' => [
        '#heading' => t('Admin Toolbar Langswitch'),
        'content' => [
          '#theme' => 'item_list',
          '#items' => $links,
          '#attributes' => [
            'class' => [
              'toolbar-menu',
            ],
          ],
        ],
      ],
    ];
    return $items;
  }
}