feeds.api.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 $source
  84. * FeedsSource object that describes the source that has been imported.
  85. * @param $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 item is saved.
  94. *
  95. * @param $source
  96. * FeedsSource object that describes the source that is being imported.
  97. * @param $entity
  98. * The entity object.
  99. * @param $item
  100. * The parser result for this entity.
  101. */
  102. function hook_feeds_presave(FeedsSource $source, $entity, $item) {
  103. if ($entity->feeds_item->entity_type == 'node') {
  104. // Skip saving this entity.
  105. $entity->feeds_item->skip = TRUE;
  106. }
  107. }
  108. /**
  109. * Invoked after a feed source has been imported.
  110. *
  111. * @param $source
  112. * FeedsSource object that describes the source that has been imported.
  113. */
  114. function hook_feeds_after_import(FeedsSource $source) {
  115. // See geotaxonomy module's implementation for an example.
  116. }
  117. /**
  118. * Invoked after a feed source has been cleared of its items.
  119. *
  120. * @param $source
  121. * FeedsSource object that describes the source that has been cleared.
  122. */
  123. function hook_feeds_after_clear(FeedsSource $source) {
  124. }
  125. /**
  126. * @}
  127. */
  128. /**
  129. * @defgroup mappingapi Mapping API
  130. * @{
  131. */
  132. /**
  133. * Alter mapping sources.
  134. *
  135. * Use this hook to add additional mapping sources for any parser. Allows for
  136. * registering a callback to be invoked at mapping time.
  137. *
  138. * @see my_source_get_source().
  139. * @see locale_feeds_parser_sources_alter().
  140. */
  141. function hook_feeds_parser_sources_alter(&$sources, $content_type) {
  142. $sources['my_source'] = array(
  143. 'name' => t('Images in description element'),
  144. 'description' => t('Images occuring in the description element of a feed item.'),
  145. 'callback' => 'my_source_get_source',
  146. );
  147. }
  148. /**
  149. * Example callback specified in hook_feeds_parser_sources_alter().
  150. *
  151. * To be invoked on mapping time.
  152. *
  153. * @param $source
  154. * The FeedsSource object being imported.
  155. * @param $result
  156. * The FeedsParserResult object being mapped from.
  157. * @param $key
  158. * The key specified in the $sources array in
  159. * hook_feeds_parser_sources_alter().
  160. *
  161. * @return
  162. * The value to be extracted from the source.
  163. *
  164. * @see hook_feeds_parser_sources_alter().
  165. * @see locale_feeds_get_source().
  166. */
  167. function my_source_get_source($source, FeedsParserResult $result, $key) {
  168. $item = $result->currentItem();
  169. return my_source_parse_images($item['description']);
  170. }
  171. /**
  172. * Alter mapping targets for entities. Use this hook to add additional target
  173. * options to the mapping form of Node processors.
  174. *
  175. * If the key in $targets[] does not correspond to the actual key on the node
  176. * object ($node->key), real_target MUST be specified. See mappers/link.inc
  177. *
  178. * For an example implementation, see mappers/content.inc
  179. *
  180. * @param &$targets
  181. * Array containing the targets to be offered to the user. Add to this array
  182. * to expose additional options. Remove from this array to suppress options.
  183. * Remove with caution.
  184. * @param $entity_type
  185. * The entity type of the target, for instance a 'node' entity.
  186. * @param $bundle_name
  187. * The bundle name for which to alter targets.
  188. */
  189. function hook_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
  190. if ($entity_type == 'node') {
  191. $targets['my_node_field'] = array(
  192. 'name' => t('My custom node field'),
  193. 'description' => t('Description of what my custom node field does.'),
  194. 'callback' => 'my_module_set_target',
  195. // Specify both summary_callback and form_callback to add a per mapping
  196. // configuration form.
  197. 'summary_callback' => 'my_module_summary_callback',
  198. 'form_callback' => 'my_module_form_callback',
  199. );
  200. $targets['my_node_field2'] = array(
  201. 'name' => t('My Second custom node field'),
  202. 'description' => t('Description of what my second custom node field does.'),
  203. 'callback' => 'my_module_set_target2',
  204. 'real_target' => 'my_node_field_two', // Specify real target field on node.
  205. );
  206. }
  207. }
  208. /**
  209. * Example callback specified in hook_feeds_processor_targets_alter().
  210. *
  211. * @param $source
  212. * Field mapper source settings.
  213. * @param $entity
  214. * An entity object, for instance a node object.
  215. * @param $target
  216. * A string identifying the target on the node.
  217. * @param $value
  218. * The value to populate the target with.
  219. * @param $mapping
  220. * Associative array of the mapping settings from the per mapping
  221. * configuration form.
  222. */
  223. function my_module_set_target($source, $entity, $target, $value, $mapping) {
  224. $entity->{$target}[$entity->language][0]['value'] = $value;
  225. if (isset($source->importer->processor->config['input_format'])) {
  226. $entity->{$target}[$entity->language][0]['format'] =
  227. $source->importer->processor->config['input_format'];
  228. }
  229. }
  230. /**
  231. * Example of the summary_callback specified in
  232. * hook_feeds_processor_targets_alter().
  233. *
  234. * @param $mapping
  235. * Associative array of the mapping settings.
  236. * @param $target
  237. * Array of target settings, as defined by the processor or
  238. * hook_feeds_processor_targets_alter().
  239. * @param $form
  240. * The whole mapping form.
  241. * @param $form_state
  242. * The form state of the mapping form.
  243. *
  244. * @return
  245. * Returns, as a string that may contain HTML, the summary to display while
  246. * the full form isn't visible.
  247. * If the return value is empty, no summary and no option to view the form
  248. * will be displayed.
  249. */
  250. function my_module_summary_callback($mapping, $target, $form, $form_state) {
  251. if (empty($mapping['my_setting'])) {
  252. return t('My setting <strong>not</strong> active');
  253. }
  254. else {
  255. return t('My setting <strong>active</strong>');
  256. }
  257. }
  258. /**
  259. * Example of the form_callback specified in
  260. * hook_feeds_processor_targets_alter().
  261. *
  262. * The arguments are the same that my_module_summary_callback() gets.
  263. *
  264. * @see my_module_summary_callback()
  265. *
  266. * @return
  267. * The per mapping configuration form. Once the form is saved, $mapping will
  268. * be populated with the form values.
  269. */
  270. function my_module_form_callback($mapping, $target, $form, $form_state) {
  271. return array(
  272. 'my_setting' => array(
  273. '#type' => 'checkbox',
  274. '#title' => t('My setting checkbox'),
  275. '#default_value' => !empty($mapping['my_setting']),
  276. ),
  277. );
  278. }
  279. /**
  280. * @}
  281. */