root / drupal7 / sites / all / modules / feeds / feeds.api.php @ 3aa14731
1 |
<?php
|
---|---|
2 |
|
3 |
/**
|
4 |
* @file
|
5 |
* Documentation of Feeds hooks.
|
6 |
*/
|
7 |
|
8 |
/**
|
9 |
* Feeds offers a CTools based plugin API. Fetchers, parsers and processors are
|
10 |
* declared to Feeds as plugins.
|
11 |
*
|
12 |
* @see feeds_feeds_plugins()
|
13 |
* @see FeedsFetcher
|
14 |
* @see FeedsParser
|
15 |
* @see FeedsProcessor
|
16 |
*
|
17 |
* @defgroup pluginapi Plugin API
|
18 |
* @{
|
19 |
*/
|
20 |
|
21 |
/**
|
22 |
* Example of a CTools plugin hook that needs to be implemented to make
|
23 |
* hook_feeds_plugins() discoverable by CTools and Feeds. The hook specifies
|
24 |
* that the hook_feeds_plugins() returns Feeds Plugin API version 1 style
|
25 |
* plugins.
|
26 |
*/
|
27 |
function hook_ctools_plugin_api($owner, $api) { |
28 |
if ($owner == 'feeds' && $api == 'plugins') { |
29 |
return array('version' => 1); |
30 |
} |
31 |
} |
32 |
|
33 |
/**
|
34 |
* A hook_feeds_plugins() declares available Fetcher, Parser or Processor
|
35 |
* plugins to Feeds. For an example look at feeds_feeds_plugin(). For exposing
|
36 |
* this hook hook_ctools_plugin_api() MUST be implemented, too.
|
37 |
*
|
38 |
* @see feeds_feeds_plugin()
|
39 |
*/
|
40 |
function hook_feeds_plugins() { |
41 |
$info = array(); |
42 |
$info['MyFetcher'] = array( |
43 |
'name' => 'My Fetcher', |
44 |
'description' => 'Fetches my stuff.', |
45 |
'help' => 'More verbose description here. Will be displayed on fetcher selection menu.', |
46 |
'handler' => array( |
47 |
'parent' => 'FeedsFetcher', |
48 |
'class' => 'MyFetcher', |
49 |
'file' => 'MyFetcher.inc', |
50 |
'path' => drupal_get_path('module', 'my_module'), // Feeds will look for MyFetcher.inc in the my_module directory. |
51 |
), |
52 |
); |
53 |
$info['MyParser'] = array( |
54 |
'name' => 'ODK parser', |
55 |
'description' => 'Parse my stuff.', |
56 |
'help' => 'More verbose description here. Will be displayed on parser selection menu.', |
57 |
'handler' => array( |
58 |
'parent' => 'FeedsParser', // Being directly or indirectly an extension of FeedsParser makes a plugin a parser plugin. |
59 |
'class' => 'MyParser', |
60 |
'file' => 'MyParser.inc', |
61 |
'path' => drupal_get_path('module', 'my_module'), |
62 |
), |
63 |
); |
64 |
$info['MyProcessor'] = array( |
65 |
'name' => 'ODK parser', |
66 |
'description' => 'Process my stuff.', |
67 |
'help' => 'More verbose description here. Will be displayed on processor selection menu.', |
68 |
'handler' => array( |
69 |
'parent' => 'FeedsProcessor', |
70 |
'class' => 'MyProcessor', |
71 |
'file' => 'MyProcessor.inc', |
72 |
'path' => drupal_get_path('module', 'my_module'), |
73 |
), |
74 |
); |
75 |
return $info; |
76 |
} |
77 |
|
78 |
/**
|
79 |
* @}
|
80 |
*/
|
81 |
|
82 |
/**
|
83 |
* @defgroup import Import and clear hooks
|
84 |
* @{
|
85 |
*/
|
86 |
|
87 |
/**
|
88 |
* Invoked after a feed source has been parsed, before it will be processed.
|
89 |
*
|
90 |
* @param FeedsSource $source
|
91 |
* FeedsSource object that describes the source that has been imported.
|
92 |
* @param FeedsParserResult $result
|
93 |
* FeedsParserResult object that has been parsed from the source.
|
94 |
*/
|
95 |
function hook_feeds_after_parse(FeedsSource $source, FeedsParserResult $result) { |
96 |
// For example, set title of imported content:
|
97 |
$result->title = 'Import number ' . my_module_import_id(); |
98 |
} |
99 |
|
100 |
/**
|
101 |
* Invoked before a feed source import starts.
|
102 |
*
|
103 |
* @param FeedsSource $source
|
104 |
* FeedsSource object that describes the source that is going to be imported.
|
105 |
*/
|
106 |
function hook_feeds_before_import(FeedsSource $source) { |
107 |
// See feeds_rules module's implementation for an example.
|
108 |
} |
109 |
|
110 |
/**
|
111 |
* Invoked before a feed item is updated/created/replaced.
|
112 |
*
|
113 |
* This is called every time a feed item is processed no matter if the item gets
|
114 |
* updated or not.
|
115 |
*
|
116 |
* @param FeedsSource $source
|
117 |
* The source for the current feed.
|
118 |
* @param array $item
|
119 |
* All the current item from the feed.
|
120 |
* @param int|null $entity_id
|
121 |
* The id of the current item which is going to be updated. If this is a new
|
122 |
* item, then NULL is passed.
|
123 |
*/
|
124 |
function hook_feeds_before_update(FeedsSource $source, $item, $entity_id) { |
125 |
if ($entity_id) { |
126 |
$processor = $source->importer->processor; |
127 |
db_update('foo_bar')
|
128 |
->fields(array('entity_type' => $processor->entityType(), 'entity_id' => $entity_id, 'last_seen' => REQUEST_TIME)) |
129 |
->condition('entity_type', $processor->entityType()) |
130 |
->condition('entity_id', $entity_id) |
131 |
->execute(); |
132 |
} |
133 |
} |
134 |
|
135 |
/**
|
136 |
* Invoked before a feed item is saved.
|
137 |
*
|
138 |
* @param FeedsSource $source
|
139 |
* FeedsSource object that describes the source that is being imported.
|
140 |
* @param $entity
|
141 |
* The entity object.
|
142 |
* @param array $item
|
143 |
* The parser result for this entity.
|
144 |
* @param int|null $entity_id
|
145 |
* The id of the current item which is going to be updated. If this is a new
|
146 |
* item, then NULL is passed.
|
147 |
*/
|
148 |
function hook_feeds_presave(FeedsSource $source, $entity, $item, $entity_id) { |
149 |
if ($entity->feeds_item->entity_type == 'node') { |
150 |
// Skip saving this entity.
|
151 |
$entity->feeds_item->skip = TRUE; |
152 |
} |
153 |
} |
154 |
|
155 |
/**
|
156 |
* Invoked after a feed item has been saved.
|
157 |
*
|
158 |
* @param FeedsSource $source
|
159 |
* FeedsSource object that describes the source that is being imported.
|
160 |
* @param $entity
|
161 |
* The entity object that has just been saved.
|
162 |
* @param array $item
|
163 |
* The parser result for this entity.
|
164 |
* @param int|null $entity_id
|
165 |
* The id of the current item which is going to be updated. If this is a new
|
166 |
* item, then NULL is passed.
|
167 |
*/
|
168 |
function hook_feeds_after_save(FeedsSource $source, $entity, $item, $entity_id) { |
169 |
// Use $entity->nid of the saved node.
|
170 |
|
171 |
// Although the $entity object is passed by reference, any changes made in
|
172 |
// this function will be ignored by the FeedsProcessor.
|
173 |
$config = $source->importer->getConfig(); |
174 |
|
175 |
if ($config['processor']['config']['purge_unseen_items'] && isset($entity->feeds_item)) { |
176 |
$feeds_item = $entity->feeds_item; |
177 |
$feeds_item->batch_id = feeds_delete_get_current_batch($feeds_item->feed_nid); |
178 |
|
179 |
drupal_write_record('feeds_delete_item', $feeds_item); |
180 |
} |
181 |
} |
182 |
|
183 |
/**
|
184 |
* Invoked after a feed source has been imported.
|
185 |
*
|
186 |
* @param FeedsSource $source
|
187 |
* FeedsSource object that describes the source that has been imported.
|
188 |
*/
|
189 |
function hook_feeds_after_import(FeedsSource $source) { |
190 |
// See geotaxonomy module's implementation for an example.
|
191 |
|
192 |
// We can also check for an exception in this hook. The exception should not
|
193 |
// be thrown here, Feeds will handle it.
|
194 |
if (isset($source->exception)) { |
195 |
watchdog('mymodule', 'An exception occurred during importing!', array(), WATCHDOG_ERROR); |
196 |
mymodule_panic_reaction($source);
|
197 |
} |
198 |
} |
199 |
|
200 |
/**
|
201 |
* Invoked after a feed source has been cleared of its items.
|
202 |
*
|
203 |
* @param FeedsSource $source
|
204 |
* FeedsSource object that describes the source that has been cleared.
|
205 |
*/
|
206 |
function hook_feeds_after_clear(FeedsSource $source) { |
207 |
} |
208 |
|
209 |
/**
|
210 |
* @}
|
211 |
*/
|
212 |
|
213 |
/**
|
214 |
* @defgroup mappingapi Mapping API
|
215 |
* @{
|
216 |
*/
|
217 |
|
218 |
/**
|
219 |
* Alter mapping sources.
|
220 |
*
|
221 |
* Use this hook to add additional mapping sources for any parser. Allows for
|
222 |
* registering a callback to be invoked at mapping time.
|
223 |
*
|
224 |
* @see my_source_get_source().
|
225 |
* @see locale_feeds_parser_sources_alter().
|
226 |
*/
|
227 |
function hook_feeds_parser_sources_alter(&$sources, $content_type) { |
228 |
$sources['my_source'] = array( |
229 |
'name' => t('Images in description element'), |
230 |
'description' => t('Images occurring in the description element of a feed item.'), |
231 |
'callback' => 'my_source_get_source', |
232 |
); |
233 |
} |
234 |
|
235 |
/**
|
236 |
* Example callback specified in hook_feeds_parser_sources_alter().
|
237 |
*
|
238 |
* To be invoked on mapping time.
|
239 |
*
|
240 |
* @param $source
|
241 |
* The FeedsSource object being imported.
|
242 |
* @param $result
|
243 |
* The FeedsParserResult object being mapped from.
|
244 |
* @param $key
|
245 |
* The key specified in the $sources array in
|
246 |
* hook_feeds_parser_sources_alter().
|
247 |
*
|
248 |
* @return
|
249 |
* The value to be extracted from the source.
|
250 |
*
|
251 |
* @see hook_feeds_parser_sources_alter()
|
252 |
* @see locale_feeds_get_source()
|
253 |
*/
|
254 |
function my_source_get_source(FeedsSource $source, FeedsParserResult $result, $key) { |
255 |
$item = $result->currentItem(); |
256 |
return my_source_parse_images($item['description']); |
257 |
} |
258 |
|
259 |
/**
|
260 |
* Adds mapping targets for processors.
|
261 |
*
|
262 |
* This hook allows additional target options to be added to the processors
|
263 |
* mapping form.
|
264 |
*
|
265 |
* If the key in $targets[] does not correspond to the actual key on the node
|
266 |
* object ($node->key), real_target MUST be specified. See mappers/link.inc
|
267 |
*
|
268 |
* For an example implementation, see mappers/text.inc
|
269 |
*
|
270 |
* @param string $entity_type
|
271 |
* The entity type of the target, for instance a 'node' entity.
|
272 |
* @param string $bundle
|
273 |
* The entity bundle to return targets for.
|
274 |
*
|
275 |
* @return array
|
276 |
* An array whose keys are the target name and whose values are arrays
|
277 |
* containing the following keys:
|
278 |
* - name: A human readable, translated label for the target.
|
279 |
* - description: (optional) A human readable, translated description for the
|
280 |
* target.
|
281 |
* - callback: The callback used to set the value on the target.
|
282 |
* - real_target: (optional) the name of the property on the entity that will
|
283 |
* be set by the callback. Specify this if the target name is not equal to
|
284 |
* the entity property name. This information will be used to clear the
|
285 |
* right target at the beginning of the mapping process.
|
286 |
* - optional_unique: (optional) A boolean that indicates whether or not the
|
287 |
* target can be used as an unique target. If you set this to TRUE, be sure
|
288 |
* to also specify "unique_callbacks".
|
289 |
* - unique_callbacks: (optional) An array of callbacks that are used to
|
290 |
* retrieve existing entity ids. Existing entities can be updated based on
|
291 |
* unique targets.
|
292 |
* - form_callbacks: (optional) An array of callbacks that are used to return
|
293 |
* a form with additional configuration for a target.
|
294 |
* - summary_callbacks: (optional) An array of callbacks that are used to
|
295 |
* display values of additional target configuration.
|
296 |
* - preprocess_callbacks: (optional) An array of callbacks that are used to
|
297 |
* set or change mapping options.
|
298 |
* - deprecated: (optional) A boolean that if TRUE, hides the target from the
|
299 |
* UI. Use this if you want to rename targets for consistency, but don't
|
300 |
* want to break importers that are using the old target name. If an
|
301 |
* importer uses this target it will show up as "DEPRECATED" in the UI.
|
302 |
*/
|
303 |
function hook_feeds_processor_targets($entity_type, $bundle) { |
304 |
$targets = array(); |
305 |
|
306 |
if ($entity_type == 'node') { |
307 |
// Example 1: provide the minimal info for a target. Description is
|
308 |
// optional, but recommended.
|
309 |
// @see my_module_set_target()
|
310 |
$targets['my_node_field'] = array( |
311 |
'name' => t('My custom node field'), |
312 |
'description' => t('Description of what my custom node field does.'), |
313 |
'callback' => 'my_module_set_target', |
314 |
); |
315 |
|
316 |
// Example 2: specify "real_target" if the target name is different from
|
317 |
// the entity property name.
|
318 |
// Here the target is called "my_node_field2:uri", but the entity property
|
319 |
// is called "my_node_field2". This will ensure that the property
|
320 |
// "my_node_field2" is cleared out that the beginning of the mapping
|
321 |
// process.
|
322 |
$targets['my_node_field2:uri'] = array( |
323 |
'name' => t('My third custom node field'), |
324 |
'description' => t('A target that sets a property that does not have the same name as the target.'), |
325 |
'callback' => 'my_module_set_target2', |
326 |
'real_target' => 'my_node_field2', |
327 |
); |
328 |
|
329 |
// Example 3: you can make your target selectable as an unique target by
|
330 |
// setting "optional_unique" to TRUE and specify one or more callbacks to
|
331 |
// retrieve existing entity id's.
|
332 |
// @see my_module_mapper_unique()
|
333 |
$targets['my_node_field3'] = array( |
334 |
'name' => t('My third custom node field'), |
335 |
'description' => t('A field that can be set as an unique target.'), |
336 |
'callback' => 'my_module_set_target3', |
337 |
'optional_unique' => TRUE, |
338 |
'unique_callbacks' => array('my_module_mapper_unique'), |
339 |
); |
340 |
|
341 |
// Example 4: use the form and summary callbacks to add additional
|
342 |
// configuration options for your target. Use the form callbacks to provide
|
343 |
// a form to set the target configuration. Use the summary callbacks to
|
344 |
// display the target configuration.
|
345 |
// @see my_module_form_callback()
|
346 |
// @see my_module_summary_callback()
|
347 |
$targets['my_node_field4'] = array( |
348 |
'name' => t('My fourth custom node field'), |
349 |
'description' => t('A field with additional configuration.'), |
350 |
'callback' => 'my_module_set_target4', |
351 |
'form_callbacks' => array('my_module_form_callback'), |
352 |
'summary_callbacks' => array('my_module_summary_callback'), |
353 |
); |
354 |
|
355 |
// Example 5: use preprocess callbacks to set or change mapping options.
|
356 |
// @see my_module_preprocess_callback()
|
357 |
$targets['my_node_field5'] = array( |
358 |
'name' => t('My fifth custom node field'), |
359 |
'description' => t('A field with additional configuration.'), |
360 |
'callback' => 'my_module_set_target5', |
361 |
'preprocess_callbacks' => array('my_module_preprocess_callback'), |
362 |
); |
363 |
|
364 |
// Example 6: when you want to remove or rename previously provided targets,
|
365 |
// you can set "deprecated" to TRUE for the old target name. This will make
|
366 |
// the target to be no longer selectable in the UI. If an importer uses this
|
367 |
// target it will show up as "DEPRECATED" in the UI.
|
368 |
// If you want that the target continues to work, you can still specify the
|
369 |
// callback.
|
370 |
$targets['deprecated_target'] = array( |
371 |
'name' => t('A target that cannot be chosen in the UI.'), |
372 |
'deprecated' => TRUE, |
373 |
); |
374 |
} |
375 |
|
376 |
return $targets; |
377 |
} |
378 |
|
379 |
/**
|
380 |
* Alters the target array.
|
381 |
*
|
382 |
* This hook allows modifying the target array.
|
383 |
*
|
384 |
* @param array &$targets
|
385 |
* Array containing the targets to be offered to the user. Add to this array
|
386 |
* to expose additional options.
|
387 |
* @param string $entity_type
|
388 |
* The entity type of the target, for instance a 'node' entity.
|
389 |
* @param string $bundle
|
390 |
* The entity bundle to return targets for.
|
391 |
*
|
392 |
* @see hook_feeds_processor_targets()
|
393 |
*/
|
394 |
function hook_feeds_processor_targets_alter(array &$targets, $entity_type, $bundle) { |
395 |
// Example: set an existing target as optional unique.
|
396 |
if ($entity_type == 'node' && $bundle == 'article') { |
397 |
if (isset($targets['nid'])) { |
398 |
$targets['nid']['unique_callbacks'][] = 'my_module_mapper_unique'; |
399 |
$targets['nid']['optional_unique'] = TRUE; |
400 |
} |
401 |
} |
402 |
} |
403 |
|
404 |
/**
|
405 |
* Example callback specified in hook_feeds_processor_targets().
|
406 |
*
|
407 |
* @param FeedsSource $source
|
408 |
* Field mapper source settings.
|
409 |
* @param object $entity
|
410 |
* An entity object, for instance a node object.
|
411 |
* @param string $target
|
412 |
* A string identifying the target on the node.
|
413 |
* @param array $values
|
414 |
* The value to populate the target with.
|
415 |
* @param array $mapping
|
416 |
* Associative array of the mapping settings from the per mapping
|
417 |
* configuration form.
|
418 |
*/
|
419 |
function my_module_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) { |
420 |
$entity->{$target}[$entity->language][0]['value'] = reset($values); |
421 |
if (isset($source->importer->processor->config['input_format'])) { |
422 |
$entity->{$target}[$entity->language][0]['format'] = $source->importer->processor->config['input_format']; |
423 |
} |
424 |
} |
425 |
|
426 |
/**
|
427 |
* Example of the form_callback specified in hook_feeds_processor_targets().
|
428 |
*
|
429 |
* The arguments are the same that my_module_summary_callback() gets.
|
430 |
*
|
431 |
* @return array
|
432 |
* The per mapping configuration form. Once the form is saved, $mapping will
|
433 |
* be populated with the form values.
|
434 |
*
|
435 |
* @see my_module_summary_callback()
|
436 |
*/
|
437 |
function my_module_form_callback(array $mapping, $target, array $form, array $form_state) { |
438 |
return array( |
439 |
'my_setting' => array( |
440 |
'#type' => 'checkbox', |
441 |
'#title' => t('My setting checkbox'), |
442 |
'#default_value' => !empty($mapping['my_setting']), |
443 |
), |
444 |
); |
445 |
} |
446 |
|
447 |
/**
|
448 |
* Example of the summary_callback specified in hook_feeds_processor_targets().
|
449 |
*
|
450 |
* @param array $mapping
|
451 |
* Associative array of the mapping settings.
|
452 |
* @param string $target
|
453 |
* Array of target settings, as defined by the processor or
|
454 |
* hook_feeds_processor_targets_alter().
|
455 |
* @param array $form
|
456 |
* The whole mapping form.
|
457 |
* @param array $form_state
|
458 |
* The form state of the mapping form.
|
459 |
*
|
460 |
* @return string
|
461 |
* Returns, as a string that may contain HTML, the summary to display while
|
462 |
* the full form isn't visible.
|
463 |
* If the return value is empty, no summary and no option to view the form
|
464 |
* will be displayed.
|
465 |
*/
|
466 |
function my_module_summary_callback(array $mapping, $target, array $form, array $form_state) { |
467 |
if (empty($mapping['my_setting'])) { |
468 |
return t('My setting <strong>not</strong> active'); |
469 |
} |
470 |
else {
|
471 |
return t('My setting <strong>active</strong>'); |
472 |
} |
473 |
} |
474 |
|
475 |
/**
|
476 |
* Example of the unique_callbacks specified in hook_feeds_processor_targets().
|
477 |
*
|
478 |
* @param FeedsSource $source
|
479 |
* The Feed source.
|
480 |
* @param string $entity_type
|
481 |
* Entity type for the entity to be processed.
|
482 |
* @param string $bundle
|
483 |
* Bundle name for the entity to be processed.
|
484 |
* @param string $target
|
485 |
* A string identifying the unique target on the entity.
|
486 |
* @param array $values
|
487 |
* The unique values to be checked.
|
488 |
*
|
489 |
* @return int|null
|
490 |
* The existing entity id, or NULL if no existing entity is found.
|
491 |
*
|
492 |
* @see hook_feeds_processor_targets()
|
493 |
* @see FeedsProcessor::existingEntityId()
|
494 |
*/
|
495 |
function my_module_mapper_unique(FeedsSource $source, $entity_type, $bundle, $target, array $values) { |
496 |
list($field_name, $column) = explode(':', $target . ':value'); |
497 |
// Example for if the target is a field.
|
498 |
$query = new EntityFieldQuery(); |
499 |
$result = $query |
500 |
->entityCondition('entity_type', $entity_type) |
501 |
->entityCondition('bundle', $bundle) |
502 |
->fieldCondition($field_name, $column, $values) |
503 |
->execute(); |
504 |
|
505 |
if (!empty($result[$entity_type])) { |
506 |
return key($result[$entity_type]); |
507 |
} |
508 |
} |
509 |
|
510 |
/**
|
511 |
* Example of the preprocess_callbacks specified in hook_feeds_processor_targets().
|
512 |
*
|
513 |
* @param array $target
|
514 |
* The full target definition.
|
515 |
* @param array &$mapping
|
516 |
* The mapping configuration.
|
517 |
*
|
518 |
* @see hook_feeds_processor_targets()
|
519 |
*/
|
520 |
function my_module_preprocess_callback(array $target, array &$mapping) { |
521 |
// Add in default values.
|
522 |
$mapping += array('setting_value' => TRUE); |
523 |
} |
524 |
|
525 |
/**
|
526 |
* @}
|
527 |
*/
|