You are here

function taxonomy_xml_setup_vocabulary_from_data in Taxonomy import/export via XML 6.2

Same name and namespace in other branches
  1. 7 formats/xml_format.inc \taxonomy_xml_setup_vocabulary_from_data()

The setting is to initialize a vocabulary from the given settings (an array of attributes).

Depending on the state of pre-existing taxonomies, this will either create a new vocab, or update an existing one.

Parameters

$edit: array of vocabulary settings parsed from the data.

Return value

the appropriate vocabulary id

1 call to taxonomy_xml_setup_vocabulary_from_data()
taxonomy_xml_xml_parse in ./xml_format.inc
Initiate the parser on the custom XML schema.

File

./xml_format.inc, line 306
Include routines for the original XML parsing and taxonomy/term creation.

Code

function taxonomy_xml_setup_vocabulary_from_data($edit) {

  // Massage/Parse vocabulary node types from csv value (old syntax) into the array we need
  if (!empty($edit['nodes']) && is_string($edit['nodes'])) {
    $node_types = explode(',', $edit['nodes']);
    if (!empty($node_types)) {
      $edit['nodes'] = array_combine($node_types, $node_types);
    }
  }

  // Potential conflict with vids?
  // See if the data does in fact define a vid
  if (isset($edit['vid'])) {
    $placeholder_vocabulary = (array) taxonomy_vocabulary_load($edit['vid']);

    // Does this vocab already exist?
    // Note that a failure to load a vocab does not return NULL, it returns an array with an actual empty item. Annoying
    if (empty($placeholder_vocabulary['vid'])) {
      drupal_set_message(t("\n        Declared vocab ID:%data_vid does NOT yet exist.\n        Need to make a new one, (cannot retain the internal IDs).", array(
        '%data_vid' => $edit['vid'],
      )));

      // Drupal database API will not allow me to override the VID when creating a new vocab.
      // Fair enough I guess. Make a brand new one.
      $placeholder_vocabulary = (array) _taxonomy_xml_get_vocabulary_placeholder($edit['name']);
      $vid = $placeholder_vocabulary['vid'];

      // Use other attributes defined in the source file (all except the vid)
      unset($edit['vid']);
      $placeholder_vocabulary = array_merge($placeholder_vocabulary, $edit);
      drupal_set_message(t("Applying imported settings onto vocabulary:%vid.", array(
        '%vid' => $vid,
      )));
    }
    else {

      // TODO valid vocab found, but is it REALLY the same?
      drupal_set_message(t("\n        Declared vocab ID:%data_vid DOES already exist.\n        Terms will be imported there, but <a href='!settings_url'>existing vocabulary settings</a>\n        will not be modified.", array(
        '%data_vid' => $edit['vid'],
        '!settings_url' => url('admin/content/taxonomy/edit/vocabulary/' . $edit['vid']),
      )));

      // TODO sanity check to avoid inadvertant overwrites.
      // No change
      $vid = $placeholder_vocabulary['vid'];
    }
  }
  else {

    // Input did not define a vid. Make a new vocab with these new values
    // Should I try to match on *name* to find an earlier version?
    $placeholder_vocabulary = (array) _taxonomy_xml_get_vocabulary_placeholder($edit['name']);
    $vid = $placeholder_vocabulary['vid'];

    // Use other attributes defined in the source file (all except the vid)
    unset($edit['vid']);
    $placeholder_vocabulary = array_merge($placeholder_vocabulary, $edit);
    drupal_set_message(t("Applying imported settings onto vocabulary:%vid.", array(
      '%vid' => $vid,
    )));
  }

  // Ensure the merged attributes are up to date.
  taxonomy_save_vocabulary($placeholder_vocabulary);
  return $placeholder_vocabulary['vid'];
}