LinksConverter.php in Hashtags 8
File
src/Plugin/Filter/LinksConverter.php
View source
<?php
namespace Drupal\hashtags\Plugin\Filter;
use Drupal\Core\Link;
use Drupal\Core\Url;
class LinksConverter {
private $hashtags_tids;
function __construct($hashtags_tids) {
$this->hashtags_tids = $hashtags_tids;
}
function replace($matches) {
if (isset($this->hashtags_tids)) {
$hashtags_tids = $this->hashtags_tids;
}
$first_delimeter = isset($matches[1]) ? $matches[1] : '';
$hashtag_name = isset($matches[3]) ? $matches[3] : '';
$hashtag_tid = isset($this->hashtags_tids[strtolower($hashtag_name)]) ? $this->hashtags_tids[strtolower($hashtag_name)] : '';
$hashtag_name = '#' . $hashtag_name;
if (empty($hashtag_tid)) {
return $first_delimeter . $hashtag_name;
}
$hashtag_link = Link::fromTextAndUrl($hashtag_name, Url::fromRoute('entity.taxonomy_term.canonical', array(
'taxonomy_term' => $hashtag_tid,
), array(
'attributes' => array(
'class' => 'hashtag',
),
)))
->toString();
return $first_delimeter . $hashtag_link;
}
}