You are here

class RenderBuilder in Admin Toolbar Language Switcher 8

Same name and namespace in other branches
  1. 8.1.x src/RenderBuilder.php \Drupal\toolbar_language_switcher\RenderBuilder
  2. 2.0.x src/RenderBuilder.php \Drupal\toolbar_language_switcher\RenderBuilder

Class RenderBuilder.

Hierarchy

Expanded class hierarchy of RenderBuilder

1 string reference to 'RenderBuilder'
toolbar_language_switcher.services.yml in ./toolbar_language_switcher.services.yml
toolbar_language_switcher.services.yml
1 service uses RenderBuilder
tls.render.builder in ./toolbar_language_switcher.services.yml
Drupal\toolbar_language_switcher\RenderBuilder

File

src/RenderBuilder.php, line 14

Namespace

Drupal\toolbar_language_switcher
View source
class RenderBuilder {
  use StringTranslationTrait;

  /**
   * Language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * Path matcher service.
   *
   * @var \Drupal\Core\Path\PathMatcherInterface
   */
  protected $pathMatcher;

  /**
   * Id of the current language.
   *
   * @var \Drupal\Core\Language\LanguageInterface
   */
  protected $currentLanguage;

  /**
   * List of the available languages.
   *
   * @var \Drupal\Core\Language\LanguageInterface[]
   */
  protected $languages;

  /**
   * Current route name.
   *
   * @var string
   */
  protected $route;

  /**
   * RenderBuilder constructor.
   *
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   Language manager.
   * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
   *   Path builder.
   */
  public function __construct(LanguageManagerInterface $language_manager, PathMatcherInterface $path_matcher) {
    $this->languageManager = $language_manager;
    $this->pathMatcher = $path_matcher;

    // Get languages, get current route.
    $this->currentLanguage = $this->languageManager
      ->getCurrentLanguage();
    $this->languages = $this->languageManager
      ->getLanguages();
    $this->route = $this->pathMatcher
      ->isFrontPage() ? '<front>' : '<current>';
  }

  /**
   * Main build method.
   *
   * @return array
   *   Render array for the toolbar items.
   */
  public function build() {
    $languages_count = count($this->languages);
    if ($languages_count < 2) {
      return [];
    }

    // 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' => 'html_tag',
        '#tag' => 'a',
        '#attributes' => [
          'class' => [
            'toolbar-icon',
            'toolbar-icon-language',
            // @ToDo: Remove following class before the beta release.
            'toolbar-item-admin-toolbar-langswitch',
          ],
        ],
        '#attached' => [
          'library' => [
            'toolbar_language_switcher/toolbar',
          ],
        ],
      ],
    ];
    $current_language_name = $this->currentLanguage
      ->getName();
    $current_language_id = $this->currentLanguage
      ->getId();
    if ($languages_count > 2) {

      // Get links.
      $links = [];
      foreach ($this->languages as $language) {
        $link = [];
        $link_title = $language
          ->getName();
        $url_options = [
          'language' => $language,
        ];
        if ($language
          ->getId() === $current_language_id) {
          $link['attributes']['class'][] = 'is-active';
          $link['attributes']['title'] = $this
            ->t('Current active @current language', [
            '@current' => $link_title,
          ]);
          $url_options['fragment'] = '!';
        }
        else {
          $link['attributes']['title'] = $this
            ->t('Change @current language to @another', [
            '@current' => $current_language_name,
            '@another' => $link_title,
          ]);
        }
        $link['title'] = $link_title;
        $link['url'] = new Url($this->route, [], $url_options);
        $links[] = $link;
      }

      // Build toolbar item and tray.
      $items['admin_toolbar_langswitch'] = NestedArray::mergeDeep($items['admin_toolbar_langswitch'], [
        'tab' => [
          '#value' => $current_language_name,
          '#attributes' => [
            'href' => '#',
            'title' => $this
              ->t('Selected language: @lang', [
              '@lang' => $current_language_name,
            ]),
          ],
        ],
        'tray' => [
          '#heading' => $this
            ->t('Admin Toolbar Language Switcher'),
          'content' => [
            '#theme' => 'links',
            '#links' => $links,
            '#attributes' => [
              'class' => [
                'toolbar-menu',
              ],
            ],
          ],
        ],
      ]);
    }
    else {
      unset($this->languages[$current_language_id]);
      $another_language = reset($this->languages);
      $another_language_name = $another_language
        ->getName();

      // Build toolbar item.
      $items['admin_toolbar_langswitch'] = NestedArray::mergeDeep($items['admin_toolbar_langswitch'], [
        'tab' => [
          '#value' => $this
            ->t('Switch to @another', [
            '@another' => $another_language_name,
          ]),
          '#attributes' => [
            'href' => (new Url($this->route, [], [
              'language' => $another_language,
            ]))
              ->toString(),
            'title' => $this
              ->t('Switch @current language to @another', [
              '@current' => $current_language_name,
              '@another' => $another_language_name,
            ]),
          ],
        ],
      ]);
    }
    return $items;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RenderBuilder::$currentLanguage protected property Id of the current language.
RenderBuilder::$languageManager protected property Language manager.
RenderBuilder::$languages protected property List of the available languages.
RenderBuilder::$pathMatcher protected property Path matcher service.
RenderBuilder::$route protected property Current route name.
RenderBuilder::build public function Main build method.
RenderBuilder::__construct public function RenderBuilder constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.