You are here

public static function Heartbeat::parseUsernames in Heartbeat 8

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

File

src/Entity/Heartbeat.php, line 650

Class

Heartbeat

Namespace

Drupal\heartbeat\Entity

Code

public static function parseUsernames(&$message) {
  $lastRow = false;
  $usersArray = explode('@', $message);
  $i = 0;
  $num = count($usersArray);
  foreach ($usersArray as $username) {
    if ($i === $num - 1) {
      $lastuserArray = explode(' ', $username);
      if (strlen($lastuserArray[1]) > 1) {
        $username = trim($lastuserArray[0]);
        $lastRow = true;
        $remainder = '';
        $lastRowArgCount = count($lastuserArray);
        for ($x = 1; $x < $lastRowArgCount; $x++) {
          $remainder .= ' ' . $lastuserArray[$x];
        }
      }
    }
    $tid = \Drupal::entityQuery("taxonomy_term")
      ->condition("name", trim($username))
      ->condition('vid', [
      'twitter_user',
      'usernames',
    ], 'IN')
      ->execute();
    if (count($tid) > 0) {
      $term = Term::load(array_values($tid)[0]);
      $link = Link::fromTextAndUrl('@' . $username, $term
        ->toUrl());
      $usersArray[$i] = '<div class="heartbeat-username">';
      $usersArray[$i] .= !$lastRow ? $link
        ->toString() . ' </div>' : $link
        ->toString() . '</div>' . $remainder;
    }
    $i++;
  }
  $message = '';
  foreach ($usersArray as $replacements) {
    $message .= $replacements;
  }
}