From b7d02f77273dc31cadb65184c7c044353854b578 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Lahmann?= <lahmann@ub.uni-leipzig.de>
Date: Tue, 3 Jan 2023 11:02:46 +0100
Subject: [PATCH] refs #21209 [finc] add ability to change link description via
 link rewrite

* set https as standard protocol if no specific protocol is defined in given url
---
 local/config/vufind/config.ini                  |  2 ++
 .../finc/src/finc/View/Helper/Root/Record.php   | 17 ++++++++++++++++-
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/local/config/vufind/config.ini b/local/config/vufind/config.ini
index ba72c6c8e78..779730170c5 100644
--- a/local/config/vufind/config.ini
+++ b/local/config/vufind/config.ini
@@ -2027,6 +2027,8 @@ remove[] = "gndmusic"
 ; identifier[function] calls a function if available with the link as parameter.
 ;    The function performed on the url *before* replacing with the replacement. It is 
 ;    intended to perform url-encoding links so they can be used as parameter value.
+; identifier[description] will replace the link description with the given value if
+;    the url was successfully replaced or a method got called upon it.
 ; Examples:
 ; url[pattern] = url.pattern.to.resolve.link
 ; url[method]  = resolveLink
diff --git a/module/finc/src/finc/View/Helper/Root/Record.php b/module/finc/src/finc/View/Helper/Root/Record.php
index c7117e000c2..f6e0ea4d287 100644
--- a/module/finc/src/finc/View/Helper/Root/Record.php
+++ b/module/finc/src/finc/View/Helper/Root/Record.php
@@ -376,6 +376,15 @@ class Record extends \VuFind\View\Helper\Root\Record
     protected function rewriteLink(&$link)
     {
         $rewrite = $this->config->LinksRewrite->toArray();
+
+        // helper function to replace the link description with a configured value
+        $overwriteDescription = function (&$link, $currentRewrite) {
+            // if another link description is configured use this instead
+            if (isset($currentRewrite['description'])) {
+                $link['desc'] = $currentRewrite['description'];
+            }
+        };
+
         foreach ($rewrite as $r) {
             // is remove pattern than suppress link refs #10834
             if (isset($r['remove'])) {
@@ -411,15 +420,21 @@ class Record extends \VuFind\View\Helper\Root\Record
                         1,
                         $count
                     );
+                    if ($count) {
+                        // overwrite description as we have a successful replace
+                        $overwriteDescription($link, $r);
+                    }
                     // add http if needed
                     // @to-do make it https compatible
                     if (!preg_match('/^(https?:\/\/)/', $link['url'])) {
-                        $link['url'] = 'http://' . $link['url'];
+                        $link['url'] = 'https://' . $link['url'];
                     }
                 }
                 // is method set so call alternatively method proceed link
                 if (isset($r['method']) && method_exists($this, $r['method'])) {
                     $link['url'] = $this->$r['method']($link['url']);
+                    // overwrite description as we called the defined method
+                    $overwriteDescription($link, $r);
                 } // end if isset method
             } // end if isset pattern
         } // end foreach
-- 
GitLab