You are here

function LinksConverter::replace in Hashtags 8

File

src/Plugin/Filter/LinksConverter.php, line 23

Class

LinksConverter

Namespace

Drupal\hashtags\Plugin\Filter

Code

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;

  // hashtag is not exists - show without link
  if (empty($hashtag_tid)) {
    return $first_delimeter . $hashtag_name;
  }

  // Fatal error: [] operator not supported for strings in /includes/common.inc on line 2442
  // Issue comes up when we try to bind attribute to link which has path parameter of the current page............

  /*if ($_GET['q'] == 'taxonomy/term/'.$hashtag_tid) {
        $hashtag_link = l($hashtag_name, 'taxonomy/term/'.$hashtag_tid);
    } else {
        $hashtag_link = l($hashtag_name, 'taxonomy/term/'.$hashtag_tid, array('attributes' => array('class' => 'hashtag')));
    }*/
  $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;
}