Projet

Général

Profil

Révision a192dc0b

Ajouté par Assos Assos il y a environ 8 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/feeds/feeds.api.php
227 227
function hook_feeds_parser_sources_alter(&$sources, $content_type) {
228 228
  $sources['my_source'] = array(
229 229
    'name' => t('Images in description element'),
230
    'description' => t('Images occuring in the description element of a feed item.'),
230
    'description' => t('Images occurring in the description element of a feed item.'),
231 231
    'callback' => 'my_source_get_source',
232 232
  );
233 233
}
......
273 273
 *   The entity bundle to return targets for.
274 274
 *
275 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.
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.
278 302
 */
279 303
function hook_feeds_processor_targets($entity_type, $bundle) {
280 304
  $targets = array();
281 305

  
282 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()
283 310
    $targets['my_node_field'] = array(
284 311
      'name' => t('My custom node field'),
285 312
      'description' => t('Description of what my custom node field does.'),
286 313
      'callback' => 'my_module_set_target',
287

  
288
      // Specify both summary_callback and form_callback to add a per mapping
289
      // configuration form.
290
      'summary_callbacks' => array('my_module_summary_callback'),
291
      'form_callbacks' => array('my_module_form_callback'),
292 314
    );
293
    $targets['my_node_field2'] = array(
294
      'name' => t('My Second custom node field'),
295
      'description' => t('Description of what my second custom node field does.'),
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.'),
296 325
      'callback' => 'my_module_set_target2',
297
      'real_target' => 'my_node_field_two', // Specify real target field on node.
326
      'real_target' => 'my_node_field2',
298 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()
299 333
    $targets['my_node_field3'] = array(
300 334
      'name' => t('My third custom node field'),
301
      'description' => t('Description of what my third custom node field does.'),
335
      'description' => t('A field that can be set as an unique target.'),
302 336
      '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 337
      'optional_unique' => TRUE,
308 338
      'unique_callbacks' => array('my_module_mapper_unique'),
339
    );
309 340

  
310
      // Preprocess callbacks are called before the actual callback allowing you
311
      // to prepare values on the entity or mapping array.
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',
312 361
      'preprocess_callbacks' => array('my_module_preprocess_callback'),
313 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
    );
314 374
  }
315 375

  
316 376
  return $targets;
......
332 392
 * @see hook_feeds_processor_targets()
333 393
 */
334 394
function hook_feeds_processor_targets_alter(array &$targets, $entity_type, $bundle) {
395
  // Example: set an existing target as optional unique.
335 396
  if ($entity_type == 'node' && $bundle == 'article') {
336 397
    if (isset($targets['nid'])) {
337 398
      $targets['nid']['unique_callbacks'][] = 'my_module_mapper_unique';
......
362 423
  }
363 424
}
364 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

  
365 447
/**
366 448
 * Example of the summary_callback specified in hook_feeds_processor_targets().
367 449
 *
......
390 472
  }
391 473
}
392 474

  
393
/**
394
 * Example of the form_callback specified in hook_feeds_processor_targets().
395
 *
396
 * The arguments are the same that my_module_summary_callback() gets.
397
 *
398
 * @return array
399
 *   The per mapping configuration form. Once the form is saved, $mapping will
400
 *   be populated with the form values.
401
 *
402
 * @see my_module_summary_callback()
403
 */
404
function my_module_form_callback(array $mapping, $target, array $form, array $form_state) {
405
  return array(
406
    'my_setting' => array(
407
      '#type' => 'checkbox',
408
      '#title' => t('My setting checkbox'),
409
      '#default_value' => !empty($mapping['my_setting']),
410
    ),
411
  );
412
}
413

  
414 475
/**
415 476
 * Example of the unique_callbacks specified in hook_feeds_processor_targets().
416 477
 *
......
449 510
/**
450 511
 * Example of the preprocess_callbacks specified in hook_feeds_processor_targets().
451 512
 *
452
 * @param FeedsSource $source
453
 *   The Feed source.
454
 * @param object $entity
455
 *   The entity being processed.
456 513
 * @param array $target
457 514
 *   The full target definition.
458 515
 * @param array &$mapping
......
460 517
 *
461 518
 * @see hook_feeds_processor_targets()
462 519
 */
463
function my_module_preprocess_callback(FeedsSource $source, $entity, array $target, array &$mapping) {
520
function my_module_preprocess_callback(array $target, array &$mapping) {
464 521
  // Add in default values.
465 522
  $mapping += array('setting_value' => TRUE);
466 523
}

Formats disponibles : Unified diff