You are here

function tweet_feed_process_taxonomy in Tweet Feed 7.3

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 $terms: An array of taxonomy objects to be saved to the node for this tweet.

1 call to tweet_feed_process_taxonomy()
tweet_feed_save_tweet in ./tweet_feed.module
Save The Tweet (and profile)

File

./tweet_feed.module, line 615

Code

function tweet_feed_process_taxonomy($entities, $taxonomy) {
  $terms = array();
  foreach ($entities as $entity) {
    switch ($taxonomy) {
      case 'hashtag_terms':
        $taxonomy_name = $entity->text;
        break;
      case 'user_mention_terms':
        $taxonomy_name = $entity->screen_name;
        break;
      default:
        break;
    }

    // Check to see if this entity is in our hashtag taxonomy
    $vocabulary = taxonomy_vocabulary_machine_name_load($taxonomy);

    // if the taxonomy doesn't exist for some reason, then we need to fataly error
    if ($vocabulary == FALSE) {
      tweet_feed_set_message('The ' . $taxonomy . ' taxonomy vocabulary could not be found. Please uninstall and re-install Tweet Feed', 'fatal', $web_interface);
      return FALSE;
    }

    // Now that we have the vocabulary information, see if this term already
    // exists and if it does, give us the tid
    $result = db_select('taxonomy_term_data', 'td')
      ->fields('td', array(
      'tid',
    ))
      ->condition('td.vid', $vocabulary->vid)
      ->condition('td.name', $taxonomy_name)
      ->execute();

    // If we have one, great! If we don't, we need to create one and then get the tid
    // that way.
    if ($result
      ->rowCount() > 0) {
      $tid = $result
        ->fetchField();
    }
    else {
      $term = new stdClass();
      $term->vid = $vocabulary->vid;
      $term->name = $taxonomy_name;
      taxonomy_term_save($term);
      $tid = $term->tid;
    }
    $terms[] = $tid;
  }
  return $terms;
}