You are here

public function FeedsTermProcessor::process in Feeds 7

Same name and namespace in other branches
  1. 6 plugins/FeedsTermProcessor.inc \FeedsTermProcessor::process()

Implements FeedsProcessor::process().

Overrides FeedsProcessor::process

File

plugins/FeedsTermProcessor.inc, line 16
FeedsTermProcessor class.

Class

FeedsTermProcessor
Feeds processor plugin. Create taxonomy terms from feed items.

Code

public function process(FeedsImportBatch $batch, FeedsSource $source) {
  if (empty($this->config['vocabulary'])) {
    throw new Exception(t('You must define a vocabulary for Taxonomy term processor before importing.'));
  }

  // Count number of created and updated nodes.
  $created = $updated = $no_name = 0;
  while ($item = $batch
    ->shiftItem()) {
    if (!($tid = $this
      ->existingItemId($batch, $source)) || $this->config['update_existing'] != FEEDS_SKIP_EXISTING) {

      // Map item to a term.
      $term = new stdClass();
      if ($tid && $this->config['update_existing'] == FEEDS_UPDATE_EXISTING) {
        $term = taxonomy_term_load($tid);
        $term = module_invoke_all('feeds_taxonomy_load', $term);
      }
      $term->entity_type = 'taxonomy_term';
      $term = $this
        ->map($batch, $term, $source->feed_nid);

      // Check if term name is set, otherwise continue.
      if (empty($term->name)) {
        $no_name++;
        continue;
      }

      // Add term id if available.
      if (!empty($tid)) {
        $term->tid = $tid;
      }

      // Save the term.
      $term->feeds_importer_id = $this->id;
      $term->feed_nid = $source->feed_nid;
      taxonomy_term_save($term);
      if ($tid) {
        $updated++;
      }
      else {
        $created++;
      }
    }
  }

  // Set messages.
  $vocabulary = $this
    ->vocabulary();
  if ($no_name) {
    drupal_set_message(format_plural($no_name, 'There was @number term that could not be imported because their name was empty. Check mapping settings on Taxomy term processor.', 'There were @number terms that could not be imported because their name was empty. Check mapping settings on Taxomy term processor.', array(
      '@number' => $no_name,
    )), 'error');
  }
  if ($created) {
    drupal_set_message(format_plural($created, 'Created @number term in !vocabulary.', 'Created @number terms in !vocabulary.', array(
      '@number' => $created,
      '!vocabulary' => $vocabulary->name,
    )));
  }
  elseif ($updated) {
    drupal_set_message(format_plural($updated, 'Updated @number term in !vocabulary.', 'Updated @number terms in !vocabulary.', array(
      '@number' => $updated,
      '!vocabulary' => $vocabulary->name,
    )));
  }
  else {
    drupal_set_message(t('There are no new terms.'));
  }
}