Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #3821: Make the # link stays at the current page #3933

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,20 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
// Link.
$link_render_array = [];
$link_url = '';
$current_path = \Drupal::service('path.current')->getPath();
if ($item->link_title || $item->link_uri) {
if (str_starts_with($item->link_uri ?? '', '/' . PublicStream::basePath())) {
// Link to public file: use fromUri() to get the URL.
$link_url = Url::fromUri(urldecode('base:' . $item->link_uri));
}
else {
$link_url = $this->pathValidator->getUrlIfValid($item->link_uri ?? '<none>');
// Check if the link is an anchor within the current page.
if (str_starts_with($item->link_uri, "#")) {
$link_url = $this->pathValidator->getUrlIfValid($current_path . $item->link_uri);
}
else {
$link_url = $this->pathValidator->getUrlIfValid($item->link_uri ?? '<none>');
}
}
$link_render_array = [
'#type' => 'link',
Expand Down
Loading