Projet

Général

Profil

Paste
Télécharger (11 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / feeds.api.php @ f0456308

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) {
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

    
193
/**
194
 * Invoked after a feed source has been cleared of its items.
195
 *
196
 * @param FeedsSource $source
197
 *  FeedsSource object that describes the source that has been cleared.
198
 */
199
function hook_feeds_after_clear(FeedsSource $source) {
200
}
201

    
202
/**
203
 * @}
204
 */
205

    
206
/**
207
 * @defgroup mappingapi Mapping API
208
 * @{
209
 */
210

    
211
/**
212
 * Alter mapping sources.
213
 *
214
 * Use this hook to add additional mapping sources for any parser. Allows for
215
 * registering a callback to be invoked at mapping time.
216
 *
217
 * @see my_source_get_source().
218
 * @see locale_feeds_parser_sources_alter().
219
 */
220
function hook_feeds_parser_sources_alter(&$sources, $content_type) {
221
  $sources['my_source'] = array(
222
    'name' => t('Images in description element'),
223
    'description' => t('Images occuring in the description element of a feed item.'),
224
    'callback' => 'my_source_get_source',
225
  );
226
}
227

    
228
/**
229
 * Example callback specified in hook_feeds_parser_sources_alter().
230
 *
231
 * To be invoked on mapping time.
232
 *
233
 * @param $source
234
 *   The FeedsSource object being imported.
235
 * @param $result
236
 *   The FeedsParserResult object being mapped from.
237
 * @param $key
238
 *   The key specified in the $sources array in
239
 *   hook_feeds_parser_sources_alter().
240
 *
241
 * @return
242
 *   The value to be extracted from the source.
243
 *
244
 * @see hook_feeds_parser_sources_alter()
245
 * @see locale_feeds_get_source()
246
 */
247
function my_source_get_source(FeedsSource $source, FeedsParserResult $result, $key) {
248
  $item = $result->currentItem();
249
  return my_source_parse_images($item['description']);
250
}
251

    
252
/**
253
 * Alter mapping targets for entities. Use this hook to add additional target
254
 * options to the mapping form of Node processors.
255
 *
256
 * If the key in $targets[] does not correspond to the actual key on the node
257
 * object ($node->key), real_target MUST be specified. See mappers/link.inc
258
 *
259
 * For an example implementation, see mappers/content.inc
260
 *
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
266
 *   The entity type of the target, for instance a 'node' entity.
267
 * @param $bundle_name
268
 *   The bundle name for which to alter targets.
269
 */
270
function hook_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
271
  if ($entity_type == 'node') {
272
    $targets['my_node_field'] = array(
273
      'name' => t('My custom node field'),
274
      'description' => t('Description of what my custom node field does.'),
275
      'callback' => 'my_module_set_target',
276

    
277
      // Specify both summary_callback and form_callback to add a per mapping
278
      // configuration form.
279
      'summary_callback' => 'my_module_summary_callback',
280
      'form_callback' => 'my_module_form_callback',
281
    );
282
    $targets['my_node_field2'] = array(
283
      'name' => t('My Second custom node field'),
284
      'description' => t('Description of what my second custom node field does.'),
285
      'callback' => 'my_module_set_target2',
286
      'real_target' => 'my_node_field_two', // Specify real target field on node.
287
    );
288
  }
289
}
290

    
291
/**
292
 * Example callback specified in hook_feeds_processor_targets_alter().
293
 *
294
 * @param $source
295
 *   Field mapper source settings.
296
 * @param $entity
297
 *   An entity object, for instance a node object.
298
 * @param $target
299
 *   A string identifying the target on the node.
300
 * @param $value
301
 *   The value to populate the target with.
302
 * @param $mapping
303
 *  Associative array of the mapping settings from the per mapping
304
 *  configuration form.
305
 */
306
function my_module_set_target($source, $entity, $target, $value, $mapping) {
307
  $entity->{$target}[$entity->language][0]['value'] = $value;
308
  if (isset($source->importer->processor->config['input_format'])) {
309
    $entity->{$target}[$entity->language][0]['format'] =
310
      $source->importer->processor->config['input_format'];
311
  }
312
}
313

    
314
/**
315
 * Example of the summary_callback specified in
316
 * hook_feeds_processor_targets_alter().
317
 *
318
 * @param $mapping
319
 *   Associative array of the mapping settings.
320
 * @param $target
321
 *   Array of target settings, as defined by the processor or
322
 *   hook_feeds_processor_targets_alter().
323
 * @param $form
324
 *   The whole mapping form.
325
 * @param $form_state
326
 *   The form state of the mapping form.
327
 *
328
 * @return
329
 *   Returns, as a string that may contain HTML, the summary to display while
330
 *   the full form isn't visible.
331
 *   If the return value is empty, no summary and no option to view the form
332
 *   will be displayed.
333
 */
334
function my_module_summary_callback($mapping, $target, $form, $form_state) {
335
  if (empty($mapping['my_setting'])) {
336
    return t('My setting <strong>not</strong> active');
337
  }
338
  else {
339
    return t('My setting <strong>active</strong>');
340
  }
341
}
342

    
343
/**
344
 * Example of the form_callback specified in
345
 * hook_feeds_processor_targets_alter().
346
 *
347
 * The arguments are the same that my_module_summary_callback() gets.
348
 *
349
 * @return
350
 *   The per mapping configuration form. Once the form is saved, $mapping will
351
 *   be populated with the form values.
352
 *
353
 * @see my_module_summary_callback()
354
 */
355
function my_module_form_callback($mapping, $target, $form, $form_state) {
356
  return array(
357
    'my_setting' => array(
358
      '#type' => 'checkbox',
359
      '#title' => t('My setting checkbox'),
360
      '#default_value' => !empty($mapping['my_setting']),
361
    ),
362
  );
363
}
364

    
365
/**
366
 * @}
367
 */