Skip to content
Snippets Groups Projects
Commit 9a9fabc6 authored by Dorian Merz's avatar Dorian Merz Committed by Robert Lange
Browse files

refs #18442 [finc] introduce externalLinks ViewHelper

* externalize generation of off site links
* add target = _blank and rel=noopener to all external links
* set standard value for $translateLabel to false
* set merging order for parameters, so rel and target may be overwritten
parent ee37dfb0
No related merge requests found
<?php
/**
* External link view helper
*
* PHP version 7
*
* Copyright (C) Leipzig University Library 2020.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package View_Helpers
* @author Dorian Merz <merz@ub.uni-leipzig.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/
namespace finc\View\Helper\Root;
/**
* External link view helper
*
* @category VuFind
* @package View_Helpers
* @author Dorian Merz <merz@ub.uni-leipzig.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/
class ExternalLink extends \Zend\View\Helper\AbstractHelper
{
/**
* Context view helper
*
* @var \VuFind\View\Helper\Root\Translate
*/
protected $translator;
/**
* Default html attributes for external links
*
* @var string[]
*/
protected $defaultAttributes = [
'target' => "_blank",
'rel' => 'noopener'
];
/**
* Renders an anchor element (hyperlink) to an external website
*
* @param string $href URL of the external website
* @param string $label desired label or translation token
* @param array $attributes more attributes to apply to the a element,
* key-value-pairs
* @param bool $translateLabel true if label shall be translated
*
* @return string
*/
public function __invoke(
$href,
$label = '',
$attributes = [],
$translateLabel = false
) {
$link = '<a href="' . $href . '"';
$attributes = array_merge($this->defaultAttributes, $attributes);
foreach ($attributes as $key => $value) {
$link .= ' ' . $key . '="' . $value . '"';
}
$link .= '>';
if (empty($label)) {
$label = $href;
} elseif ($translateLabel) {
$label = $this->translate($label);
}
$link .= $label . '</a>';
return $link;
}
/**
* Helper function to provide access to Translate ViewHelper
*
* @param string $label translation token
*
* @return string
*/
protected function translate($label)
{
if (!isset($this->translator)) {
$this->translator = $this->getView()->plugin('translate');
}
return $this->translator->translate($label);
}
}
...@@ -151,7 +151,8 @@ class Factory ...@@ -151,7 +151,8 @@ class Factory
// constructor // constructor
if (file_exists( if (file_exists(
\VuFind\Config\Locator::getConfigPath('OpenUrlRules.json') \VuFind\Config\Locator::getConfigPath('OpenUrlRules.json')
)) { )
) {
$openUrlRules = json_decode( $openUrlRules = json_decode(
file_get_contents( file_get_contents(
\VuFind\Config\Locator::getConfigPath('OpenUrlRules.json') \VuFind\Config\Locator::getConfigPath('OpenUrlRules.json')
...@@ -194,7 +195,8 @@ class Factory ...@@ -194,7 +195,8 @@ class Factory
// constructor // constructor
if (file_exists( if (file_exists(
\VuFind\Config\Locator::getConfigPath('ExternalCatalogue.json') \VuFind\Config\Locator::getConfigPath('ExternalCatalogue.json')
)) { )
) {
$externalAccessLinks = json_decode( $externalAccessLinks = json_decode(
file_get_contents( file_get_contents(
\VuFind\Config\Locator::getConfigPath('ExternalCatalogue.json') \VuFind\Config\Locator::getConfigPath('ExternalCatalogue.json')
...@@ -209,9 +211,26 @@ class Factory ...@@ -209,9 +211,26 @@ class Factory
); );
} }
/**
* Construct Head Title Helper
*
* @param ContainerInterface $container Service manager
*
* @return HeadTitle
*/
public static function getHeadTitle(ContainerInterface $container) public static function getHeadTitle(ContainerInterface $container)
{ {
$config = $container->get('VuFind\Config')->get('config')->Site; $config = $container->get('VuFind\Config')->get('config')->Site;
return new HeadTitle($config->title ?? ''); return new HeadTitle($config->title ?? '');
} }
/**
* Construct ExternalLink
*
* @return ExternalLink
*/
public static function getExternalLink()
{
return new ExternalLink();
}
} }
...@@ -18,6 +18,7 @@ return [ ...@@ -18,6 +18,7 @@ return [
'record' => 'finc\View\Helper\Root\Record', 'record' => 'finc\View\Helper\Root\Record',
'flashmessages' => 'finc\View\Helper\Root\Flashmessages', 'flashmessages' => 'finc\View\Helper\Root\Flashmessages',
'headTitle' => 'finc\View\Helper\Root\HeadTitle', 'headTitle' => 'finc\View\Helper\Root\HeadTitle',
'externalLink' => 'finc\View\Helper\Root\ExternalLink',
], ],
'factories' => [ 'factories' => [
'finc\View\Helper\Root\BranchInfo' => 'finc\View\Helper\Root\BranchInfo' =>
...@@ -44,6 +45,8 @@ return [ ...@@ -44,6 +45,8 @@ return [
'VuFind\View\Helper\Root\FlashmessagesFactory', 'VuFind\View\Helper\Root\FlashmessagesFactory',
'finc\View\Helper\Root\HeadTitle' => 'finc\View\Helper\Root\HeadTitle' =>
'finc\View\Helper\Root\Factory::getHeadTitle', 'finc\View\Helper\Root\Factory::getHeadTitle',
'finc\View\Helper\Root\ExternalLink' =>
'finc\View\Helper\Root\Factory::getExternalLink',
] ]
] ]
]; ];
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment