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