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
|
// Add in default values.
|
89
|
$mapping += array(
|
90
|
'term_search' => FEEDS_TAXONOMY_SEARCH_TERM_NAME,
|
91
|
'autocreate' => FALSE,
|
92
|
);
|
93
|
|
94
|
$info = field_info_field($target);
|
95
|
|
96
|
$cache = &drupal_static(__FUNCTION__);
|
97
|
if (!isset($cache['allowed_values'][$target])) {
|
98
|
$cache['allowed_values'][$target] = taxonomy_allowed_values($info);
|
99
|
}
|
100
|
|
101
|
if (!isset($cache['allowed_vocabularies'][$target])) {
|
102
|
foreach ($info['settings']['allowed_values'] as $tree) {
|
103
|
if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
|
104
|
$cache['allowed_vocabularies'][$target][$vocabulary->vid] = $vocabulary->machine_name;
|
105
|
}
|
106
|
}
|
107
|
}
|
108
|
|
109
|
// Some kind of configuration issue. Perhaps the vocabulary was deleted.
|
110
|
// Nothing we can do about it.
|
111
|
if (empty($cache['allowed_vocabularies'][$target])) {
|
112
|
return;
|
113
|
}
|
114
|
|
115
|
$query = new EntityFieldQuery();
|
116
|
$query->entityCondition('entity_type', 'taxonomy_term')
|
117
|
->entityCondition('bundle', $cache['allowed_vocabularies'][$target])
|
118
|
->range(0, 1);
|
119
|
|
120
|
|
121
|
$field = isset($entity->$target) ? $entity->$target : array('und' => array());
|
122
|
|
123
|
// Allow for multiple mappings to the same target.
|
124
|
$delta = count($field['und']);
|
125
|
|
126
|
// Iterate over all values.
|
127
|
foreach ($terms as $term) {
|
128
|
|
129
|
if ($info['cardinality'] == $delta) {
|
130
|
break;
|
131
|
}
|
132
|
|
133
|
$tid = FALSE;
|
134
|
|
135
|
// FeedsTermElement already is a term.
|
136
|
if ($term instanceof FeedsTermElement) {
|
137
|
$tid = $term->tid;
|
138
|
}
|
139
|
else {
|
140
|
switch ($mapping['term_search']) {
|
141
|
|
142
|
// Lookup by name.
|
143
|
case FEEDS_TAXONOMY_SEARCH_TERM_NAME:
|
144
|
$term = trim($term);
|
145
|
$name_query = clone $query;
|
146
|
if (strlen($term) && $tids = $name_query->propertyCondition('name', $term)->execute()) {
|
147
|
|
148
|
// Find the first allowed term.
|
149
|
foreach ($tids['taxonomy_term'] as $possible_term) {
|
150
|
if (isset($cache['allowed_values'][$target][$possible_term->tid])) {
|
151
|
$tid = $possible_term->tid;
|
152
|
break;
|
153
|
}
|
154
|
}
|
155
|
}
|
156
|
elseif ($mapping['autocreate'] && strlen($term)) {
|
157
|
$term = (object) array(
|
158
|
'name' => drupal_substr($term, 0, 255),
|
159
|
'vid' => key($cache['allowed_vocabularies'][$target]),
|
160
|
'vocabulary_machine_name' => reset($cache['allowed_vocabularies'][$target]),
|
161
|
);
|
162
|
taxonomy_term_save($term);
|
163
|
$tid = $term->tid;
|
164
|
// Add to the list of allowed values.
|
165
|
$cache['allowed_values'][$target][$tid] = $term->name;
|
166
|
}
|
167
|
break;
|
168
|
|
169
|
// Lookup by tid.
|
170
|
case FEEDS_TAXONOMY_SEARCH_TERM_ID:
|
171
|
if (is_numeric($term)) {
|
172
|
$tid = (int) $term;
|
173
|
}
|
174
|
break;
|
175
|
|
176
|
// Lookup by GUID.
|
177
|
case FEEDS_TAXONOMY_SEARCH_TERM_GUID:
|
178
|
$tid = taxonomy_feeds_term_lookup_term_by_guid($term);
|
179
|
break;
|
180
|
}
|
181
|
}
|
182
|
|
183
|
if ($tid && isset($cache['allowed_values'][$target][$tid])) {
|
184
|
$field['und'][] = array('tid' => $tid);
|
185
|
$delta++;
|
186
|
}
|
187
|
}
|
188
|
|
189
|
$entity->$target = $field;
|
190
|
}
|
191
|
|
192
|
/**
|
193
|
* Finds all terms associated with the given node, within one vocabulary.
|
194
|
*/
|
195
|
function taxonomy_feeds_node_get_terms($node, $key = 'tid') {
|
196
|
$terms = &drupal_static(__FUNCTION__);
|
197
|
|
198
|
if (!isset($terms[$node->nid][$key])) {
|
199
|
// Get tids from all taxonomy_term_reference fields.
|
200
|
$tids = array();
|
201
|
$fields = field_info_fields();
|
202
|
foreach ($fields as $field_name => $field) {
|
203
|
if ($field['type'] == 'taxonomy_term_reference' && field_info_instance('node', $field_name, $node->type)) {
|
204
|
if (($items = field_get_items('node', $node, $field_name)) && is_array($items)) {
|
205
|
$tids = array_merge($tids, array_map('_taxonomy_feeds_extract_tid', $items));
|
206
|
}
|
207
|
}
|
208
|
}
|
209
|
|
210
|
// Load terms and cache them in static var.
|
211
|
$curr_terms = taxonomy_term_load_multiple($tids);
|
212
|
$terms[$node->nid][$key] = array();
|
213
|
foreach ($curr_terms as $term) {
|
214
|
$terms[$node->nid][$key][$term->$key] = $term;
|
215
|
}
|
216
|
}
|
217
|
return $terms[$node->nid][$key];
|
218
|
}
|
219
|
|
220
|
/**
|
221
|
* Extracts tid from array item returned by field_get_items().
|
222
|
*
|
223
|
* @param array $item
|
224
|
* Tid information in the form of a single element array
|
225
|
* (key == 'tid', value == tid we're looking for)
|
226
|
*
|
227
|
* @return int
|
228
|
* Term id extracted from $item.
|
229
|
*
|
230
|
* @see taxonomy_feeds_node_get_terms()
|
231
|
* @see field_get_items()
|
232
|
*/
|
233
|
function _taxonomy_feeds_extract_tid($item) {
|
234
|
return $item['tid'];
|
235
|
}
|
236
|
|
237
|
/**
|
238
|
* Looks up a term by GUID, assumes SQL storage backend.
|
239
|
*
|
240
|
* @param string $guid
|
241
|
* The Feeds GUID to compare against.
|
242
|
*
|
243
|
* @return int|FALSE
|
244
|
* The term id, or FALSE if one was not found.
|
245
|
*/
|
246
|
function taxonomy_feeds_term_lookup_term_by_guid($guid) {
|
247
|
return db_select('feeds_item')
|
248
|
->fields('feeds_item', array('entity_id'))
|
249
|
->condition('entity_type', 'taxonomy_term')
|
250
|
->condition('guid', $guid)
|
251
|
->execute()
|
252
|
->fetchField();
|
253
|
}
|
254
|
|
255
|
/**
|
256
|
* Mapping configuration summary for taxonomy.module.
|
257
|
*/
|
258
|
function taxonomy_feeds_summary_callback(array $mapping, $target, array $form, array $form_state) {
|
259
|
$options = _taxonomy_feeds_form_callback_options();
|
260
|
if (empty($mapping['term_search'])) {
|
261
|
return t('Search taxonomy terms by: <strong>@search</strong>', array('@search' => $options[FEEDS_TAXONOMY_SEARCH_TERM_NAME]));
|
262
|
}
|
263
|
return t('Search taxonomy terms by: <strong>@search</strong>', array('@search' => $options[$mapping['term_search']]));
|
264
|
}
|
265
|
|
266
|
/**
|
267
|
* Settings form callback.
|
268
|
*/
|
269
|
function taxonomy_feeds_form_callback(array $mapping, $target, array $form, array $form_state) {
|
270
|
return array(
|
271
|
'term_search' => array(
|
272
|
'#type' => 'select',
|
273
|
'#title' => t('Search taxonomy terms by'),
|
274
|
'#options' => _taxonomy_feeds_form_callback_options(),
|
275
|
'#default_value' => !empty($mapping['term_search']) ? $mapping['term_search'] : FEEDS_TAXONOMY_SEARCH_TERM_NAME,
|
276
|
),
|
277
|
'autocreate' => array(
|
278
|
'#type' => 'checkbox',
|
279
|
'#title' => t('Auto create'),
|
280
|
'#description' => t("Create the term if it doesn't exist."),
|
281
|
'#default_value' => !empty($mapping['autocreate']) ? $mapping['autocreate'] : 0,
|
282
|
'#states' => array(
|
283
|
'visible' => array(
|
284
|
':input[name$="[settings][term_search]"]' => array('value' => FEEDS_TAXONOMY_SEARCH_TERM_NAME),
|
285
|
),
|
286
|
),
|
287
|
),
|
288
|
);
|
289
|
}
|
290
|
|
291
|
/**
|
292
|
* Returns the list of available term search methods.
|
293
|
*
|
294
|
* @return array
|
295
|
* An array of taxonomy search option titles.
|
296
|
*/
|
297
|
function _taxonomy_feeds_form_callback_options() {
|
298
|
return array(
|
299
|
FEEDS_TAXONOMY_SEARCH_TERM_NAME => 'Term name',
|
300
|
FEEDS_TAXONOMY_SEARCH_TERM_ID => 'Term ID',
|
301
|
FEEDS_TAXONOMY_SEARCH_TERM_GUID => 'GUID',
|
302
|
);
|
303
|
}
|