function get_term_reference in Content Import 8.9
To get Reference field ids.
1 call to get_term_reference()
- import_node in ./
contentimport.module - To import data as Content type nodes.
File
- ./
contentimport.module, line 46 - Module file for Contentimport.
Code
function get_term_reference($voc, $terms) {
$vocName = strtolower($voc);
$vid = preg_replace('@[^a-z0-9_]+@', '_', $vocName);
$vocabularies = Vocabulary::loadMultiple();
/* Create Vocabulary if it is not exists */
if (!isset($vocabularies[$vid])) {
create_voc($vid, $voc);
}
$termArray = array_map('trim', explode(',', $terms));
$termIds = [];
foreach ($termArray as $term) {
$term_id = get_term_id($term, $vid);
if (empty($term_id)) {
$term_id = create_term($voc, $term, $vid);
}
$termIds[]['target_id'] = $term_id;
}
return $termIds;
}