Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
D
drupal
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Custom Issue Tracker
Custom Issue Tracker
Labels
Merge Requests
225
Merge Requests
225
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
project
drupal
Commits
cd0cb0d7
Commit
cd0cb0d7
authored
Dec 09, 2014
by
catch
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#2369225
by mpdonadio: Add $options['base_url'] to UrlGenerator::generateFromRoute()
parent
ee287003
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
114 additions
and
3 deletions
+114
-3
core/lib/Drupal/Core/Routing/UrlGenerator.php
core/lib/Drupal/Core/Routing/UrlGenerator.php
+19
-1
core/lib/Drupal/Core/Routing/UrlGeneratorInterface.php
core/lib/Drupal/Core/Routing/UrlGeneratorInterface.php
+4
-2
core/modules/language/src/Tests/LanguageSwitchingTest.php
core/modules/language/src/Tests/LanguageSwitchingTest.php
+65
-0
core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php
core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php
+26
-0
No files found.
core/lib/Drupal/Core/Routing/UrlGenerator.php
View file @
cd0cb0d7
...
...
@@ -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
;
}
...
...
core/lib/Drupal/Core/Routing/UrlGeneratorInterface.php
View file @
cd0cb0d7
...
...
@@ -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.
*
...
...
core/modules/language/src/Tests/LanguageSwitchingTest.php
View file @
cd0cb0d7
...
...
@@ -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.
*/
...
...
core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php
View file @
cd0cb0d7
...
...
@@ -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.
*/
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment