Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
project
drupal
Commits
a39876b9
Commit
a39876b9
authored
May 04, 2016
by
catch
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#2693725
by chx, alexpott: Add <nolink> to allow for non-link links
parent
ba8e8b2a
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
55 additions
and
2 deletions
+55
-2
core/lib/Drupal/Core/GeneratedLink.php
core/lib/Drupal/Core/GeneratedLink.php
+5
-0
core/lib/Drupal/Core/GeneratedNoLink.php
core/lib/Drupal/Core/GeneratedNoLink.php
+15
-0
core/lib/Drupal/Core/Utility/LinkGenerator.php
core/lib/Drupal/Core/Utility/LinkGenerator.php
+7
-2
core/modules/system/system.routing.yml
core/modules/system/system.routing.yml
+7
-0
core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php
core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php
+21
-0
No files found.
core/lib/Drupal/Core/GeneratedLink.php
View file @
a39876b9
...
...
@@ -14,6 +14,11 @@
*/
class
GeneratedLink
extends
BubbleableMetadata
implements
MarkupInterface
,
\
Countable
{
/**
* HTML tag to use when building the link.
*/
const
TAG
=
'a'
;
/**
* The HTML string value containing a link.
*
...
...
core/lib/Drupal/Core/GeneratedNoLink.php
0 → 100644
View file @
a39876b9
<?php
namespace
Drupal\Core
;
/**
* This class holds a <span> generated from the <nolink> route.
*/
class
GeneratedNoLink
extends
GeneratedLink
{
/**
* {@inheritdoc}
*/
const
TAG
=
'span'
;
}
core/lib/Drupal/Core/Utility/LinkGenerator.php
View file @
a39876b9
...
...
@@ -7,6 +7,7 @@
use
Drupal\Component\Render\MarkupInterface
;
use
Drupal\Core\Extension\ModuleHandlerInterface
;
use
Drupal\Core\GeneratedLink
;
use
Drupal\Core\GeneratedNoLink
;
use
Drupal\Core\Link
;
use
Drupal\Core\Render\RendererInterface
;
use
Drupal\Core\Routing\UrlGeneratorInterface
;
...
...
@@ -142,7 +143,7 @@ public function generate($text, Url $url) {
$this
->
moduleHandler
->
alter
(
'link'
,
$variables
);
// Move attributes out of options since generateFromRoute() doesn't need
// them.
Include a placeholder for the href
.
// them.
Make sure the "href" comes first for testing purposes
.
$attributes
=
array
(
'href'
=>
''
)
+
$variables
[
'options'
][
'attributes'
];
unset
(
$variables
[
'options'
][
'attributes'
]);
$url
->
setOptions
(
$variables
[
'options'
]);
...
...
@@ -152,6 +153,10 @@ public function generate($text, Url $url) {
$generated_link
=
new
GeneratedLink
();
$attributes
[
'href'
]
=
$url
->
toString
(
FALSE
);
}
elseif
(
$url
->
isRouted
()
&&
$url
->
getRouteName
()
===
'<nolink>'
)
{
$generated_link
=
new
GeneratedNoLink
();
unset
(
$attributes
[
'href'
]);
}
else
{
$generated_url
=
$url
->
toString
(
TRUE
);
$generated_link
=
GeneratedLink
::
createFromObject
(
$generated_url
);
...
...
@@ -166,7 +171,7 @@ public function generate($text, Url $url) {
$attributes
=
new
Attribute
(
$attributes
);
// This is safe because Attribute does escaping and $variables['text'] is
// either rendered or escaped.
return
$generated_link
->
setGeneratedLink
(
'<
a
'
.
$attributes
.
'>'
.
$variables
[
'text'
]
.
'</
a
>'
);
return
$generated_link
->
setGeneratedLink
(
'<'
.
$generated_link
::
TAG
.
$attributes
.
'>'
.
$variables
[
'text'
]
.
'</
'
.
$generated_link
::
TAG
.
'
>'
);
}
}
core/modules/system/system.routing.yml
View file @
a39876b9
...
...
@@ -397,6 +397,13 @@ system.theme_settings_theme:
requirements
:
_access
:
'
TRUE'
'
<nolink>'
:
path
:
'
'
options
:
_no_path
:
TRUE
requirements
:
_access
:
'
TRUE'
'
<current>'
:
path
:
'
<current>'
...
...
core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php
View file @
a39876b9
...
...
@@ -3,6 +3,7 @@
namespace
Drupal\Tests\Core\Utility
{
use
Drupal\Component\Render\MarkupInterface
;
use
Drupal\Core\GeneratedNoLink
;
use
Drupal\Core\GeneratedUrl
;
use
Drupal\Core\Language\Language
;
use
Drupal\Core\Link
;
...
...
@@ -146,6 +147,26 @@ public function testGenerate() {
),
$result
);
}
/**
* Tests the generate() method with the <nolink> route.
*
* @covers ::generate
*/
public
function
testGenerateNoLink
()
{
$this
->
urlGenerator
->
expects
(
$this
->
never
())
->
method
(
'generateFromRoute'
);
$this
->
moduleHandler
->
expects
(
$this
->
once
())
->
method
(
'alter'
)
->
with
(
'link'
,
$this
->
isType
(
'array'
));
$url
=
Url
::
fromRoute
(
'<nolink>'
);
$url
->
setUrlGenerator
(
$this
->
urlGenerator
);
$result
=
$this
->
linkGenerator
->
generate
(
'Test'
,
$url
);
$this
->
assertTrue
(
$result
instanceof
GeneratedNoLink
);
$this
->
assertSame
(
'<span>Test</span>'
,
(
string
)
$result
);
}
/**
* Tests the generate() method with an external URL.
*
...
...
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