You are here

public function TweetFeed::processTaxonomy in Tweet Feed 8.3

Same name and namespace in other branches
  1. 4.x src/Controller/TweetFeed.php \Drupal\tweet_feed\Controller\TweetFeed::processTaxonomy()

Process hashtags and user mentions in tweets

We need to store these in our taxonomy (do not save duplicates) and save a reference to them in our created tweet node

Parameters

array $entities: An array of entities to be saved to our taxonomy.

string $taxonomy: The machine name of the taxonomy to be saved to.

array $tids: The term id's to be saved

1 call to TweetFeed::processTaxonomy()
TweetFeed::saveTweet in src/Controller/TweetFeed.php
Save the tweet to our tweet entity.

File

src/Controller/TweetFeed.php, line 439

Class

TweetFeed
Class TweetFeed.

Namespace

Drupal\tweet_feed\Controller

Code

public function processTaxonomy($entities, $taxonomy) {
  $tids = [];
  foreach ($entities as $entity) {
    switch ($taxonomy) {
      case 'twitter_hashtag_terms':
        $taxonomy_name = $entity->text;
        break;
      case 'twitter_user_mention_terms':
        $taxonomy_name = $entity->screen_name;
        break;
      default:
        break;
    }
    $tid = [];
    $terms = \Drupal::entityTypeManager()
      ->getStorage('taxonomy_term')
      ->loadTree($taxonomy);
    if (!empty($terms)) {
      foreach ($terms as $term) {
        if ($term->name == $taxonomy_name) {
          $tid[0]['value'] = $term->tid;
          break;
        }
      }
    }
    if (empty($tid)) {
      $new_term = \Drupal\taxonomy\Entity\Term::create([
        'vid' => $taxonomy,
        'name' => $taxonomy_name,
      ]);
      $new_term
        ->save();
      $tid = $new_term->tid
        ->getValue();
    }
    $tids[] = $tid[0]['value'];
  }
  return $tids;
}