public static function FeedImportFilter::setTaxonomyTerms in Feed Import 8
Same name in this branch
- 8 feed_import_base/filters/feed_import_default_filters.php \FeedImportFilter::setTaxonomyTerms()
- 8 feed_import_base/src/Filter/FeedImportFilter.php \Drupal\feed_import_base\FeedImportFilter::setTaxonomyTerms()
Same name and namespace in other branches
- 7.3 feed_import_base/filters/feed_import_default_filters.php \FeedImportFilter::setTaxonomyTerms()
- 7 feed_import_filter.inc.php \FeedImportFilter::setTaxonomyTerms()
- 7.2 feed_import_filter.inc.php \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/
filters/ feed_import_default_filters.php, line 557 - Provides filters for feed import.
Class
- FeedImportFilter
- This class contains default filters for feed import.
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;
}