You are here

public static function FeedImportFilter::setTaxonomyTerms in Feed Import 8

Same name in this branch
  1. 8 feed_import_base/filters/feed_import_default_filters.php \FeedImportFilter::setTaxonomyTerms()
  2. 8 feed_import_base/src/Filter/FeedImportFilter.php \Drupal\feed_import_base\FeedImportFilter::setTaxonomyTerms()

Save specified taxonomy terms to vocabulary

Parameters

mixed $name: A string or an array of strings

int|string $voc: (optionally) Vocabulary id/name

Return value

mixed Fetched and inserted tids

File

feed_import_base/src/Filter/FeedImportFilter.php, line 553

Class

FeedImportFilter
This class contains default filters for feed import.

Namespace

Drupal\feed_import_base

Code

public static function setTaxonomyTerms($name, $voc = 0) {
  if (!is_numeric($voc)) {
    $voc = self::getVidFromName($voc);
  }
  if (!is_array($name)) {
    $name = array(
      $name,
    );
  }
  $tids = array();
  $existing = self::getTaxonomyIdByName($name, $voc);
  if (!empty($existing)) {
    $existing = \Drupal::entityManager()
      ->getStorage('taxonomy_term')
      ->loadByProperties(array(
      'vid' => $voc,
    ));
    foreach ($existing as &$term) {
      $tids[\Drupal\Component\Utility\Unicode::strtolower($term->name)] = $term->tid;
      $term = NULL;
    }
  }
  unset($existing);
  foreach ($name as &$term) {
    $lterm = \Drupal\Component\Utility\Unicode::strtolower($term);
    if (!isset($tids[$lterm])) {
      $t = new stdClass();
      $t->vid = $voc;
      $t->name = $term;
      $t
        ->save();
      $tids[$lterm] = $t->tid;
      $t = NULL;
      $term = NULL;
    }
  }
  return $tids;
}