function taxonomy_csv_term_find in Taxonomy CSV import/export 7.5
Same name and namespace in other branches
- 6.5 taxonomy_csv.term.api.inc \taxonomy_csv_term_find()
- 6.2 taxonomy_csv.term.api.inc \taxonomy_csv_term_find()
- 6.3 taxonomy_csv.term.api.inc \taxonomy_csv_term_find()
- 6.4 taxonomy_csv.term.api.inc \taxonomy_csv_term_find()
- 7.4 taxonomy_csv.term.api.inc \taxonomy_csv_term_find()
Find a term by its name and load it. This function manages duplicates.
If the term has got duplicates, only first one (lower tid) will be returned.
@note Need to maintain a specific function and a direct query, because taxonomy_term_load_multiple doesn't manage parents and duplicates. db_query() is prefered, because it's four times faster than db_select(), EntityFieldQuery and taxonomy_get_term_by_name() (these last two don't manage parents'). Anyway terms are loaded to get all fields, specialy parents.
Parameters
$term: The term object to find. It's not necessarily a standard term object. It's an object which needs only a name and eventually a vid, a parent id and a language. Of course, if tid is set, the found term is the existing one.
$all_vocabularies: (Optional) Boolean. Search in all vocabularies or only in $term->vid vocabulary (default), which need to be set.
$parent_tid: (Optional) The direct parent term id where to restrict search. Used for structure import. Default to NULL (no parent restriction).
Return value
Formatted found term object, or FALSE if not found or error.
1 call to taxonomy_csv_term_find()
- taxonomy_csv_term_import in import/
taxonomy_csv.import.line.api.inc - Update/create a term with the given name and parent in the given vocabulary.
File
- ./
taxonomy_csv.term.api.inc, line 34 - Find, get and set full or detail term items.
Code
function taxonomy_csv_term_find($term, $all_vocabularies = FALSE, $parent_tid = NULL) {
if (isset($term->tid) && $term->tid) {
$result = taxonomy_term_load($term->tid);
if ($result) {
return $result;
}
unset($term->tid);
return FALSE;
}
static $flag_i18n = NULL;
if (is_NULL($flag_i18n)) {
$flag_i18n = module_exists('i18n_taxonomy');
}
if (isset($term->name)) {
$name = drupal_strtolower(trim($term->name));
// Only term id is selected, because taxonomy_term_load is used next in
// order to take advantage of taxonomy cache.
$query = db_select('taxonomy_term_data', 't');
$query
->addField('t', 'tid');
$query
->innerJoin('taxonomy_term_hierarchy', 'h', 't.tid = h.tid');
$query
->where('LOWER(t.name) = :name', array(
':name' => $name,
));
if (isset($term->vid) && $term->vid && !$all_vocabularies) {
$query
->condition('t.vid', $term->vid);
}
if ($flag_i18n && isset($term->language)) {
$query
->condition('t.language', $term->language);
}
if ($parent_tid) {
$query
->condition('h.parent', $parent_tid);
}
$query
->orderBy('t.tid', 'ASC');
$query
->extend('PagerDefault')
->limit(1);
$result = $query
->execute()
->fetchField();
if ($result) {
return taxonomy_term_load($result);
}
}
// Not found or error.
unset($term->tid);
return FALSE;
}