Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drupal
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
drupal
Merge requests
!8411
Issue
#3450518
: Add a generic trait for logic to convert references into Urls in LinkWidget
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Issue
#3450518
: Add a generic trait for logic to convert references into Urls in LinkWidget
issue/drupal-3450518:3450518-linkwidgettrait
into
11.x
Overview
0
Commits
1
Pipelines
1
Changes
2
Open
Pablo López
requested to merge
issue/drupal-3450518:3450518-linkwidgettrait
into
11.x
11 months ago
Overview
0
Commits
1
Pipelines
1
Changes
2
Expand
Closes
#3450518
0
0
Merge request reports
Compare
11.x
11.x (base)
and
latest version
latest version
d257f234
1 commit,
11 months ago
2 files
+
146
−
121
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
Search (e.g. *.vue) (Ctrl+P)
core/lib/Drupal/Core/Render/Element/UriValidationTrait.php
0 → 100644
+
141
−
0
Options
<?php
namespace
Drupal\Core\Render\Element
;
use
Drupal\Core\Entity\Element\EntityAutocomplete
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\Core\StringTranslation\TranslatableMarkup
;
/**
* Provides a trait for uri validation and management.
*
* This trait is useful to validate URIs entered form elements and transform
* them back to human readable strings when used as from element default value.
*/
trait
UriValidationTrait
{
/**
* Form element validation handler for the 'uri' element.
*
* Disallows saving inaccessible or untrusted URLs.
*/
public
static
function
validateUriElement
(
$element
,
FormStateInterface
$form_state
,
$form
)
{
$uri
=
static
::
getUserEnteredStringAsUri
(
$element
[
'#value'
]);
$form_state
->
setValueForElement
(
$element
,
$uri
);
// If getUserEnteredStringAsUri() mapped the entered value to an 'internal:'
// URI , ensure the raw value begins with '/', '?' or '#'.
// @todo '<front>' is valid input for BC reasons, may be removed by
// https://www.drupal.org/node/2421941
if
(
parse_url
(
$uri
,
PHP_URL_SCHEME
)
===
'internal'
&&
!
in_array
(
$element
[
'#value'
][
0
],
[
'/'
,
'?'
,
'#'
,
],
TRUE
)
&&
!
str_starts_with
(
$element
[
'#value'
],
'<front>'
))
{
$form_state
->
setError
(
$element
,
new
TranslatableMarkup
(
'Manually entered paths should start with one of the following characters: / ? #'
));
return
;
}
}
/**
* Gets the user-entered string as a URI.
*
* The following two forms of input are mapped to URIs:
* - entity autocomplete ("label (entity id)") strings: to 'entity:' URIs;
* - strings without a detectable scheme: to 'internal:' URIs.
*
* This method is the inverse of ::getUriAsDisplayableString().
*
* @param string $string
* The user-entered string.
*
* @return string
* The URI, if a non-empty $uri was passed.
*
* @see static::getUriAsDisplayableString()
*/
protected
static
function
getUserEnteredStringAsUri
(
$string
)
{
// By default, assume the entered string is a URI.
$uri
=
trim
(
$string
);
// Detect entity autocomplete string, map to 'entity:' URI.
$entity_id
=
EntityAutocomplete
::
extractEntityIdFromAutocompleteInput
(
$string
);
if
(
$entity_id
!==
NULL
)
{
// @todo Support entity types other than 'node'. Will be fixed in
// https://www.drupal.org/node/2423093.
$uri
=
'entity:node/'
.
$entity_id
;
}
// Support linking to nothing.
elseif
(
in_array
(
$string
,
[
'<nolink>'
,
'<none>'
,
'<button>'
],
TRUE
))
{
$uri
=
'route:'
.
$string
;
}
// Detect a schemeless string, map to 'internal:' URI.
elseif
(
!
empty
(
$string
)
&&
parse_url
(
$string
,
PHP_URL_SCHEME
)
===
NULL
)
{
// @todo '<front>' is valid input for BC reasons, may be removed by
// https://www.drupal.org/node/2421941
// - '<front>' -> '/'
// - '<front>#foo' -> '/#foo'
if
(
str_starts_with
(
$string
,
'<front>'
))
{
$string
=
'/'
.
substr
(
$string
,
strlen
(
'<front>'
));
}
$uri
=
'internal:'
.
$string
;
}
return
$uri
;
}
/**
* Gets the URI without the 'internal:' or 'entity:' scheme.
*
* The following two forms of URIs are transformed:
* - 'entity:' URIs: to entity autocomplete ("label (entity id)") strings;
* - 'internal:' URIs: the scheme is stripped.
*
* This method is the inverse of ::getUserEnteredStringAsUri().
*
* @param string $uri
* The URI to get the displayable string for.
*
* @return string
*
* @see static::getUserEnteredStringAsUri()
*/
protected
static
function
getUriAsDisplayableString
(
$uri
)
{
$scheme
=
parse_url
(
$uri
,
PHP_URL_SCHEME
);
// By default, the displayable string is the URI.
$displayable_string
=
$uri
;
// A different displayable string may be chosen in case of the 'internal:'
// or 'entity:' built-in schemes.
if
(
$scheme
===
'internal'
)
{
$uri_reference
=
explode
(
':'
,
$uri
,
2
)[
1
];
// @todo '<front>' is valid input for BC reasons, may be removed by
// https://www.drupal.org/node/2421941
$path
=
parse_url
(
$uri
,
PHP_URL_PATH
);
if
(
$path
===
'/'
)
{
$uri_reference
=
'<front>'
.
substr
(
$uri_reference
,
1
);
}
$displayable_string
=
$uri_reference
;
}
elseif
(
$scheme
===
'entity'
)
{
[
$entity_type
,
$entity_id
]
=
explode
(
'/'
,
substr
(
$uri
,
7
),
2
);
// Show the 'entity:' URI as the entity autocomplete would.
// @todo Support entity types other than 'node'. Will be fixed in
// https://www.drupal.org/node/2423093.
if
(
$entity_type
==
'node'
&&
$entity
=
\Drupal
::
entityTypeManager
()
->
getStorage
(
$entity_type
)
->
load
(
$entity_id
))
{
$displayable_string
=
EntityAutocomplete
::
getEntityLabels
([
$entity
]);
}
}
elseif
(
$scheme
===
'route'
)
{
$displayable_string
=
ltrim
(
$displayable_string
,
'route:'
);
}
return
$displayable_string
;
}
}
Loading