| Commit | Line | Data |
|---|---|---|
| 6f438a92 | 1 | <?php |
| 6f438a92 AB |
2 | |
| 3 | /** | |
| 4 | * @file | |
| 5 | * Mapper that exposes a node's taxonomy vocabularies as mapping targets. | |
| 6 | */ | |
| 7 | ||
| 8 | /** | |
| 9f20738f | 9 | * Implements hook_feeds_parser_sources_alter(). |
| 68613918 AB |
10 | */ |
| 11 | function taxonomy_feeds_parser_sources_alter(&$sources, $content_type) { | |
| 12 | if (!empty($content_type)) { | |
| 3e9ba338 | 13 | foreach (taxonomy_get_vocabularies($content_type) as $vocabulary) { |
| 725e6aeb | 14 | $sources['parent:taxonomy:'. $vocabulary->machine_name] = array( |
| 3e9ba338 | 15 | 'name' => t('Feed node: Taxonomy: @vocabulary', array('@vocabulary' => $vocabulary->name)), |
| 68613918 AB |
16 | 'description' => t('Taxonomy terms from feed node in given vocabulary.'), |
| 17 | 'callback' => 'taxonomy_feeds_get_source', | |
| 18 | ); | |
| 19 | } | |
| 20 | } | |
| 21 | } | |
| 22 | ||
| 23 | /** | |
| 24 | * Callback, returns taxonomy from feed node. | |
| 25 | */ | |
| 26 | function taxonomy_feeds_get_source(FeedsImportBatch $batch, $key) { | |
| 27 | if ($node = $batch->feedNode()) { | |
| 28 | $terms = taxonomy_node_get_terms($node); | |
| 725e6aeb AB |
29 | $vocabularies = taxonomy_vocabulary_load_multiple(array(), array('machine_name' => str_replace('parent:taxonomy:', '', $key))); |
| 30 | $vocabulary = array_shift($vocabularies); | |
| 68613918 AB |
31 | $result = array(); |
| 32 | foreach ($terms as $tid => $term) { | |
| a039bfa8 | 33 | if ($term->vid == $vocabulary->vid) { |
| 68613918 AB |
34 | $result[] = new FeedsTermElement($term); |
| 35 | } | |
| 36 | } | |
| 37 | return $result; | |
| 38 | } | |
| 39 | } | |
| 40 | ||
| 41 | /** | |
| 725e6aeb | 42 | * Implements hook_feeds_processor_targets_alter(). |
| 6f438a92 | 43 | */ |
| 725e6aeb AB |
44 | function taxonomy_feeds_processor_targets_alter(&$targets, $entity_type, $content_type) { |
| 45 | foreach (field_info_instances($entity_type, $content_type) as $name => $instance) { | |
| 46 | $info = field_info_field($name); | |
| 47 | if ($info['type'] == 'taxonomy_term_reference') { | |
| 48 | $targets[$name] = array( | |
| 49 | 'name' => $instance['label'], | |
| 50 | 'callback' => 'taxonomy_feeds_set_target', | |
| 51 | 'description' => t('The @label field of the node.', array('@label' => $instance['label'])), | |
| 52 | ); | |
| 53 | } | |
| 6f438a92 AB |
54 | } |
| 55 | } | |
| 56 | ||
| 57 | /** | |
| 58 | * Callback for mapping. Here is where the actual mapping happens. | |
| 59 | * | |
| 725e6aeb | 60 | * @todo Do not create new terms for non-autotag fields. |
| 6f438a92 | 61 | */ |
| 725e6aeb | 62 | function taxonomy_feeds_set_target($entity, $target, $terms) { |
| 6f438a92 AB |
63 | if (empty($terms)) { |
| 64 | return; | |
| 65 | } | |
| 66 | ||
| 725e6aeb | 67 | // Handle non-multiple values. |
| 6f438a92 AB |
68 | if (!is_array($terms)) { |
| 69 | $terms = array($terms); | |
| 70 | } | |
| 71 | ||
| 725e6aeb AB |
72 | $info = field_info_field($target); |
| 73 | ||
| 74 | // See http://drupal.org/node/881530 | |
| 75 | if (isset($info['settings']['allowed_values'][0]['vocabulary'])) { | |
| 76 | $vocabulary = taxonomy_vocabulary_machine_name_load($info['settings']['allowed_values'][0]['vocabulary']); | |
| 6f438a92 AB |
77 | } |
| 78 | else { | |
| 725e6aeb | 79 | $vocabulary = taxonomy_vocabulary_load($info['settings']['allowed_values'][0]['vid']); |
| 6f438a92 | 80 | } |
| 6f438a92 | 81 | |
| 725e6aeb AB |
82 | $i = 0; |
| 83 | $entity->$target = isset($entity->$target) ? $entity->$target : array(); | |
| 84 | foreach ($terms as $term) { | |
| 85 | $tid = 0; | |
| 86 | if ($term instanceof FeedsTermElement) { | |
| 87 | $tid = $term->tid; | |
| 88 | } | |
| 89 | elseif (is_numeric($term)) { | |
| 90 | $tid = $term; | |
| 91 | } | |
| 92 | elseif (is_string($term)) { | |
| 93 | $tid = taxonomy_term_check_term($term, $vocabulary->vid); | |
| 94 | } | |
| 95 | if ($tid) { | |
| 96 | $entity->{$target}['und'][$i]['tid'] = $tid; | |
| 97 | } | |
| a039bfa8 | 98 | |
| 725e6aeb AB |
99 | if ($info['cardinality'] == 1) { |
| 100 | break; | |
| a039bfa8 | 101 | } |
| 725e6aeb | 102 | $i++; |
| a039bfa8 | 103 | } |
| a039bfa8 AB |
104 | } |
| 105 | ||
| 106 | /** | |
| 725e6aeb AB |
107 | * Checks whether a term identified by name and vocabulary exists. Creates a |
| 108 | * new term if it does not exist. | |
| 109 | * | |
| 110 | * @param $name | |
| 111 | * A term name. | |
| 112 | * @param $vid | |
| 113 | * A vocabulary id. | |
| a039bfa8 AB |
114 | * |
| 115 | * @return | |
| 725e6aeb | 116 | * A term id. |
| a039bfa8 | 117 | */ |
| 725e6aeb AB |
118 | function taxonomy_term_check_term($name, $vid) { |
| 119 | $terms = taxonomy_term_load_multiple(array(), array('name' => $name, 'vid' => $vid)); | |
| 120 | if (empty($terms)) { | |
| 121 | $term = new stdClass(); | |
| 122 | $term->name = $name; | |
| 123 | $term->vid = $vid; | |
| 124 | taxonomy_term_save($term); | |
| 125 | return $term->tid; | |
| a039bfa8 | 126 | } |
| 725e6aeb AB |
127 | $term = reset($terms); |
| 128 | return $term->tid; | |
| a039bfa8 | 129 | } |