You are here

function taxonomy_xml_set_term_relations in Taxonomy import/export via XML 5.2

Same name and namespace in other branches
  1. 5 taxonomy_xml.module \taxonomy_xml_set_term_relations()
  2. 6.2 taxonomy_xml.module \taxonomy_xml_set_term_relations()
  3. 6 taxonomy_xml.module \taxonomy_xml_set_term_relations()
  4. 7 taxonomy_xml.process.inc \taxonomy_xml_set_term_relations()

Given a list of terms, set the related-terms and structure, and save again

Helper function for bulk processes.

The terms are currently indexed by either URI or name. The reference arrays refer to either the URI or name. Scan the current array for the objects (terms) being linked to.

Input would look (in part) like this:

$terms = array( '#123' => ( 'name' => 'hotels', 'tid' => 23, 'predicates' => ( 'See Also' => ['#135', 'camping'] 'Broader Term' => ['accomodation'] ) ) '#135' => ( 'name' => 'motels', 'tid' => 35 ) '#145' => ( 'name' => 'camping', 'tid' => 37 ) 'accomodation' => ( 'name' => 'accomodation', 'tid' => 11 ) )

The process will read the 'predicates', figure out what they mean, figure out which other term is being referenced, and create properties on the term object.

And will return the term objects with appropriate Drupal attributes

'#123' => ( 'name' => 'hotels', 'nid' => 23, 'parent' => 11, 'relations' => array(35, 37), )

Note that the key need have no relation with the nid, and may be a full string, which will work just as well. The above shows an example of both, although that would be rare in the one import.

Relationships cannot be created if the target term is not included in the $terms list itself. If we are adding to an existing vocab, doing a partial merge, the target terms should have already been retrieved from the database and included in the complete list.

Parameters

$terms an indexed array of existing taxonomy term objects, possibly: referring to each other by id.

2 calls to taxonomy_xml_set_term_relations()
taxonomy_xml_csv_parse in ./csv_format.inc
Scan the input CSV file and create a taxonomy structure out of it.
taxonomy_xml_rdf_parse in ./rdf_format.inc
Read in RDF taxonomies and vocabularies. Create vocabs and terms as needed.

File

./taxonomy_xml.module, line 556
taxonomy_xml.module This module makes it possible to import and export taxonomies as XML documents.

Code

function taxonomy_xml_set_term_relations(&$terms) {
  drupal_set_message(t("Now connecting all known term relations and hierarchy links"));
  foreach ($terms as $uri => $term) {
    $name = $term->name;

    // Go through all and add relationships
    // The linking earlier may have given us some duplicates if the source had redundant info, so filter for uniques
    if (isset($term->predicates[TAXONOMY_XML_PARENT]) && is_array($term->predicates[TAXONOMY_XML_PARENT])) {
      foreach (array_unique($term->predicates[TAXONOMY_XML_PARENT]) as $key => $termname) {
        if ($termname) {
          $parent = $terms[$termname];
          if ($termname == $uri) {
            drupal_set_message(t("Not setting %name as a child of itself", array(
              '%name' => $term->name,
            )));
            continue;
          }
          if ($parent && isset($parent->tid)) {
            drupal_set_message(t("!name # %tid is a child of !parent # %ptid", array(
              '!name' => l($term->name, 'admin/content/taxonomy/edit/term/' . $term->tid),
              '%tid' => $term->tid,
              '!parent' => l($parent->name, 'admin/content/taxonomy/edit/term/' . $parent->tid),
              '%ptid' => $parent->tid,
            )));
            $term->parent[$parent->tid] = $parent->tid;
          }
          else {
            drupal_set_message(t("Couldn't find the parent called %termname for %name # %tid", array(
              '%termname' => $termname,
              '%name' => $name,
              '%tid' => $term->tid,
            )));
          }
        }
      }
    }

    #else{drupal_set_message(" $name ". $term->tid ." has no parent term");}
    if (isset($term->predicates[TAXONOMY_XML_RELATED]) && is_array($term->predicates[TAXONOMY_XML_RELATED])) {
      foreach (array_unique($term->predicates[TAXONOMY_XML_RELATED]) as $key => $termname) {
        if ($termname) {
          $related = $terms[$termname];
          if ($related) {
            $term->relations[] = $related->tid;
            drupal_set_message("Term " . $term->name . " " . $term->tid . " is related to {$related->name} " . $related->tid);
          }
          else {
            drupal_set_message(t("Couldn't find the term called '%termname' to link to '%name' as being related. This relationship will be discarded. ", array(
              '%name' => $name,
              '%termname' => $termname,
              '%debug' => print_r(array_keys($terms), 1),
            )));

            # dpm(array('available terms' => array_keys($terms)));
          }
        }
      }
    }
    if (!empty($term->synonyms_array)) {
      $term->synonyms = join("\n", array_unique($term->synonyms_array));
    }

    #     dpm(array('Saving' => $term));
    $save_term = (array) $term;
    taxonomy_save_term($save_term);
  }
}