You are here

private function StatusInstagram::processTerms in Heartbeat 8

1 call to StatusInstagram::processTerms()
StatusInstagram::setNodeData in modules/statusmessage/src/StatusInstagram.php

File

modules/statusmessage/src/StatusInstagram.php, line 261

Class

StatusInstagram

Namespace

Drupal\statusmessage

Code

private function processTerms($data) {
  $terms = new \stdClass();
  $terms->tags = [];
  $terms->users = [];
  $terms->username = -1;
  if ($data->user->screen_name) {
    $term = \Drupal::entityQuery('taxonomy_term')
      ->condition('name', $data->user->screen_name)
      ->condition('vid', 'instagram_user')
      ->execute();
    if (count($term) < 1) {
      $term = Term::create([
        'name' => $data->user->screen_name,
        'vid' => 'instagram_user',
        'field_count' => 1,
      ]);
      if ($term
        ->save()) {
        $terms->username = $term
          ->id();
        if (\Drupal::moduleHandler()
          ->moduleExists('heartbeat')) {
          \Drupal\heartbeat\Entity\Heartbeat::newTermUsage($term
            ->id());
        }
      }
      else {
        \Drupal::logger('StatusInstagram')
          ->warning('Could not save term with name %name', array(
          '%name' => $data->user->screen_name,
        ));
      }
    }
    else {
      $terms->username = array_values($term)[0];
      if (\Drupal::moduleHandler()
        ->moduleExists('heartbeat')) {
        \Drupal\heartbeat\Entity\Heartbeat::updateTermUsage(array_values($term)[0], 'instagram_user');
      }
    }
    $term = NULL;
    foreach ($data->entities->hashtags as $key => $h) {
      $term = \Drupal::entityQuery('taxonomy_term')
        ->condition('name', $h->text)
        ->condition('vid', 'Instagram')
        ->execute();
      if (count($term) < 1) {
        $term = Term::create([
          'name' => $h->text,
          'vid' => 'Instagram',
          'field_count' => 1,
        ]);
        if ($term
          ->save()) {
          $terms->tags[] = $term
            ->id();
          if (\Drupal::moduleHandler()
            ->moduleExists('heartbeat')) {
            \Drupal\heartbeat\Entity\Heartbeat::newTermUsage($term
              ->id());
          }
        }
        else {
          \Drupal::logger('StatusInstagram')
            ->warning('Could not save term with name %name', array(
            '%name' => $h->text,
          ));
        }
      }
      else {
        $terms->tags[] = array_values($term)[0];
        if (\Drupal::moduleHandler()
          ->moduleExists('heartbeat')) {
          \Drupal\heartbeat\Entity\Heartbeat::updateTermUsage(array_values($term)[0], 'Instagram');
        }
      }
    }
    $term = NULL;
    foreach ($data->entities->user_mentions as $u) {
      $term = \Drupal::entityQuery('taxonomy_term')
        ->condition('name', $u->screen_name)
        ->condition('vid', 'instagram_user')
        ->execute();
      if (count($term) < 1) {
        $term = Term::create([
          'name' => $u->screen_name,
          'vid' => 'instagram_user',
        ]);
        if ($term
          ->save()) {
          $terms->users[] = $term
            ->id();
        }
        else {
          \Drupal::logger('StatusInstagram')
            ->warning('Could not save term with name %name', array(
            '%name' => $u->screen_name,
          ));
        }
      }
      else {
        $terms->users[] = array_values($term)[0];
      }
    }
  }
  return $terms;
}