feeds.api.php 18 KB

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