-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
linkUrlFilter moved from Controller to its own Twig extension class
Moving the code that provides the functionality of the link_url filter used in Twig templates into its own class (LinkUrlExtension.php) makes the code cleaner and enables the extraction of translation messages from Twig templates.
- Loading branch information
Showing
4 changed files
with
70 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFilter; | ||
|
||
class LinkUrlExtension extends AbstractExtension | ||
{ | ||
private $model; | ||
|
||
public function __construct($model) | ||
{ | ||
$this->model = $model; | ||
} | ||
|
||
public function getFilters() | ||
{ | ||
return [ | ||
new TwigFilter('link_url', [$this, 'linkUrlFilter']), | ||
]; | ||
} | ||
|
||
/** | ||
* Creates Skosmos links from uris. | ||
* @param string $uri | ||
* @param Vocabulary $vocab | ||
* @param string $lang | ||
* @param string $type | ||
* @param string $clang content | ||
* @param string $term | ||
* @throws Exception if the vocabulary ID is not found in configuration | ||
* @return string containing the Skosmos link | ||
*/ | ||
public function linkUrlFilter($uri, $vocab, $lang, $type = 'page', $clang = null, $term = null) | ||
{ | ||
// $vocab can either be null, a vocabulary id (string) or a Vocabulary object | ||
if ($vocab === null) { | ||
return $uri; | ||
} elseif (is_string($vocab)) { | ||
$vocid = $vocab; | ||
$vocab = $this->model->getVocabulary($vocid); | ||
} else { | ||
$vocid = $vocab->getId(); | ||
} | ||
|
||
$params = []; | ||
if (isset($clang) && $clang !== $lang) { | ||
$params['clang'] = $clang; | ||
} | ||
|
||
if (isset($term)) { | ||
$params['q'] = $term; | ||
} | ||
|
||
$localname = $vocab->getLocalName($uri); | ||
if ($localname !== $uri && $localname === urlencode($localname)) { | ||
$paramstr = count($params) > 0 ? '?' . http_build_query($params) : ''; | ||
if ($type && $type !== '' && $type !== 'vocab' && !($localname === '' && $type === 'page')) { | ||
return "$vocid/$lang/$type/$localname" . $paramstr; | ||
} | ||
|
||
return "$vocid/$lang/$localname" . $paramstr; | ||
} | ||
|
||
$params['uri'] = $uri; | ||
return "$vocid/$lang/$type/?" . http_build_query($params); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters