setCurrentRequestForRoute example

// In order to reflect the changes for a multilingual site in the container     // we have to rebuild it.     $this->rebuildContainer();

    // The method language-content-entity should run before language-url and     // append query parameter for the content language and prevent language-url     // from overwriting the URL.     $default_site_langcode = $this->config('system.site')->get('default_langcode');

    // Now switching to an entity route, so that the URL links are generated     // while being on an entity route.     $this->setCurrentRequestForRoute('/entity_test/{entity_test}', 'entity.entity_test.canonical');

    $translation = $this->entity;
    $this->drupalGet($translation->toUrl());
    $last = $this->container->get('state')->get('language_test.language_negotiation_last');
    $last_content_language = $last[LanguageInterface::TYPE_CONTENT];
    $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
    // Check that interface language and content language are the same as the     // default translation language of the entity.     $this->assertSame($default_site_langcode$last_interface_language);
    $this->assertSame($last_content_language$last_interface_language);
    $this->assertSame($translation->language()->getId()$last_content_language);

    
LanguageNegotiationContentEntity::METHOD_ID => 0,
      LanguageNegotiationUrl::METHOD_ID => 1,
    ]);
    $config->save();

    // Without being on a content entity route the default entity URL tests     // should still pass.     $this->testEntityUrlLanguage();

    // Now switching to an entity route, so that the URL links are generated     // while being on an entity route.     $this->setCurrentRequestForRoute('/entity_test/{entity_test}', 'entity.entity_test.canonical');

    // The method language-content-entity should run before language-url and     // append query parameter for the content language and prevent language-url     // from overwriting the URL.     $this->assertStringContainsString('/en/entity_test/' . $this->entity->id() . '?' . LanguageNegotiationContentEntity::QUERY_PARAMETER . '=en', $this->entity->toUrl('canonical')->toString());
    $this->assertStringContainsString('/en/entity_test/' . $this->entity->id() . '?' . LanguageNegotiationContentEntity::QUERY_PARAMETER . '=es', $this->entity->getTranslation('es')->toUrl('canonical')->toString());
    $this->assertStringContainsString('/en/entity_test/' . $this->entity->id() . '?' . LanguageNegotiationContentEntity::QUERY_PARAMETER . '=fr', $this->entity->getTranslation('fr')->toUrl('canonical')->toString());

    // Define the method language-url with a higher priority than     // language-content-entity. This configuration should match the default one,     // where the language-content-entity is turned off.
Home | Imprint | This part of the site doesn't use cookies.