Wiki.php in Freelinking 4.0.x
File
src/Plugin/freelinking/Wiki.php
View source
<?php
namespace Drupal\freelinking\Plugin\freelinking;
use Drupal\Core\Url;
use Drupal\freelinking\Plugin\FreelinkingPluginBase;
class Wiki extends FreelinkingPluginBase {
protected $wikiMap = [
'wp' => 'wikipedia',
'wq' => 'wikiquote',
'wt' => 'wiktionary',
'wn' => 'wikinews',
'ws' => 'wikisource',
'wb' => 'wikibooks',
];
public function getTip() {
return $this
->t('Click to view a wiki page.');
}
public function getIndicator() {
return '/w((ikipedia|p)|(ikiquote|q)|(iktionary|t)|(ikinews|n)|(ikisource|s)|(ikibooks|b))?$/A';
}
public function buildLink(array $target) {
$wikiname = $this
->getWikiFromIndicator($target['indicator']);
$langcode = $target['language']
->getId();
$dest = str_replace(' ', '_', $target['dest']);
$wikiurl = 'https://' . $langcode . '.' . $wikiname . '.org/wiki/' . $dest;
return [
'#type' => 'link',
'#title' => isset($target['text']) ? $target['text'] : ucwords($wikiname),
'#url' => Url::fromUri($wikiurl, [
'language' => $target['language'],
'absolute' => TRUE,
]),
'#attributes' => [
'title' => isset($target['tooltip']) ? $target['tooltip'] : $this
->getTip(),
],
];
}
protected function getWikiFromIndicator($indicator) {
$ret = '';
if (preg_match($this
->getIndicator(), $indicator, $matches)) {
if (strlen($matches[0]) === 2) {
$ret = $this->wikiMap[$matches[0]];
}
else {
$ret = $matches[0];
}
}
return $ret;
}
}
Classes
Name |
Description |
Wiki |
Freelinking wiki plugin implements wikipedia amongst other plugins. |