6 * Mapper that exposes a node's taxonomy vocabularies as mapping targets.
10 * Implements hook_feeds_parser_sources_alter().
12 function taxonomy_feeds_parser_sources_alter(&$sources, $content_type) {
13 if (!empty($content_type)) {
14 foreach (taxonomy_get_vocabularies($content_type) as
$vocabulary) {
15 $sources['parent:taxonomy:'.
taxonomy_vocabulary_id($vocabulary)] = array(
16 'name' => t('Feed node: Taxonomy: @vocabulary', array('@vocabulary' => $vocabulary->name
)),
17 'description' => t('Taxonomy terms from feed node in given vocabulary.'),
18 'callback' => 'taxonomy_feeds_get_source',
25 * Callback, returns taxonomy from feed node.
27 function taxonomy_feeds_get_source(FeedsImportBatch
$batch, $key) {
28 if ($node = $batch->feedNode()) {
29 $terms = taxonomy_node_get_terms($node);
30 $vocabulary = taxonomy_get_vocabulary(str_replace('parent:taxonomy:', '', $key));
32 foreach ($terms as
$tid => $term) {
33 if ($term->vid
== $vocabulary->vid
) {
34 $result[] = new
FeedsTermElement($term);
42 * Implements hook_feeds_node_processor_targets_alter().
44 * @see FeedsNodeProcessor::getMappingTargets().
46 function taxonomy_feeds_node_processor_targets_alter(&$targets, $content_type) {
47 foreach (taxonomy_get_vocabularies($content_type) as
$vocabulary) {
48 $description = t('The @name vocabulary of the node. If this is a "Tags" vocabulary, any new terms will be created on import. Otherwise only existing terms will be used. If this is not a "Tags" vocabulary and not a "Multiple select" vocabulary, only the first available term will be created. See !settings.', array('@name' => $vocabulary->name
, '!settings' => l(t('vocabulary settings'), 'admin/content/taxonomy/edit/vocabulary/'.
$vocabulary->vid
, array('query' => 'destination='.
$_GET['q']))));
50 $targets['taxonomy:'.
taxonomy_vocabulary_id($vocabulary)] = array(
51 'name' => "Taxonomy: ".
$vocabulary->name
,
52 'callback' => 'taxonomy_feeds_set_target',
53 'description' => $description,
54 'real_target' => 'taxonomy',
60 * Callback for mapping. Here is where the actual mapping happens.
63 * Reference to the node object we are working on.
66 * A key as added to the $targets array by
67 * taxonomy_feeds_node_processor_targets_alter().
70 * Given terms as array.
72 * Add the given terms to the node object so the taxonomy module can add them
75 function taxonomy_feeds_set_target(&$node, $key, $terms) {
77 // Return if there are no terms.
82 // Load target vocabulary to check, if it has the "tags" flag.
83 $vocabulary = taxonomy_get_vocabulary(str_replace('taxonomy:', '', $key));
85 // Cast a given single string to an array so we can use it.
86 if (!is_array($terms)) {
87 $terms = array($terms);
90 if ($vocabulary->tags
) {
91 // Simply add a comma separated list to the node for a "tags" vocabulary.
92 $terms = array_merge($terms, drupal_explode_tags($node->taxonomy
['tags'][$vocabulary->vid
]));
93 $node->taxonomy
['tags'][$vocabulary->vid
] = implode(',', $terms);
96 foreach ($terms as
$term) {
97 if ($term instanceof FeedsTermElement
) {
98 $node->taxonomy
[$term->tid
] = (object)$term;
100 // Check if a term already exists.
101 elseif ($terms_found = taxonomy_get_term_by_name_vid($term, $vocabulary->vid
)) {
102 // If any terms are found add them to the node's taxonomy by found tid.
103 foreach ($terms_found AS
$term_found) {
104 $node->taxonomy
[$term_found->tid
] = $term_found;
105 if (!$vocabulary->multiple
) {
110 // If the vocab is not for multiple tags break after the first hit.
111 if (!$vocabulary->multiple
) {
119 * Try to map a string to an existing term by name and vocabulary id.
121 * Provides a case-insensitive and trimmed mapping, to maximize the likelihood
122 * of a successful match limited by a vocabulary id.
125 * Name of the term to search for.
128 * The vocabulary's ID.
131 * An array of matching term objects.
133 function taxonomy_get_term_by_name_vid($name, $vid) {
134 $db_result = db_query(db_rewrite_sql("SELECT t.tid, t.name FROM {term_data} t WHERE LOWER(t.name) = LOWER('%s') AND t.vid = %d", 't', 'tid'), trim($name), $vid);
136 while ($term = db_fetch_object($db_result)) {
143 * Look up a vocabulary by vid or module name.
146 * A module name or a numeric vocabulary id.
149 * An object of type stdClass that represents a vocabulary.
151 function taxonomy_get_vocabulary($id) {
152 static
$vocabularies;
153 if (!isset($vocabularies[$id])) {
154 foreach (taxonomy_get_vocabularies() as
$vocabulary) {
155 if ($vocabulary->vid
== $id) {
156 $vocabularies[$id] = $vocabulary;
159 elseif ($vocabulary->module
== $id) {
160 $vocabularies[$id] = $vocabulary;
165 return $vocabularies[$id];
169 * Return the vocabulary identifier, the vocabulary's vid or module.
172 * Vocabulary's module name if it is a features vocabulary (= exportable),
173 * vocabulary's vid otherwise.
175 function taxonomy_vocabulary_id($vocabulary) {
176 if (strpos($vocabulary->module
, 'features_') === 0) {
177 return $vocabulary->module
;
179 return $vocabulary->vid
;