You are here

function taxonomy_csv_term_find in Taxonomy CSV import/export 7.4

Same name and namespace in other branches
  1. 6.5 taxonomy_csv.term.api.inc \taxonomy_csv_term_find()
  2. 6.2 taxonomy_csv.term.api.inc \taxonomy_csv_term_find()
  3. 6.3 taxonomy_csv.term.api.inc \taxonomy_csv_term_find()
  4. 6.4 taxonomy_csv.term.api.inc \taxonomy_csv_term_find()
  5. 7.5 taxonomy_csv.term.api.inc \taxonomy_csv_term_find()

Find and load a term.

@todo Use a regular Drupal 7 dbquery.

Need to maintain a specific function and a direct query, because taxonomy_term_load_multiple doesn't manage 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 or a parent id. 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. Used with relations import.

$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.

2 calls to taxonomy_csv_term_find()
taxonomy_csv_line_import in import/taxonomy_csv.import.line.api.inc
Process the import of items.
taxonomy_csv_term_import in import/taxonomy_csv.import.line.api.inc
Update or create a term with the given name in the given vocabulary and given parent.

File

./taxonomy_csv.term.api.inc, line 30
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) {
    $found_term = taxonomy_term_load($term->tid);
  }
  elseif (isset($term->name)) {
    $name = drupal_strtolower(trim($term->name));
    if (drupal_strlen($name)) {

      // Only term id is selected, because taxonomy_term_load is used next in
      // order to take advantage of taxonomy cache.
      $sql = "\n        SELECT t.tid\n        FROM {taxonomy_term_data} t\n        INNER JOIN {taxonomy_term_hierarchy} h ON t.tid = h.tid\n        WHERE :name LIKE LOWER(t.name)\n      ";
      $args = array();
      $args[':name'] = $name;
      if (isset($term->vid) && $term->vid && !$all_vocabularies) {
        $sql .= ' AND {t.vid} = :vid';
        $args[':vid'] = $term->vid;
      }
      if ($parent_tid) {
        $sql .= ' AND {h.parent} = :parent';
        $args[':parent'] = $parent_tid;
      }
      $sql .= ' ORDER BY {t.tid} ASC LIMIT 1';
      $result = db_query($sql, $args);

      // Only zero or one result.
      foreach ($result as $item) {
        $found_term = taxonomy_term_load($item->tid);
      }
    }
  }

  // Complete and internal format term.
  if (isset($found_term)) {
    $found_term->parents = taxonomy_csv_term_get_parents_tids($found_term->tid);
    return _taxonomy_csv_term_format_internal($found_term);
  }

  // Not found, or error (neither tid nor name).
  return FALSE;
}