feeds_tests.module

Tracking 7.x-2.x branch
  1. drupal
    1. 7 contributions/feeds/tests/feeds_tests.module

Helper module for Feeds tests.

Functions & methods

NameDescription
feeds_tests_feeds_processor_targets_alterImplements hook_feeds_processor_targets_alter()
feeds_tests_filesOutputs a CSV file pointing to files.
feeds_tests_flickrOutputs flickr test feed.
feeds_tests_mapper_form
feeds_tests_mapper_set_targetSet target value on entity.
feeds_tests_mapper_summaryProvides setting summary for the mapper.
feeds_tests_menuImplements hook_menu().
feeds_tests_themeImplements hook_theme().

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Helper module for Feeds tests.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function feeds_tests_menu() {
  10. $items['testing/feeds/flickr.xml'] = array(
  11. 'page callback' => 'feeds_tests_flickr',
  12. 'access arguments' => array('access content'),
  13. 'type' => MENU_CALLBACK,
  14. );
  15. $items['testing/feeds/files.csv'] = array(
  16. 'page callback' => 'feeds_tests_files',
  17. 'access arguments' => array('access content'),
  18. 'type' => MENU_CALLBACK,
  19. );
  20. return $items;
  21. }
  22. /**
  23. * Implements hook_theme().
  24. */
  25. function feeds_tests_theme() {
  26. return array(
  27. 'feeds_tests_flickr' => array(
  28. 'variables' => array('image_urls' => array()),
  29. 'path' => drupal_get_path('module', 'feeds_tests') . '/feeds',
  30. 'template' => 'feeds-tests-flickr',
  31. ),
  32. 'feeds_tests_files' => array(
  33. 'variables' => array('files' => array()),
  34. 'path' => drupal_get_path('module', 'feeds_tests') . '/feeds',
  35. 'template' => 'feeds-tests-files',
  36. ),
  37. );
  38. }
  39. /**
  40. * Outputs flickr test feed.
  41. */
  42. function feeds_tests_flickr() {
  43. $images = array(
  44. 0 => "tubing.jpeg",
  45. 1 => "foosball.jpeg",
  46. 2 => "attersee.jpeg",
  47. 3 => "hstreet.jpeg",
  48. 4 => "la fayette.jpeg",
  49. );
  50. $path = drupal_get_path('module', 'feeds_tests') . '/feeds/assets';
  51. foreach ($images as &$image) {
  52. $image = url("$path/$image", array('absolute' => TRUE));
  53. }
  54. drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8');
  55. print theme('feeds_tests_flickr', array('image_urls' => $images));
  56. }
  57. /**
  58. * Outputs a CSV file pointing to files.
  59. */
  60. function feeds_tests_files() {
  61. $images = array(
  62. 0 => "tubing.jpeg",
  63. 1 => "foosball.jpeg",
  64. 2 => "attersee.jpeg",
  65. 3 => "hstreet.jpeg",
  66. 4 => "la fayette.jpeg",
  67. );
  68. foreach ($images as &$image) {
  69. $image = "public://images/$image";
  70. }
  71. drupal_add_http_header('Content-Type', 'text/plain; charset=utf-8');
  72. print theme('feeds_tests_files', array('files' => $images));
  73. }
  74. /**
  75. * Implements hook_feeds_processor_targets_alter()
  76. */
  77. function feeds_tests_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
  78. $targets['test_target'] = array(
  79. 'name' => t('Test Target'),
  80. 'description' => t('This is a test target.'),
  81. 'callback' => 'feeds_tests_mapper_set_target',
  82. 'summary_callback' => 'feeds_tests_mapper_summary',
  83. 'form_callback' => 'feeds_tests_mapper_form',
  84. );
  85. }
  86. /**
  87. * Set target value on entity.
  88. *
  89. * @see my_module_set_target()
  90. */
  91. function feeds_tests_mapper_set_target($source, $entity, $target, $value, $mapping) {
  92. $entity->body['und'][0]['value'] = serialize($mapping);
  93. }
  94. /**
  95. * Provides setting summary for the mapper.
  96. *
  97. * @see my_module_summary_callback()
  98. */
  99. function feeds_tests_mapper_summary($mapping, $target, $form, $form_state) {
  100. $options = array(
  101. 'option1' => t('Option 1'),
  102. 'option2' => t('Another Option'),
  103. 'option3' => t('Option for select'),
  104. 'option4' => t('Another One')
  105. );
  106. $items = array();
  107. if (!empty($mapping['checkbox']) && $mapping['checkbox']) {
  108. $items[] = t('Checkbox active.');
  109. }
  110. else {
  111. $items[] = t('Checkbox inactive.');
  112. }
  113. if (!empty($mapping['textfield'])) {
  114. $items[] = t('<strong>Textfield value</strong>: %textfield', array('%textfield' => $mapping['textfield']));
  115. }
  116. if (!empty($mapping['textarea'])) {
  117. $items[] = t('<strong>Textarea value</strong>: %textarea', array('%textarea' => $mapping['textarea']));
  118. }
  119. if (!empty($mapping['radios'])) {
  120. $items[] = t('<strong>Radios value</strong>: %radios', array('%radios' => $options[$mapping['radios']]));
  121. }
  122. if (!empty($mapping['select'])) {
  123. $items[] = t('<strong>Select value</strong>: %select', array('%select' => $options[$mapping['select']]));
  124. }
  125. $list = array(
  126. '#type' => 'ul',
  127. '#theme' => 'item_list',
  128. '#items' => $items,
  129. );
  130. return drupal_render($list);
  131. }
  132. /*
  133. * Provides the form with mapper settings.
  134. */
  135. function feeds_tests_mapper_form($mapping, $target, $form, $form_state) {
  136. $mapping += array(
  137. 'checkbox' => FALSE,
  138. 'textfield' => '',
  139. 'textarea' => '',
  140. 'radios' => NULL,
  141. 'select' => NULL,
  142. );
  143. return array(
  144. 'checkbox' => array(
  145. '#type' => 'checkbox',
  146. '#title' => t('A checkbox'),
  147. '#default_value' => !empty($mapping['checkbox']),
  148. ),
  149. 'textfield' => array(
  150. '#type' => 'textfield',
  151. '#title' => t('A text field'),
  152. '#default_value' => $mapping['textfield'],
  153. '#required' => TRUE,
  154. ),
  155. 'textarea' => array(
  156. '#type' => 'textarea',
  157. '#title' => t('A textarea'),
  158. '#default_value' => $mapping['textarea'],
  159. ),
  160. 'radios' => array(
  161. '#type' => 'radios',
  162. '#title' => t('Some radios'),
  163. '#options' => array('option1' => t('Option 1'), 'option2' => t('Another Option')),
  164. '#default_value' => $mapping['radios'],
  165. ),
  166. 'select' => array(
  167. '#type' => 'select',
  168. '#title' => t('A select list'),
  169. '#options' => array('option3' => t('Option for select'), 'option4' => t('Another One')),
  170. '#default_value' => $mapping['select'],
  171. ),
  172. );
  173. }