From 3b112a44e174b490007cb9b5a0fc5b99e8301f66 Mon Sep 17 00:00:00 2001 From: BaoNguyen09 Date: Mon, 9 Dec 2024 16:20:14 -0700 Subject: [PATCH 1/5] fix: make the # link stays at the current page --- .../Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/modules/custom/az_card/src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php b/modules/custom/az_card/src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php index c759d6e7b2..13ffe27af7 100644 --- a/modules/custom/az_card/src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php +++ b/modules/custom/az_card/src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php @@ -131,7 +131,11 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $link_url = Url::fromUri(urldecode('base:' . $item->link_uri)); } else { - $link_url = $this->pathValidator->getUrlIfValid($item->link_uri ?? ''); + if (strcmp($item->link_uri, "#") == 0) { + $link_url = $this->pathValidator->getUrlIfValid(''); + } else { + $link_url = $this->pathValidator->getUrlIfValid($item->link_uri ?? ''); + } } $link_render_array = [ '#type' => 'link', From 69fb542bc91e4b9cee96fa7cf32d175941b9d6fe Mon Sep 17 00:00:00 2001 From: BaoNguyen09 Date: Mon, 9 Dec 2024 16:25:17 -0700 Subject: [PATCH 2/5] fix: Add newline after closing brace Additional change - Use === operator to not convert type that may cause unintended results --- .../Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/custom/az_card/src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php b/modules/custom/az_card/src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php index 13ffe27af7..9090bdfc8b 100644 --- a/modules/custom/az_card/src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php +++ b/modules/custom/az_card/src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php @@ -131,9 +131,10 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $link_url = Url::fromUri(urldecode('base:' . $item->link_uri)); } else { - if (strcmp($item->link_uri, "#") == 0) { + if (strcmp($item->link_uri, "#") === 0) { $link_url = $this->pathValidator->getUrlIfValid(''); - } else { + } + else { $link_url = $this->pathValidator->getUrlIfValid($item->link_uri ?? ''); } } From 3606c6e18f7b201d82e67e2448681c2d14be2ad2 Mon Sep 17 00:00:00 2001 From: BaoNguyen09 Date: Mon, 9 Dec 2024 16:29:34 -0700 Subject: [PATCH 3/5] fix: remove a whitespace after closing brace --- .../src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/custom/az_card/src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php b/modules/custom/az_card/src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php index 9090bdfc8b..a4b7e9a9c9 100644 --- a/modules/custom/az_card/src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php +++ b/modules/custom/az_card/src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php @@ -133,7 +133,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { else { if (strcmp($item->link_uri, "#") === 0) { $link_url = $this->pathValidator->getUrlIfValid(''); - } + } else { $link_url = $this->pathValidator->getUrlIfValid($item->link_uri ?? ''); } From cb0da7d266b26f918c0be4ea1b2e2d9c9e5204e2 Mon Sep 17 00:00:00 2001 From: BaoNguyen09 Date: Tue, 10 Dec 2024 17:02:42 -0700 Subject: [PATCH 4/5] feat: solve this issue for all anchor links --- .../Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/custom/az_card/src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php b/modules/custom/az_card/src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php index a4b7e9a9c9..73b7443855 100644 --- a/modules/custom/az_card/src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php +++ b/modules/custom/az_card/src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php @@ -125,14 +125,16 @@ 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 { - if (strcmp($item->link_uri, "#") === 0) { - $link_url = $this->pathValidator->getUrlIfValid(''); + // 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 ?? ''); From b8e37917cdc2bc14255f7d954236a2be3cd93857 Mon Sep 17 00:00:00 2001 From: Bao Nguyen <88011047+BaoNguyen09@users.noreply.github.com> Date: Wed, 11 Dec 2024 16:01:30 -0700 Subject: [PATCH 5/5] Update modules/custom/az_card/src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php Co-authored-by: Brian Berndt <74572157+bberndt-uaz@users.noreply.github.com> --- .../src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/custom/az_card/src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php b/modules/custom/az_card/src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php index 73b7443855..1e06d2f15b 100644 --- a/modules/custom/az_card/src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php +++ b/modules/custom/az_card/src/Plugin/Field/FieldFormatter/AZCardDefaultFormatter.php @@ -132,7 +132,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $link_url = Url::fromUri(urldecode('base:' . $item->link_uri)); } else { - // Check if the link is an anchor within the current page + // 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); }