You are here

function taxonomy_csv_term_import in Taxonomy CSV import/export 7.5

Same name and namespace in other branches
  1. 6.5 import/taxonomy_csv.import.line.api.inc \taxonomy_csv_term_import()
  2. 6.2 import/taxonomy_csv.import.line.api.inc \taxonomy_csv_term_import()
  3. 6.3 import/taxonomy_csv.import.line.api.inc \taxonomy_csv_term_import()
  4. 6.4 import/taxonomy_csv.import.line.api.inc \taxonomy_csv_term_import()
  5. 7.4 import/taxonomy_csv.import.line.api.inc \taxonomy_csv_term_import()

Update/create a term with the given name and parent in the given vocabulary.

@todo Include true update/replace/ignore for fields.

Parameters

$term: A term object to import. Term contains either:

  • 'name' => term name string,
  • 'tid' => term id,

and eventually, matching options:

  • 'vid' => the vocabulary id where to import,
  • 'description' => description string,
  • 'format' => the filter format of the description,
  • 'language' => the language id of the term,
  • 'weight' => weight integer,
  • 'parent' => array of first level parent tids,

$update_or_ignore: (Optional) Type of import on existing terms. Default to ignore and create.

$parent_tid: (Optional) The direct parent term id where to restrict search. Used for structure import. Default to NULL (no parent restriction).

Return value

array 'name' => term name, 'tid' => term id, 'msg' => messages array.

7 calls to taxonomy_csv_term_import()
taxonomy_csv_line_import_fields in import/taxonomy_csv.import.line.api.inc
Import a fields line.
taxonomy_csv_line_import_flat in import/taxonomy_csv.import.line.api.inc
Import a flat line.
taxonomy_csv_line_import_localize in import/taxonomy_csv.import.line.api.inc
Import a localization line.
taxonomy_csv_line_import_structure in import/taxonomy_csv.import.line.api.inc
Import a structure line.
taxonomy_csv_line_import_tid in import/taxonomy_csv.import.line.api.inc
Import a flat or a structure line with tids and names.

... See full list

File

import/taxonomy_csv.import.line.api.inc, line 655
Process import of a csv line, i.e. of a term or a list of terms.

Code

function taxonomy_csv_term_import($term, $update_or_ignore = TAXONOMY_CSV_EXISTING_IGNORE, $parent_tid = NULL) {
  $messages = array();

  // Basic check to avoid notices when "Check lines" option is disabled.
  if (!isset($term->name) && !isset($term->tid) || isset($term->name) && !$term->name || isset($term->tid) && !$term->tid) {
    $term->tid = 0;
    $messages[] = 432;

    // Warning line contains an empty term.
    return array(
      'name' => '',
      'tid' => 0,
      'msg' => $messages,
    );
  }
  switch ($update_or_ignore) {
    case TAXONOMY_CSV_EXISTING_UPDATE:
      $existing_term = taxonomy_csv_term_find($term, FALSE, $parent_tid);
      if ($existing_term) {

        // Update only fields that are set. Other fields are not changed.
        foreach ($term as $key => $value) {

          // Arrays: merge existing and new items.
          // Only used for parent in standard taxonomy of Drupal 7.
          if (is_array($value) && isset($existing_term->{$key})) {
            $term->{$key} = array_unique(array_merge($existing_term->{$key}, $value));
          }

          // Else simply use new key: no merge is possible and useful for
          // string, numeric or boolean fields.
          // Description was an exception in previous version, but this
          // exception is removed for simplicity and real use of this module.
          // An option may be added if needed.
        }

        // Existing fields of existing term should be kept even if they have
        // not been set in new term.
        $term = (object) array_merge((array) $existing_term, (array) $term);
      }
      else {
        unset($term->tid);
      }
      break;
    case TAXONOMY_CSV_EXISTING_IGNORE_PREVIOUS:

      // Doesn't ignore, but use previous parents.
      $existing_term = taxonomy_csv_term_find($term, FALSE, $parent_tid);
      if ($existing_term) {

        // All fields are replaced by new ones. Other existing fields of
        // existing term should be kept even if they are not set in new term.
        $term = (object) array_merge((array) $existing_term, (array) $term);
      }
      else {
        unset($term->tid);
      }
      break;
    case TAXONOMY_CSV_EXISTING_IGNORE:

      // Nothing to do: keep the term.
      break;
  }

  // Finish to set the term to avoid NULL. Format and language are set before.
  if (!isset($term->format)) {
    $term->format = 'plain_text';
  }
  if (!isset($term->description)) {
    $term->description = '';
  }

  // Currently, custom fields are managed by an external function.
  if (isset($term->fields_to_import)) {
    $messages = _taxonomy_csv_term_field_import($term, $update_or_ignore);
    unset($term->fields_to_import);
    unset($term->fields_to_import_instances);
    unset($term->fields_to_import_fields);
    if (_taxonomy_csv_worst_message($messages) < TAXONOMY_CSV_PROCESS_NOTICE) {
      return array(
        'name' => '',
        'tid' => 0,
        'msg' => $messages,
      );
    }
  }

  // Save regularly formatted term.
  // Return either SAVED_NEW, SAVED_UPDATED or FALSE (no change).
  $result = taxonomy_term_save($term);
  $messages[] = $result == SAVED_NEW ? 691 : 692;

  // Saved or updated.
  return array(
    'name' => $term->name,
    'tid' => $term->tid,
    'msg' => $messages,
  );
}