You are here

function taxonomy_feeds_atom_rdf_map_alter in Feeds Atom 6

Implementation of hook_feeds_atom_rdf_map_alter().

We implement this hook on behalf of the taxonomy module so that we can lazy-create terms as needed. We only do so if the vocabulary the term is in already exists. If the vocabulary doesn't exist, the incoming term is ignored.

Parameters

$target_item: The node that we are creating/editing.

$source_item: The parsed data array from the feed.

Return value

unknown_type

File

./feeds_atom.module, line 64
Contains the main functionality for feeds_atom.

Code

function taxonomy_feeds_atom_rdf_map_alter(&$target_item, $source_item) {
  if (empty($source_item['rdf']['taxonomy']) || !is_array($source_item['rdf']['taxonomy'])) {

    // Nothing to process
    return;
  }

  // Zero out the taxonomy data that is already there, as it will break if we
  // try to save the node with it there.
  $target_item->taxonomy = array();

  // Process the term data.
  foreach ($source_item['rdf']['taxonomy'] as $source_term) {

    // Add new terms if they don't exist
    $vid = NULL;
    $tid = NULL;

    // Find the vocabulary.
    if (!empty($source_term['vocabulary'])) {

      // Features intergration: Features stores vocabulary machine name's in
      // module key prepended with features_
      if (module_exists('features')) {
        $machine_name = !empty($source_term['machine']) ? $source_term['machine'] : $source_term['vocabulary'];

        // Add in features_ if doesn't exist
        if (strpos($machine_name, 'features_') !== 0) {
          $machine_name = 'features_' . $machine_name;
        }
        $vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module = '%s'", strtolower($machine_name)));
      }

      // Fallback to name matching if vid not found above.
      if (empty($vid)) {
        $vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE lower(name) = '%s'", strtolower($source_term['vocabulary'])));
      }
    }

    // See if the term already exists
    foreach (taxonomy_get_term_by_name($source_term['title']) as $term) {

      // if VID was not found but name matches or vid is term's vocabulary.
      if (empty($vid) || $vid == $term->vid) {
        $tid = $term->tid;
      }
    }

    // Create the new term if doesn't exist and know vocabulary
    if (empty($tid) && !empty($vid)) {
      $new_term = array(
        'vid' => $vid,
        'name' => $source_term['title'],
        'description' => $source_term['description'],
      );
      taxonomy_save_term($new_term);
      $tid = $new_term['tid'];
    }

    // Apply the term to the target node.
    if (!empty($tid)) {
      $term = taxonomy_get_term($tid, TRUE);
      $target_item->taxonomy[$term->tid] = $term;
    }
  }
}