Commit cd0cb0d7 authored by catch's avatar catch

Issue #2369225 by mpdonadio: Add $options['base_url'] to UrlGenerator::generateFromRoute()

parent ee287003
......@@ -148,7 +148,6 @@ public function generate($name, $parameters = array(), $absolute = FALSE) {
*/
public function generateFromRoute($name, $parameters = array(), $options = array()) {
$options += array('prefix' => '');
$absolute = !empty($options['absolute']);
$route = $this->getRoute($name);
$this->processRoute($name, $route, $parameters);
......@@ -159,6 +158,7 @@ public function generateFromRoute($name, $parameters = array(), $options = array
$path = $this->getInternalPathFromRoute($route, $parameters);
$path = $this->processPath($path, $options);
if (!empty($options['prefix'])) {
$path = ltrim($path, '/');
$prefix = empty($path) ? rtrim($options['prefix'], '/') : $options['prefix'];
......@@ -172,7 +172,25 @@ public function generateFromRoute($name, $parameters = array(), $options = array
}
}
// The base_url might be rewritten from the language rewrite in domain mode.
if (isset($options['base_url'])) {
$base_url = $options['base_url'];
if (isset($options['https'])) {
if ($options['https'] === TRUE) {
$base_url = str_replace('http://', 'https://', $base_url);
}
elseif ($options['https'] === FALSE) {
$base_url = str_replace('https://', 'http://', $base_url);
}
}
return $base_url . $path . $fragment;
}
$base_url = $this->context->getBaseUrl();
$absolute = !empty($options['absolute']);
if (!$absolute || !$host = $this->context->getHost()) {
return $base_url . $path . $fragment;
}
......
......@@ -59,8 +59,8 @@ interface UrlGeneratorInterface extends VersatileGeneratorInterface {
* - 'https': Whether this URL should point to a secure location. If not
* defined, the current scheme is used, so the user stays on HTTP or HTTPS
* respectively. TRUE enforces HTTPS and FALSE enforces HTTP.
* - 'base_url': Only used internally, to modify the base URL when a language
* dependent URL requires so.
* - 'base_url': Only used internally by a path processor, for example, to
* modify the base URL when a language dependent URL requires so.
* - 'prefix': Only used internally, to modify the path when a language
* dependent URL requires so.
* - 'script': Added to the URL between the base path and the path prefix.
......@@ -132,6 +132,8 @@ public function getPathFromRoute($name, $parameters = array());
* - 'https': Whether this URL should point to a secure location. If not
* defined, the current scheme is used, so the user stays on HTTP or HTTPS
* respectively. TRUE enforces HTTPS and FALSE enforces HTTP.
* - 'base_url': Only used internally by a path processor, for example, to
* modify the base URL when a language dependent URL requires so.
* - 'prefix': Only used internally, to modify the path when a language
* dependent URL requires so.
*
......
......@@ -7,6 +7,8 @@
namespace Drupal\language\Tests;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Drupal\Core\Language\LanguageInterface;
use Drupal\simpletest\WebTestBase;
......@@ -158,6 +160,69 @@ protected function doTestLanguageBlockAnonymous($block_label) {
$this->assertIdentical($labels, array('English', 'français'), 'The language links labels are in their own language on the language switcher block.');
}
/**
* Test languge switcher links for domain based negotiation
*/
function testLanguageBlockWithDomain() {
// Add the Italian language.
ConfigurableLanguage::createFromLangcode('it')->save();
// Rebuild the container so that the new language is picked up by services
// that hold a list of languages.
$this->rebuildContainer();
$languages = $this->container->get('language_manager')->getLanguages();
// Enable browser and URL language detection.
$edit = array(
'language_interface[enabled][language-url]' => TRUE,
'language_interface[weight][language-url]' => -10,
);
$this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
// Do not allow blank domain.
$edit = array(
'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN,
'domain[en]' => '',
);
$this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
$this->assertText(t('The domain may not be left blank for English'), 'The form does not allow blank domains.');
// Change the domain for the Italian language.
$edit = array(
'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN,
'domain[en]' => \Drupal::request()->getHost(),
'domain[it]' => 'it.example.com',
);
$this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
$this->assertText(t('The configuration options have been saved'), 'Domain configuration is saved.');
// Enable the language switcher block.
$this->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_INTERFACE, array('id' => 'test_language_block'));
$this->drupalGet('');
/** @var \Drupal\Core\Routing\UrlGenerator $generator */
$generator = $this->container->get('url_generator');
// Verfify the English URL is correct
list($english_link) = $this->xpath('//div[@id=:id]/ul/li/a[@hreflang=:hreflang]', array(
':id' => 'block-test-language-block',
':hreflang' => 'en',
));
$english_url = $generator->generateFromPath('user/2', array('language' => $languages['en']));
$this->assertEqual($english_url, (string) $english_link['href']);
// Verfify the Italian URL is correct
list($italian_link) = $this->xpath('//div[@id=:id]/ul/li/a[@hreflang=:hreflang]', array(
':id' => 'block-test-language-block',
':hreflang' => 'it',
));
$italian_url = $generator->generateFromPath('user/2', array('language' => $languages['it']));
$this->assertEqual($italian_url, (string) $italian_link['href']);
}
/**
* Test active class on links when switching languages.
*/
......
......@@ -290,6 +290,32 @@ public function testAbsoluteURLGeneration() {
$this->assertEquals('http://localhost/hello/world?zoo=5#top', $url);
}
/**
* Confirms that explicitly setting the base_url works with generated routes
*/
public function testBaseURLGeneration() {
$options = array('base_url' => 'http://www.example.com:8888');
$url = $this->generator->generateFromRoute('test_1', array(), $options);
$this->assertEquals('http://www.example.com:8888/hello/world', $url);
$options = array('base_url' => 'http://www.example.com:8888', 'https' => TRUE);
$url = $this->generator->generateFromRoute('test_1', array(), $options);
$this->assertEquals('https://www.example.com:8888/hello/world', $url);
$options = array('base_url' => 'https://www.example.com:8888', 'https' => FALSE);
$url = $this->generator->generateFromRoute('test_1', array(), $options);
$this->assertEquals('http://www.example.com:8888/hello/world', $url);
$this->routeProcessorManager->expects($this->once())
->method('processOutbound')
->with($this->anything());
$options = array('base_url' => 'http://www.example.com:8888', 'fragment' => 'top');
// Extra parameters should appear in the query string.
$url = $this->generator->generateFromRoute('test_1', array('zoo' => '5'), $options);
$this->assertEquals('http://www.example.com:8888/hello/world?zoo=5#top', $url);
}
/**
* Test that the 'scheme' route requirement is respected during url generation.
*/
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment