function LanguageBlock::build

Same name and namespace in other branches
  1. 8.9.x core/modules/language/src/Plugin/Block/LanguageBlock.php \Drupal\language\Plugin\Block\LanguageBlock::build()
  2. 10 core/modules/language/src/Plugin/Block/LanguageBlock.php \Drupal\language\Plugin\Block\LanguageBlock::build()
  3. 11.x core/modules/language/src/Plugin/Block/LanguageBlock.php \Drupal\language\Plugin\Block\LanguageBlock::build()

Overrides BlockPluginInterface::build

File

core/modules/language/src/Plugin/Block/LanguageBlock.php, line 84

Class

LanguageBlock
Provides a 'Language switcher' block.

Namespace

Drupal\language\Plugin\Block

Code

public function build() {
    $build = [];
    $type = $this->getDerivativeId();
    $route_match = \Drupal::routeMatch();
    // If there is no route match, for example when creating blocks on 404 pages
    // for logged-in users with big_pipe enabled using the front page instead.
    $url = $route_match->getRouteObject() ? Url::fromRouteMatch($route_match) : Url::fromRoute('<front>');
    $links = $this->languageManager
        ->getLanguageSwitchLinks($type, $url);
    if (isset($links->links)) {
        $build = [
            '#theme' => 'links__language_block',
            '#links' => $links->links,
            '#attributes' => [
                'class' => [
                    "language-switcher-{$links->method_id}",
                ],
            ],
            '#set_active_class' => TRUE,
        ];
    }
    return $build;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.