1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* On behalf implementation of Feeds mapping API for taxonomy.module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Search by term name.
|
10
|
*/
|
11
|
define('FEEDS_TAXONOMY_SEARCH_TERM_NAME', 0);
|
12
|
|
13
|
/**
|
14
|
* Search by term id.
|
15
|
*/
|
16
|
define('FEEDS_TAXONOMY_SEARCH_TERM_ID', 1);
|
17
|
|
18
|
/**
|
19
|
* Search by GUID.
|
20
|
*/
|
21
|
define('FEEDS_TAXONOMY_SEARCH_TERM_GUID', 2);
|
22
|
|
23
|
/**
|
24
|
* Implements hook_feeds_parser_sources_alter().
|
25
|
*/
|
26
|
function taxonomy_feeds_parser_sources_alter(array &$sources, $content_type) {
|
27
|
if (!empty($content_type)) {
|
28
|
foreach (taxonomy_get_vocabularies($content_type) as $vocabulary) {
|
29
|
$sources['parent:taxonomy:' . $vocabulary->machine_name] = array(
|
30
|
'name' => t('Feed node: Taxonomy: @vocabulary', array('@vocabulary' => $vocabulary->name)),
|
31
|
'description' => t('Taxonomy terms from feed node in given vocabulary.'),
|
32
|
'callback' => 'taxonomy_feeds_get_source',
|
33
|
);
|
34
|
}
|
35
|
}
|
36
|
}
|
37
|
|
38
|
/**
|
39
|
* Callback, returns taxonomy from feed node.
|
40
|
*/
|
41
|
function taxonomy_feeds_get_source(FeedsSource $source, FeedsParserResult $result, $key) {
|
42
|
if ($node = node_load($source->feed_nid)) {
|
43
|
$terms = taxonomy_feeds_node_get_terms($node);
|
44
|
$vocabularies = taxonomy_vocabulary_load_multiple(array(), array('machine_name' => str_replace('parent:taxonomy:', '', $key)));
|
45
|
$vocabulary = array_shift($vocabularies);
|
46
|
$result = array();
|
47
|
foreach ($terms as $tid => $term) {
|
48
|
if ($term->vid == $vocabulary->vid) {
|
49
|
$result[] = new FeedsTermElement($term);
|
50
|
}
|
51
|
}
|
52
|
|
53
|
return $result;
|
54
|
}
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* Implements hook_feeds_processor_targets().
|
59
|
*/
|
60
|
function taxonomy_feeds_processor_targets($entity_type, $bundle_name) {
|
61
|
$targets = array();
|
62
|
|
63
|
foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
|
64
|
$info = field_info_field($name);
|
65
|
if ($info['type'] == 'taxonomy_term_reference') {
|
66
|
$targets[$name] = array(
|
67
|
'name' => check_plain($instance['label']),
|
68
|
'callback' => 'taxonomy_feeds_set_target',
|
69
|
'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
|
70
|
'summary_callbacks' => array('taxonomy_feeds_summary_callback'),
|
71
|
'form_callbacks' => array('taxonomy_feeds_form_callback'),
|
72
|
);
|
73
|
}
|
74
|
}
|
75
|
if ($entity_type == 'taxonomy_term') {
|
76
|
$targets['tid']['name'] = t('Term id');
|
77
|
$targets['tid']['description'] = t('The tid of the taxonomy term. NOTE: use this feature with care, node ids are usually assigned by Drupal.');
|
78
|
unset($targets['vocabulary']);
|
79
|
}
|
80
|
|
81
|
return $targets;
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
* Callback for mapping taxonomy terms.
|
86
|
*/
|
87
|
function taxonomy_feeds_set_target(FeedsSource $source, $entity, $target, array $terms, array $mapping) {
|
88
|
$language = $mapping['language'];
|
89
|
|
90
|
// Add in default values.
|
91
|
$mapping += array(
|
92
|
'term_search' => FEEDS_TAXONOMY_SEARCH_TERM_NAME,
|
93
|
'autocreate' => FALSE,
|
94
|
);
|
95
|
|
96
|
$info = field_info_field($target);
|
97
|
|
98
|
$cache = &drupal_static(__FUNCTION__);
|
99
|
if (!isset($cache['allowed_values'][$target])) {
|
100
|
$cache['allowed_values'][$target] = taxonomy_allowed_values($info);
|
101
|
}
|
102
|
|
103
|
if (!isset($cache['allowed_vocabularies'][$target])) {
|
104
|
foreach ($info['settings']['allowed_values'] as $tree) {
|
105
|
if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
|
106
|
$vid = $vocabulary->vid;
|
107
|
|
108
|
$cache['allowed_vocabularies'][$target][$vid] = $vocabulary->machine_name;
|
109
|
$cache['vocabulary_targets'][$vid][] = $target;
|
110
|
}
|
111
|
}
|
112
|
}
|
113
|
|
114
|
// Some kind of configuration issue. Perhaps the vocabulary was deleted.
|
115
|
// Nothing we can do about it.
|
116
|
if (empty($cache['allowed_vocabularies'][$target])) {
|
117
|
return;
|
118
|
}
|
119
|
|
120
|
$query = new EntityFieldQuery();
|
121
|
$query->entityCondition('entity_type', 'taxonomy_term')
|
122
|
->entityCondition('bundle', $cache['allowed_vocabularies'][$target])
|
123
|
->range(0, 1);
|
124
|
|
125
|
$field = isset($entity->$target) ? $entity->$target : array($language => array());
|
126
|
|
127
|
if (!isset($field[$language])) {
|
128
|
$field[$language] = array();
|
129
|
}
|
130
|
|
131
|
// Allow for multiple mappings to the same target.
|
132
|
$delta = count($field[$language]);
|
133
|
|
134
|
// Iterate over all values.
|
135
|
foreach ($terms as $term) {
|
136
|
|
137
|
if ($info['cardinality'] == $delta) {
|
138
|
break;
|
139
|
}
|
140
|
|
141
|
$tid = FALSE;
|
142
|
|
143
|
// FeedsTermElement already is a term.
|
144
|
if ($term instanceof FeedsTermElement) {
|
145
|
$tid = $term->tid;
|
146
|
}
|
147
|
else {
|
148
|
switch ($mapping['term_search']) {
|
149
|
|
150
|
// Lookup by name.
|
151
|
case FEEDS_TAXONOMY_SEARCH_TERM_NAME:
|
152
|
$term = trim($term);
|
153
|
$name_query = clone $query;
|
154
|
if (strlen($term) && $tids = $name_query->propertyCondition('name', $term)->execute()) {
|
155
|
|
156
|
// Find the first allowed term.
|
157
|
foreach ($tids['taxonomy_term'] as $possible_term) {
|
158
|
if (isset($cache['allowed_values'][$target][$possible_term->tid])) {
|
159
|
$tid = $possible_term->tid;
|
160
|
break;
|
161
|
}
|
162
|
}
|
163
|
}
|
164
|
elseif ($mapping['autocreate'] && strlen($term)) {
|
165
|
$term_vid = key($cache['allowed_vocabularies'][$target]);
|
166
|
$term = (object) array(
|
167
|
'name' => drupal_substr($term, 0, 255),
|
168
|
'vid' => $term_vid,
|
169
|
'vocabulary_machine_name' => reset($cache['allowed_vocabularies'][$target]),
|
170
|
);
|
171
|
// Set language if the taxonomy is multilingual.
|
172
|
if ($language !== LANGUAGE_NONE) {
|
173
|
$info = entity_get_info('taxonomy_term');
|
174
|
if (!empty($info['entity keys']['language'])) {
|
175
|
$term->{$info['entity keys']['language']} = $language;
|
176
|
}
|
177
|
}
|
178
|
taxonomy_term_save($term);
|
179
|
$tid = $term->tid;
|
180
|
|
181
|
// Add to the list of allowed values.
|
182
|
$cache['allowed_values'][$target][$tid] = $term->name;
|
183
|
|
184
|
// Invalidate caches for other fields targeting the same vocabulary.
|
185
|
foreach ($cache['vocabulary_targets'][$term_vid] as $clear_target) {
|
186
|
if ($clear_target !== $target) {
|
187
|
unset($cache['allowed_values'][$clear_target]);
|
188
|
}
|
189
|
}
|
190
|
}
|
191
|
break;
|
192
|
|
193
|
// Lookup by tid.
|
194
|
case FEEDS_TAXONOMY_SEARCH_TERM_ID:
|
195
|
if (is_numeric($term)) {
|
196
|
$tid = (int) $term;
|
197
|
}
|
198
|
break;
|
199
|
|
200
|
// Lookup by GUID.
|
201
|
case FEEDS_TAXONOMY_SEARCH_TERM_GUID:
|
202
|
$tid = taxonomy_feeds_term_lookup_term_by_guid($term);
|
203
|
break;
|
204
|
}
|
205
|
}
|
206
|
|
207
|
if ($tid && isset($cache['allowed_values'][$target][$tid])) {
|
208
|
$field[$language][] = array('tid' => $tid);
|
209
|
$delta++;
|
210
|
}
|
211
|
}
|
212
|
|
213
|
$entity->$target = $field;
|
214
|
}
|
215
|
|
216
|
/**
|
217
|
* Finds all terms associated with the given node, within one vocabulary.
|
218
|
*/
|
219
|
function taxonomy_feeds_node_get_terms($node, $key = 'tid') {
|
220
|
$terms = &drupal_static(__FUNCTION__);
|
221
|
|
222
|
if (!isset($terms[$node->nid][$key])) {
|
223
|
// Get tids from all taxonomy_term_reference fields.
|
224
|
$tids = array();
|
225
|
$fields = field_info_fields();
|
226
|
foreach ($fields as $field_name => $field) {
|
227
|
if ($field['type'] == 'taxonomy_term_reference' && field_info_instance('node', $field_name, $node->type)) {
|
228
|
if (($items = field_get_items('node', $node, $field_name)) && is_array($items)) {
|
229
|
$tids = array_merge($tids, array_map('_taxonomy_feeds_extract_tid', $items));
|
230
|
}
|
231
|
}
|
232
|
}
|
233
|
|
234
|
// Load terms and cache them in static var.
|
235
|
$curr_terms = taxonomy_term_load_multiple($tids);
|
236
|
$terms[$node->nid][$key] = array();
|
237
|
foreach ($curr_terms as $term) {
|
238
|
$terms[$node->nid][$key][$term->$key] = $term;
|
239
|
}
|
240
|
}
|
241
|
return $terms[$node->nid][$key];
|
242
|
}
|
243
|
|
244
|
/**
|
245
|
* Extracts tid from array item returned by field_get_items().
|
246
|
*
|
247
|
* @param array $item
|
248
|
* Tid information in the form of a single element array
|
249
|
* (key == 'tid', value == tid we're looking for)
|
250
|
*
|
251
|
* @return int
|
252
|
* Term id extracted from $item.
|
253
|
*
|
254
|
* @see taxonomy_feeds_node_get_terms()
|
255
|
* @see field_get_items()
|
256
|
*/
|
257
|
function _taxonomy_feeds_extract_tid($item) {
|
258
|
return $item['tid'];
|
259
|
}
|
260
|
|
261
|
/**
|
262
|
* Looks up a term by GUID, assumes SQL storage backend.
|
263
|
*
|
264
|
* @param string $guid
|
265
|
* The Feeds GUID to compare against.
|
266
|
*
|
267
|
* @return int|false
|
268
|
* The term id, or FALSE if one was not found.
|
269
|
*/
|
270
|
function taxonomy_feeds_term_lookup_term_by_guid($guid) {
|
271
|
return db_select('feeds_item')
|
272
|
->fields('feeds_item', array('entity_id'))
|
273
|
->condition('entity_type', 'taxonomy_term')
|
274
|
->condition('guid', $guid)
|
275
|
->execute()
|
276
|
->fetchField();
|
277
|
}
|
278
|
|
279
|
/**
|
280
|
* Mapping configuration summary for taxonomy.module.
|
281
|
*/
|
282
|
function taxonomy_feeds_summary_callback(array $mapping, $target, array $form, array $form_state) {
|
283
|
$options = _taxonomy_feeds_form_callback_options();
|
284
|
if (empty($mapping['term_search'])) {
|
285
|
return t('Search taxonomy terms by: <strong>@search</strong>', array('@search' => $options[FEEDS_TAXONOMY_SEARCH_TERM_NAME]));
|
286
|
}
|
287
|
return t('Search taxonomy terms by: <strong>@search</strong>', array('@search' => $options[$mapping['term_search']]));
|
288
|
}
|
289
|
|
290
|
/**
|
291
|
* Settings form callback.
|
292
|
*/
|
293
|
function taxonomy_feeds_form_callback(array $mapping, $target, array $form, array $form_state) {
|
294
|
return array(
|
295
|
'term_search' => array(
|
296
|
'#type' => 'select',
|
297
|
'#title' => t('Search taxonomy terms by'),
|
298
|
'#options' => _taxonomy_feeds_form_callback_options(),
|
299
|
'#default_value' => !empty($mapping['term_search']) ? $mapping['term_search'] : FEEDS_TAXONOMY_SEARCH_TERM_NAME,
|
300
|
),
|
301
|
'autocreate' => array(
|
302
|
'#type' => 'checkbox',
|
303
|
'#title' => t('Auto create'),
|
304
|
'#description' => t("Create the term if it doesn't exist."),
|
305
|
'#default_value' => !empty($mapping['autocreate']) ? $mapping['autocreate'] : 0,
|
306
|
'#states' => array(
|
307
|
'visible' => array(
|
308
|
':input[name$="[settings][term_search]"]' => array('value' => FEEDS_TAXONOMY_SEARCH_TERM_NAME),
|
309
|
),
|
310
|
),
|
311
|
),
|
312
|
);
|
313
|
}
|
314
|
|
315
|
/**
|
316
|
* Returns the list of available term search methods.
|
317
|
*
|
318
|
* @return array
|
319
|
* An array of taxonomy search option titles.
|
320
|
*/
|
321
|
function _taxonomy_feeds_form_callback_options() {
|
322
|
return array(
|
323
|
FEEDS_TAXONOMY_SEARCH_TERM_NAME => 'Term name',
|
324
|
FEEDS_TAXONOMY_SEARCH_TERM_ID => 'Term ID',
|
325
|
FEEDS_TAXONOMY_SEARCH_TERM_GUID => 'GUID',
|
326
|
);
|
327
|
}
|