You are here

function _taxonomy_csv_field_update_taxonomy_term_reference in Taxonomy CSV import/export 7.5

Helper to convert an internal array field to a standard term reference field.

Parameters

$field_name: Field to update.

$term: Term object.

$existing_term: Previous term object.

Return value

Nothing: $term object is passed by reference.

File

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

Code

function _taxonomy_csv_field_update_taxonomy_term_reference($field_name, $term, $existing_term) {

  // Complete term if there are already items.
  if (is_array($existing_term->{$field_name}) && !empty($existing_term->{$field_name})) {
    $new_field = $term->{$field_name};
    $term->{$field_name} = $existing_term->{$field_name};
    $field =& $term->{$field_name};
    foreach ($new_field as $field_language => $array) {

      // Don't update if new field is empty.
      if (!empty($array)) {

        // Update the value for that language.
        if (isset($field[$field_language])) {

          // Don't use array_merge or array_merge_recursive to avoid duplicates.
          $existing = array();
          foreach ($field[$field_language] as &$value) {
            $existing[] =& $value['tid'];
          }
          foreach ($array as &$new_value) {

            // Avoid duplicates and avoid to append an empty item.
            if (!in_array($new_value['tid'], $existing) && $new_value['tid'] != 0) {
              $field[$field_language][] = $new_value;
            }
          }
        }
        else {
          foreach ($array as &$new_value) {

            // Avoid to append an empty item.
            if ($new_value['tid'] != 0) {
              $field[$field_language][] = $new_value;
            }
          }
        }
      }
    }
  }

  // Else nothing to do: term contains already new field.
}