Révision 41cc1b08
Ajouté par Assos Assos il y a presque 9 ans
drupal7/sites/all/modules/feeds/feeds.api.php | ||
---|---|---|
136 | 136 |
* Invoked before a feed item is saved. |
137 | 137 |
* |
138 | 138 |
* @param FeedsSource $source |
139 |
* FeedsSource object that describes the source that is being imported. |
|
139 |
* FeedsSource object that describes the source that is being imported.
|
|
140 | 140 |
* @param $entity |
141 | 141 |
* The entity object. |
142 | 142 |
* @param array $item |
143 | 143 |
* The parser result for this entity. |
144 | 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. |
|
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 | 147 |
*/ |
148 |
function hook_feeds_presave(FeedsSource $source, $entity, $item) { |
|
148 |
function hook_feeds_presave(FeedsSource $source, $entity, $item, $entity_id) {
|
|
149 | 149 |
if ($entity->feeds_item->entity_type == 'node') { |
150 | 150 |
// Skip saving this entity. |
151 | 151 |
$entity->feeds_item->skip = TRUE; |
... | ... | |
188 | 188 |
*/ |
189 | 189 |
function hook_feeds_after_import(FeedsSource $source) { |
190 | 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 |
} |
|
191 | 198 |
} |
192 | 199 |
|
193 | 200 |
/** |
... | ... | |
250 | 257 |
} |
251 | 258 |
|
252 | 259 |
/** |
253 |
* Alter mapping targets for entities. Use this hook to add additional target |
|
254 |
* options to the mapping form of Node processors. |
|
260 |
* Adds mapping targets for processors. |
|
261 |
* |
|
262 |
* This hook allows additional target options to be added to the processors |
|
263 |
* mapping form. |
|
255 | 264 |
* |
256 | 265 |
* If the key in $targets[] does not correspond to the actual key on the node |
257 | 266 |
* object ($node->key), real_target MUST be specified. See mappers/link.inc |
258 | 267 |
* |
259 |
* For an example implementation, see mappers/content.inc
|
|
268 |
* For an example implementation, see mappers/text.inc
|
|
260 | 269 |
* |
261 |
* @param &$targets |
|
262 |
* Array containing the targets to be offered to the user. Add to this array |
|
263 |
* to expose additional options. Remove from this array to suppress options. |
|
264 |
* Remove with caution. |
|
265 |
* @param $entity_type |
|
270 |
* @param string $entity_type |
|
266 | 271 |
* The entity type of the target, for instance a 'node' entity. |
267 |
* @param $bundle_name |
|
268 |
* The bundle name for which to alter targets. |
|
272 |
* @param string $bundle |
|
273 |
* The entity bundle to return targets for. |
|
274 |
* |
|
275 |
* @return array |
|
276 |
* Array containing the targets to be offered to the user. This function must |
|
277 |
* return an array, even an empty one. |
|
269 | 278 |
*/ |
270 |
function hook_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) { |
|
279 |
function hook_feeds_processor_targets($entity_type, $bundle) { |
|
280 |
$targets = array(); |
|
281 |
|
|
271 | 282 |
if ($entity_type == 'node') { |
272 | 283 |
$targets['my_node_field'] = array( |
273 | 284 |
'name' => t('My custom node field'), |
... | ... | |
276 | 287 |
|
277 | 288 |
// Specify both summary_callback and form_callback to add a per mapping |
278 | 289 |
// configuration form. |
279 |
'summary_callback' => 'my_module_summary_callback',
|
|
280 |
'form_callback' => 'my_module_form_callback',
|
|
290 |
'summary_callbacks' => array('my_module_summary_callback'),
|
|
291 |
'form_callbacks' => array('my_module_form_callback'),
|
|
281 | 292 |
); |
282 | 293 |
$targets['my_node_field2'] = array( |
283 | 294 |
'name' => t('My Second custom node field'), |
... | ... | |
285 | 296 |
'callback' => 'my_module_set_target2', |
286 | 297 |
'real_target' => 'my_node_field_two', // Specify real target field on node. |
287 | 298 |
); |
299 |
$targets['my_node_field3'] = array( |
|
300 |
'name' => t('My third custom node field'), |
|
301 |
'description' => t('Description of what my third custom node field does.'), |
|
302 |
'callback' => 'my_module_set_target3', |
|
303 |
|
|
304 |
// Set optional_unique to TRUE and specify unique_callbacks to allow the |
|
305 |
// target to be unique. Existing entities can be updated based on unique |
|
306 |
// targets. |
|
307 |
'optional_unique' => TRUE, |
|
308 |
'unique_callbacks' => array('my_module_mapper_unique'), |
|
309 |
|
|
310 |
// Preprocess callbacks are called before the actual callback allowing you |
|
311 |
// to prepare values on the entity or mapping array. |
|
312 |
'preprocess_callbacks' => array('my_module_preprocess_callback'), |
|
313 |
); |
|
288 | 314 |
} |
315 |
|
|
316 |
return $targets; |
|
289 | 317 |
} |
290 | 318 |
|
291 | 319 |
/** |
292 |
* Example callback specified in hook_feeds_processor_targets_alter().
|
|
320 |
* Alters the target array.
|
|
293 | 321 |
* |
294 |
* @param $source |
|
322 |
* This hook allows modifying the target array. |
|
323 |
* |
|
324 |
* @param array &$targets |
|
325 |
* Array containing the targets to be offered to the user. Add to this array |
|
326 |
* to expose additional options. |
|
327 |
* @param string $entity_type |
|
328 |
* The entity type of the target, for instance a 'node' entity. |
|
329 |
* @param string $bundle |
|
330 |
* The entity bundle to return targets for. |
|
331 |
* |
|
332 |
* @see hook_feeds_processor_targets() |
|
333 |
*/ |
|
334 |
function hook_feeds_processor_targets_alter(array &$targets, $entity_type, $bundle) { |
|
335 |
if ($entity_type == 'node' && $bundle == 'article') { |
|
336 |
if (isset($targets['nid'])) { |
|
337 |
$targets['nid']['unique_callbacks'][] = 'my_module_mapper_unique'; |
|
338 |
$targets['nid']['optional_unique'] = TRUE; |
|
339 |
} |
|
340 |
} |
|
341 |
} |
|
342 |
|
|
343 |
/** |
|
344 |
* Example callback specified in hook_feeds_processor_targets(). |
|
345 |
* |
|
346 |
* @param FeedsSource $source |
|
295 | 347 |
* Field mapper source settings. |
296 |
* @param $entity |
|
348 |
* @param object $entity
|
|
297 | 349 |
* An entity object, for instance a node object. |
298 |
* @param $target |
|
350 |
* @param string $target
|
|
299 | 351 |
* A string identifying the target on the node. |
300 |
* @param $value
|
|
352 |
* @param array $values
|
|
301 | 353 |
* The value to populate the target with. |
302 |
* @param $mapping |
|
354 |
* @param array $mapping
|
|
303 | 355 |
* Associative array of the mapping settings from the per mapping |
304 | 356 |
* configuration form. |
305 | 357 |
*/ |
306 |
function my_module_set_target($source, $entity, $target, $value, $mapping) {
|
|
307 |
$entity->{$target}[$entity->language][0]['value'] = $value;
|
|
358 |
function my_module_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
|
|
359 |
$entity->{$target}[$entity->language][0]['value'] = reset($values);
|
|
308 | 360 |
if (isset($source->importer->processor->config['input_format'])) { |
309 |
$entity->{$target}[$entity->language][0]['format'] = |
|
310 |
$source->importer->processor->config['input_format']; |
|
361 |
$entity->{$target}[$entity->language][0]['format'] = $source->importer->processor->config['input_format']; |
|
311 | 362 |
} |
312 | 363 |
} |
313 | 364 |
|
314 | 365 |
/** |
315 |
* Example of the summary_callback specified in |
|
316 |
* hook_feeds_processor_targets_alter(). |
|
366 |
* Example of the summary_callback specified in hook_feeds_processor_targets(). |
|
317 | 367 |
* |
318 |
* @param $mapping |
|
368 |
* @param array $mapping
|
|
319 | 369 |
* Associative array of the mapping settings. |
320 |
* @param $target |
|
370 |
* @param string $target
|
|
321 | 371 |
* Array of target settings, as defined by the processor or |
322 | 372 |
* hook_feeds_processor_targets_alter(). |
323 |
* @param $form |
|
373 |
* @param array $form
|
|
324 | 374 |
* The whole mapping form. |
325 |
* @param $form_state |
|
375 |
* @param array $form_state
|
|
326 | 376 |
* The form state of the mapping form. |
327 | 377 |
* |
328 |
* @return |
|
378 |
* @return string
|
|
329 | 379 |
* Returns, as a string that may contain HTML, the summary to display while |
330 | 380 |
* the full form isn't visible. |
331 | 381 |
* If the return value is empty, no summary and no option to view the form |
332 | 382 |
* will be displayed. |
333 | 383 |
*/ |
334 |
function my_module_summary_callback($mapping, $target, $form, $form_state) {
|
|
384 |
function my_module_summary_callback(array $mapping, $target, array $form, array $form_state) {
|
|
335 | 385 |
if (empty($mapping['my_setting'])) { |
336 | 386 |
return t('My setting <strong>not</strong> active'); |
337 | 387 |
} |
... | ... | |
341 | 391 |
} |
342 | 392 |
|
343 | 393 |
/** |
344 |
* Example of the form_callback specified in |
|
345 |
* hook_feeds_processor_targets_alter(). |
|
394 |
* Example of the form_callback specified in hook_feeds_processor_targets(). |
|
346 | 395 |
* |
347 | 396 |
* The arguments are the same that my_module_summary_callback() gets. |
348 | 397 |
* |
349 |
* @return |
|
398 |
* @return array
|
|
350 | 399 |
* The per mapping configuration form. Once the form is saved, $mapping will |
351 | 400 |
* be populated with the form values. |
352 | 401 |
* |
353 | 402 |
* @see my_module_summary_callback() |
354 | 403 |
*/ |
355 |
function my_module_form_callback($mapping, $target, $form, $form_state) {
|
|
404 |
function my_module_form_callback(array $mapping, $target, array $form, array $form_state) {
|
|
356 | 405 |
return array( |
357 | 406 |
'my_setting' => array( |
358 | 407 |
'#type' => 'checkbox', |
... | ... | |
362 | 411 |
); |
363 | 412 |
} |
364 | 413 |
|
414 |
/** |
|
415 |
* Example of the unique_callbacks specified in hook_feeds_processor_targets(). |
|
416 |
* |
|
417 |
* @param FeedsSource $source |
|
418 |
* The Feed source. |
|
419 |
* @param string $entity_type |
|
420 |
* Entity type for the entity to be processed. |
|
421 |
* @param string $bundle |
|
422 |
* Bundle name for the entity to be processed. |
|
423 |
* @param string $target |
|
424 |
* A string identifying the unique target on the entity. |
|
425 |
* @param array $values |
|
426 |
* The unique values to be checked. |
|
427 |
* |
|
428 |
* @return int|null |
|
429 |
* The existing entity id, or NULL if no existing entity is found. |
|
430 |
* |
|
431 |
* @see hook_feeds_processor_targets() |
|
432 |
* @see FeedsProcessor::existingEntityId() |
|
433 |
*/ |
|
434 |
function my_module_mapper_unique(FeedsSource $source, $entity_type, $bundle, $target, array $values) { |
|
435 |
list($field_name, $column) = explode(':', $target . ':value'); |
|
436 |
// Example for if the target is a field. |
|
437 |
$query = new EntityFieldQuery(); |
|
438 |
$result = $query |
|
439 |
->entityCondition('entity_type', $entity_type) |
|
440 |
->entityCondition('bundle', $bundle) |
|
441 |
->fieldCondition($field_name, $column, $values) |
|
442 |
->execute(); |
|
443 |
|
|
444 |
if (!empty($result[$entity_type])) { |
|
445 |
return key($result[$entity_type]); |
|
446 |
} |
|
447 |
} |
|
448 |
|
|
449 |
/** |
|
450 |
* Example of the preprocess_callbacks specified in hook_feeds_processor_targets(). |
|
451 |
* |
|
452 |
* @param FeedsSource $source |
|
453 |
* The Feed source. |
|
454 |
* @param object $entity |
|
455 |
* The entity being processed. |
|
456 |
* @param array $target |
|
457 |
* The full target definition. |
|
458 |
* @param array &$mapping |
|
459 |
* The mapping configuration. |
|
460 |
* |
|
461 |
* @see hook_feeds_processor_targets() |
|
462 |
*/ |
|
463 |
function my_module_preprocess_callback(FeedsSource $source, $entity, array $target, array &$mapping) { |
|
464 |
// Add in default values. |
|
465 |
$mapping += array('setting_value' => TRUE); |
|
466 |
} |
|
467 |
|
|
365 | 468 |
/** |
366 | 469 |
* @} |
367 | 470 |
*/ |
Formats disponibles : Unified diff
Update feeds 7.x-2.0-alpha9 -> 7.x-2.0-beta1
Install lib simplepie 1.3.1