getLanguageSwitchLinks example

$this->installEntitySchema('user');

    $this->languageNegotiator = $this->container->get('language_negotiator');
    $this->languageManager = $this->container->get('language_manager');
  }

  /** * @covers ::getLanguageSwitchLinks */
  public function testLanguageSwitchLinks() {
    $this->languageNegotiator->setCurrentUser($this->prophesize('Drupal\Core\Session\AccountInterface')->reveal());
    $this->languageManager->getLanguageSwitchLinks(LanguageInterface::TYPE_INTERFACE, new Url('<current>'));
  }

}

      'en' => [
        'url' => $url,
        'title' => $this->languages['en']->getName(),
        'attributes' => ['class' => ['language-link']],
        'query' => [
          LanguageNegotiationContentEntity::QUERY_PARAMETER => 'en',
          'param1' => 'xyz',
        ],
      ],
    ];
    $providedLanguageSwitchLinksArray = $languageNegotiationContentEntity->getLanguageSwitchLinks($request$this->randomMachineName()$url);
    $this->assertEquals(
      $expectedLanguageSwitchLinksArray,
      $providedLanguageSwitchLinksArray
    );
  }

}
/** * {@inheritdoc} */
  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,
      ];
/** * {@inheritdoc} */
  public function getLanguageSwitchLinks($type, Url $url) {
    if ($this->negotiator) {
      foreach ($this->negotiator->getNegotiationMethods($type) as $method_id => $method) {
        $reflector = new \ReflectionClass($method['class']);

        if ($reflector->implementsInterface('\Drupal\language\LanguageSwitcherInterface')) {
          $original_languages = $this->negotiatedLanguages;
          $result = $this->negotiator->getNegotiationMethodInstance($method_id)->getLanguageSwitchLinks($this->requestStack->getCurrentRequest()$type$url);

          if (!empty($result)) {
            // Allow modules to provide translations for specific links.             $this->moduleHandler->alter('language_switch_links', $result$type$url);

            $result = array_filter($resultfunction Darray $link): bool {
              $url = $link['url'] ?? NULL;
              $language = $link['language'] ?? NULL;
              if ($language instanceof LanguageInterface) {
                $this->negotiatedLanguages[LanguageInterface::TYPE_CONTENT] = $language;
                $this->negotiatedLanguages[LanguageInterface::TYPE_INTERFACE] = $language;
              }
Home | Imprint | This part of the site doesn't use cookies.