You are here

public static function Heartbeat::parseHashtags in Heartbeat 8

1 call to Heartbeat::parseHashtags()
Heartbeat::buildMessage in src/Entity/Heartbeat.php
Builds a message template for a given HeartbeatType

File

src/Entity/Heartbeat.php, line 605

Class

Heartbeat

Namespace

Drupal\heartbeat\Entity

Code

public static function parseHashtags(&$message) {
  $lastRow = false;
  $tagsArray = explode('#', $message);
  $i = 0;
  $num = count($tagsArray);
  foreach ($tagsArray as $hashtag) {
    if ($i === $num - 1) {
      $lastTagArray = explode(' ', $hashtag);
      if (strlen($lastTagArray[1]) > 1) {
        $hashtag = trim($lastTagArray[0]);
        $lastRow = true;
        $remainder = '';
        $lastRowArgCount = count($lastTagArray);
        for ($x = 1; $x < $lastRowArgCount; $x++) {
          $remainder .= ' ' . $lastTagArray[$x];
        }
      }
    }
    $realHashtag = strpos($hashtag, ' ') ? substr($hashtag, 0, strpos($hashtag, ' ')) : $hashtag;
    $tid = \Drupal::entityQuery("taxonomy_term")
      ->condition("name", trim($realHashtag))
      ->condition('vid', [
      'twitter',
      'tags',
    ], 'IN')
      ->execute();
    if (count($tid) > 0) {
      $term = Term::load(array_values($tid)[0]);
      $link = Link::fromTextAndUrl('#' . $realHashtag, $term
        ->toUrl());
      $textToAddBack = $link
        ->toString() . str_replace($realHashtag, '', $hashtag);
      $tagsArray[$i] = '<div class="heartbeat-hashtag">';
      $tagsArray[$i] .= !$lastRow ? $textToAddBack . ' </div>' : $textToAddBack . '</div>' . $remainder;
    }
    $i++;
  }
  $message = '';
  foreach ($tagsArray as $replacements) {
    $message .= $replacements;
  }
}