You are here

public static function StatusHeartPost::parseHashtags in Heartbeat 8

2 calls to StatusHeartPost::parseHashtags()
StatusForm::statusAjaxSubmit in modules/statusmessage/src/Form/StatusForm.php
StatusHeartPost::processTerms in modules/statusmessage/src/StatusHeartPost.php

File

modules/statusmessage/src/StatusHeartPost.php, line 173

Class

StatusHeartPost
@property \Drupal\statusmessage\MarkupGenerator generator @property message

Namespace

Drupal\statusmessage

Code

public static function parseHashtags($message) {
  $tids = array();
  $i = 0;
  $tagsArray = explode('#', $message);
  unset($tagsArray[0]);
  $num = count($tagsArray);
  foreach ($tagsArray as $hashtag) {
    if ($i === $num - 1) {
      $lastTagArray = explode(' ', $hashtag);
      if (strlen($lastTagArray[1]) > 1) {
        $hashtag = trim($lastTagArray[0]);
      }
    }
    $tid = \Drupal::entityQuery("taxonomy_term")
      ->condition("name", trim($hashtag))
      ->condition('vid', [
      'twitter',
      'tags',
    ], 'IN')
      ->execute();
    if (count($tid) > 0) {
      if (\Drupal::moduleHandler()
        ->moduleExists('heartbeat')) {
        \Drupal\heartbeat\Entity\Heartbeat::updateTermUsage(array_values($tid)[0], 'tags');
      }
      $tids[] = array_values($tid)[0];
    }
    else {
      $term = Term::create([
        'name' => trim($hashtag),
        'vid' => 'tags',
        'field_count' => 1,
      ]);
      if ($term
        ->save()) {
        $tids[] = $term
          ->id();
        if (\Drupal::moduleHandler()
          ->moduleExists('heartbeat')) {
          \Drupal\heartbeat\Entity\Heartbeat::newTermUsage($term
            ->id());
        }
      }
    }
    $i++;
  }
  return $tids;
}