You are here

function taxonomy_xml_get_term_by_guid in Taxonomy import/export via XML 7

Same name and namespace in other branches
  1. 6.2 taxonomy_xml.module \taxonomy_xml_get_term_by_guid()

Special lookup for terms if they are saved with a URI or GUID.

Very specific to certain ways of serializing terms, REQUIRES taxonomy_enhancer and a field called field_guid OR rdf.module and an owl:sameAs relation

Return value

bool|object

3 calls to taxonomy_xml_get_term_by_guid()
taxonomy_xml_add_all_children_to_queue in ./taxonomy_xml.process.inc
Queue up an import action.
taxonomy_xml_rdf_make_term in formats/rdf_format.inc
Create the placeholder and fill in the values for this term.
taxonomy_xml_set_term_relations in ./taxonomy_xml.process.inc
Given a list of terms, set the related-terms and structure, and save again.

File

./taxonomy_xml.module, line 458
Make it possible to import and export taxonomies as XML documents.

Code

function taxonomy_xml_get_term_by_guid($guid, $vid = NULL) {
  if (!$guid) {
    return NULL;
  }

  // This is probably not the best way, but I spent two hours trying to get
  // through fieldapi and things and couldn't find a clue.
  // TODO - should use entity_metadata_wrapper. Did not know that existed
  // at the time,
  static $guid_field_id;
  static $guid_field_info;
  if (!$guid_field_id) {
    $guid_field_info = field_info_field(TAXONOMY_XML_IDENTIFIER);
    $guid_field_id = $guid_field_info['id'];
  }
  if ($guid_field_id) {

    // Find the thing(s)
    // of type taxonomy_term
    // that has a field (guid)
    // with a value of (desired guid).
    $id_lookup_query = new EntityFieldQuery();
    $id_lookup_query
      ->entityCondition('entity_type', 'taxonomy_term')
      ->fieldCondition($guid_field_info, 'value', $guid);
    $field_lookup = $id_lookup_query
      ->execute();

    // $field_lookup is now an array of entities (or at least entity ids)
    // that match the lookup
    // array('taxonomy_term' => array(
    //   9 => object('tid' => 9, 'vocabulary_machine_name' => 'food'),
    // ))
    // Load that entity (assume only one valid match).
    if (!empty($field_lookup['taxonomy_term'])) {
      $term_entity = array_pop($field_lookup['taxonomy_term']);
      $term = taxonomy_term_load($term_entity->tid);
      $strings = array(
        '!guid' => $guid,
        '!term_name' => $term->name,
        '!tid' => $term_entity->tid,
      );
      watchdog('taxonomy_xml', "Found !guid = !term_name !tid", $strings, WATCHDOG_DEBUG);
      return $term;
    }
  }

  // dpm("Couldn't find a known item with a URI = $guid ");

  /* @todo D7
     if (module_exists('rdf')) {
     // Lookup the RDF values to see if this term has any 'sameAs' matches
     // Note the canononic or remote URL will be on the right of the triple
     $lookups = rdf_normalize(rdf_query(NULL, 'owl:sameAs', $guid));
     // Normalized data is indexed by [subject][predicate][] = object
     // We are looking for the value on the left - the subject.

     $local_term_paths = array_keys($lookups);
     if (count($local_term_paths) > 1) {
     // Not sure how it happened, but we may get more than one result.
     watchdog('taxonomy_xml',
     "This is confusing, apparently there are more than one local match
     that are sameAs '%guid' .
     <pre>!lookups</pre>
     Possibly the same concept in a different vocabulary.
     I'm only going to deal with one of them (the one in the current vocab - if any).",
     array('%guid' => $guid, '!lookups' => print_r(array_keys($lookups), 1)),
     WATCHDOG_DEBUG
     );
     }

     $term_base_url = url('taxonomy/term/', array('absolute' => TRUE));
     // There is (almost certainly) only one value all down this tree,
     // but I don't know the keys, just iterate.
     foreach ((array) $lookups as $subject => $predicate_array) {
     foreach ($predicate_array as $predicate => $value_array) {
     foreach ($value_array as $i => $found_value) {
     // Stored in the database was a URI, backtrack to the term ID.

     // Is my term sameAs the URI?
     if (strstr($subject, $term_base_url)) {
     // yep, it's a local term URL
     $tid = intval(drupal_substr($subject, drupal_strlen($term_base_url)));
     if ($found_term = taxonomy_term_load($tid)) {
     watchdog('taxonomy_xml',
     'Found <a href="!term_link">an existing term %term_name</a>
     in vocab %vid when looking for %guid',
     array(
     '%guid'       => $guid,
     '%term_name' => $found_term->name,
     '%vid'       => $found_term->vid,
     '!term_link' => url('taxonomy/term/' . $found_term->tid),
     ),
     WATCHDOG_DEBUG
     );

     // Now I found the term, check it's the right vocab (in case of multiples)
     if ($vid && $found_term->vid == $vid) {
     $term = $found_term;
     }

     }
     } // found a string match
     } // loop all result values
     } // loop all (1) predicates
     // Keep looking if that didn't work
     // (highly rare there's more than one)
     } // loop all (1) result

     } // RDF.module lookup
     */
  return isset($term) ? $term : NULL;
}