From f4427594929a5db39231c3f22bc69101214a4112 Mon Sep 17 00:00:00 2001 From: Thomas Skerbis Date: Wed, 22 May 2024 00:08:05 +0200 Subject: [PATCH 1/4] New Utility getCustomUrl, corrected prepareCustomLink because CustomLinkField now uses rex:// links for articles --- lib/MForm/Utils/MFormOutputHelper.php | 55 ++++++++++++++++++++------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/lib/MForm/Utils/MFormOutputHelper.php b/lib/MForm/Utils/MFormOutputHelper.php index 316199e..d64b7e4 100644 --- a/lib/MForm/Utils/MFormOutputHelper.php +++ b/lib/MForm/Utils/MFormOutputHelper.php @@ -27,7 +27,7 @@ public static function isFirstSlice($sliceId): bool public static function prepareCustomLink(array $item, bool $externBlank = true): array { - // set url + // Set URL if (!isset($item['link']) || empty($item['link'])) { return $item; } @@ -35,42 +35,71 @@ public static function prepareCustomLink(array $item, bool $externBlank = true): $item['customlink_url'] = $item['link']; $item['customlink_target'] = ''; - // media file? - if (true === file_exists(rex_path::media($item['link']))) { + // Media file? + if (file_exists(rex_path::media($item['link']))) { $item['customlink_url'] = rex_url::media($item['link']); $item['customlink_class'] = ' media'; } else { - // no media and no url and is numeric it must be an rex article id - if (false === filter_var($item['link'], FILTER_VALIDATE_URL) && is_numeric($item['link'])) { - $item['customlink_url'] = rex_getUrl($item['link'], rex_clang::getCurrentId()); + // Check for rex:// URL + if (str_starts_with($item['link'], 'rex://')) { + $articleId = (int) substr($item['link'], 6); + $item['customlink_url'] = rex_getUrl($articleId, rex_clang::getCurrentId()); $item['customlink_class'] = ' intern'; if (empty($item['customlink_text'])) { - $art = rex_article::get($item['link'], rex_clang::getCurrentId()); + $art = rex_article::get($articleId, rex_clang::getCurrentId()); if ($art) { $item['customlink_text'] = $art->getName(); } } - } else { - $item['customlink_class'] = ' extern'; + } + else { + $item['customlink_class'] = ' external'; - if (strpos($item['customlink_url'], 'tel:') === 0) { + if (str_starts_with($item['customlink_url'], 'tel:')) { $item['customlink_class'] = 'tel'; - } elseif (strpos($item['customlink_url'], 'mailto:') === 0) { + } elseif (str_starts_with($item['customlink_url'], 'mailto:')) { $item['customlink_class'] = 'mail'; } if ($externBlank) { - $item['customlink_target'] = ' target="_blank"'; + $item['customlink_target'] = ' target="_blank" rel="noopener noreferrer"'; } } } - // no link text? + // No link text? if (empty($item['customlink_text'])) { $item['customlink_text'] = str_replace(['http://', 'https://'], '', $item['customlink_url']); } return $item; } + + function getCustomUrl(mixed $value = null, ?string $lang = null): string + { + // Check if the value is null or empty + if (is_null($value) || $value === '') { + return ''; + } + + // Determine the language to use (current language if none provided) + $lang = $lang ?? rex_clang::getCurrentId(); + + // Check if the value is a REDAXO article (starts with rex://) + if (str_starts_with($value, 'rex://')) { + $articleId = (int) substr($value, 6); // Remove 'rex://' and convert the rest to an integer + return rex_getUrl($articleId, $lang); + } + + // Check if the value is numeric + if (is_numeric($value)) { + $articleId = (int) $value; + return rex_getUrl($articleId, $lang); + } + + // If the value is neither a REDAXO URL nor numeric, return the value + return $value; + } + } From c7aaa9fa504093f867131b4534b8f927a4480a1a Mon Sep 17 00:00:00 2001 From: Thomas Skerbis Date: Wed, 22 May 2024 00:13:43 +0200 Subject: [PATCH 2/4] typo --- lib/MForm/Utils/MFormOutputHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/MForm/Utils/MFormOutputHelper.php b/lib/MForm/Utils/MFormOutputHelper.php index d64b7e4..a19024c 100644 --- a/lib/MForm/Utils/MFormOutputHelper.php +++ b/lib/MForm/Utils/MFormOutputHelper.php @@ -44,7 +44,7 @@ public static function prepareCustomLink(array $item, bool $externBlank = true): if (str_starts_with($item['link'], 'rex://')) { $articleId = (int) substr($item['link'], 6); $item['customlink_url'] = rex_getUrl($articleId, rex_clang::getCurrentId()); - $item['customlink_class'] = ' intern'; + $item['customlink_class'] = ' internal'; if (empty($item['customlink_text'])) { $art = rex_article::get($articleId, rex_clang::getCurrentId()); From 50babeefb5aaae320c7ab6f82ef2c6f64b9e60db Mon Sep 17 00:00:00 2001 From: Thomas Skerbis Date: Wed, 22 May 2024 10:21:41 +0200 Subject: [PATCH 3/4] Update MFormOutputHelper.php --- lib/MForm/Utils/MFormOutputHelper.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/MForm/Utils/MFormOutputHelper.php b/lib/MForm/Utils/MFormOutputHelper.php index a19024c..18b6e49 100644 --- a/lib/MForm/Utils/MFormOutputHelper.php +++ b/lib/MForm/Utils/MFormOutputHelper.php @@ -44,7 +44,7 @@ public static function prepareCustomLink(array $item, bool $externBlank = true): if (str_starts_with($item['link'], 'rex://')) { $articleId = (int) substr($item['link'], 6); $item['customlink_url'] = rex_getUrl($articleId, rex_clang::getCurrentId()); - $item['customlink_class'] = ' internal'; + $item['customlink_class'] = ' intern'; if (empty($item['customlink_text'])) { $art = rex_article::get($articleId, rex_clang::getCurrentId()); @@ -53,7 +53,18 @@ public static function prepareCustomLink(array $item, bool $externBlank = true): } } } - else { + // No media and no URL and is numeric, it must be a rex article id + elseif (!filter_var($item['link'], FILTER_VALIDATE_URL) && is_numeric($item['link'])) { + $item['customlink_url'] = rex_getUrl($item['link'], rex_clang::getCurrentId()); + $item['customlink_class'] = ' intern'; + + if (empty($item['customlink_text'])) { + $art = rex_article::get($item['link'], rex_clang::getCurrentId()); + if ($art) { + $item['customlink_text'] = $art->getName(); + } + } + } else { $item['customlink_class'] = ' external'; if (str_starts_with($item['customlink_url'], 'tel:')) { From 73299ad0b922253f1c6e1a80190cf16f150232df Mon Sep 17 00:00:00 2001 From: Thomas Skerbis Date: Wed, 22 May 2024 10:58:52 +0200 Subject: [PATCH 4/4] typo --- lib/MForm/Utils/MFormOutputHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/MForm/Utils/MFormOutputHelper.php b/lib/MForm/Utils/MFormOutputHelper.php index 18b6e49..1ab0b77 100644 --- a/lib/MForm/Utils/MFormOutputHelper.php +++ b/lib/MForm/Utils/MFormOutputHelper.php @@ -56,7 +56,7 @@ public static function prepareCustomLink(array $item, bool $externBlank = true): // No media and no URL and is numeric, it must be a rex article id elseif (!filter_var($item['link'], FILTER_VALIDATE_URL) && is_numeric($item['link'])) { $item['customlink_url'] = rex_getUrl($item['link'], rex_clang::getCurrentId()); - $item['customlink_class'] = ' intern'; + $item['customlink_class'] = ' internal'; if (empty($item['customlink_text'])) { $art = rex_article::get($item['link'], rex_clang::getCurrentId());