TripalField.inc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /**
  3. * A base class for all Fields supported by Tripal.
  4. *
  5. * This class provides all of the necessary functions for a TripalField field.
  6. * It helps simplify and unify the process of creating fields for Tripal. This
  7. * class simply defines the function prototypes. It is up to the class that
  8. * extends this class to implement the functions.
  9. *
  10. * Each module that creates new fields should use the normal Field API hooks
  11. * (e.g. hook_field_info(), hook_field_widget_form(), etc.) to instantiate the
  12. * appropriate TripalField class.
  13. *
  14. * Because of the way Drupal handles callbacks, form validate functions,
  15. * AJAX callbacks, validate functions and theme functions cannot be part of
  16. * the implementation of this class. Those functions should be added to
  17. * the bottom of the file where the child class is housed.
  18. *
  19. */
  20. class TripalField {
  21. /**
  22. * Provides information about this field.
  23. *
  24. * @return array
  25. * An associative array with key/value pairs compatible with those from
  26. * the hook_field_info() function of the Drupal Field API.
  27. */
  28. public function field_info() {
  29. return array(
  30. );
  31. }
  32. /**
  33. * Provides an array that allows Tripal to attach a field to an entity.
  34. *
  35. * @todo: This function needs better documentation.
  36. *
  37. */
  38. public function attach_info($entity_type, $bundle, $settings) {
  39. }
  40. /**
  41. * Provides information about the widget for this field.
  42. *
  43. * @return array
  44. * An associative array with key/value paris compatible with those from the
  45. * hook_field_widget_info() function of the Drupal Field API.
  46. */
  47. public function widget_info() {
  48. return array(
  49. );
  50. }
  51. /**
  52. * Provides information about the formatter for this field.
  53. *
  54. * @return
  55. * An associative array with key/value paris compatible with those from the
  56. * hook_field_formatter_info() function of the Drupal Field API.
  57. *
  58. */
  59. public function formatter_info() {
  60. return array(
  61. );
  62. }
  63. /**
  64. * Provides a summary of the formatter settings.
  65. *
  66. * On the 'Manage Display' page of the content type administration page,
  67. * fields are allowed to provide a settings form. This settings form can
  68. * be used to allow the site admin to define how the field should be
  69. * formatted. The settings are then available for the formatter()
  70. * function of this class. This function provides a text-based description
  71. * of the settings for the site developer to see. It appears on the manage
  72. * display page inline with the field. A field must always return a
  73. * value in this function if the settings form gear button is to appear.
  74. *
  75. * See the hook_field_formatter_settings_summary() function for more
  76. * information.
  77. *
  78. * @param $field
  79. * @param $instance
  80. * @param $view_mode
  81. *
  82. * @return string
  83. * A string that provides a very brief summary of the field settings
  84. * to the user.
  85. *
  86. */
  87. public function formatter_settings_summary($field, $instance, $view_mode) {
  88. }
  89. /**
  90. * Provides the field's setting form.
  91. *
  92. * The settings form appears on the 'Manage Display' page of the content
  93. * type administration page. This function provides the form that will
  94. * appear on that page.
  95. *
  96. * To add a validate function, please create a static function in the
  97. * implementing class, and indicate that this function should be used
  98. * in the form array that is returned by this function.
  99. *
  100. * This form will not be displayed if the formatter_settings_summary()
  101. * function does not return anything.
  102. *
  103. * @param $field
  104. * @param $instance
  105. * @param $view_mode
  106. * @param $form
  107. * @param $form_state
  108. *
  109. * @return
  110. * A Drupal Form array containing the settings form for this field.
  111. */
  112. public function formatter_settings_form($field, $instance,
  113. $view_mode, $form, &$form_state) {
  114. }
  115. /**
  116. * Provides the display for a field
  117. *
  118. * This function provides the display for a field when it is viewed on
  119. * the web page. The content returned by the formatter should only include
  120. * what is present in the $items[$delta]['values] array. This way, the
  121. * contents that are displayed on the page, via webservices and downloaded
  122. * into a CSV file will always be identical. The view need not show all
  123. * of the data in the 'values' array.
  124. *
  125. * @param $element
  126. * @param $entity_type
  127. * @param $entity
  128. * @param $field
  129. * @param $instance
  130. * @param $langcode
  131. * @param $items
  132. * @param $display
  133. *
  134. * @return
  135. * An element array compatible with that returned by the
  136. * hook_field_formatter_view() function.
  137. */
  138. public function formatter_view(&$element, $entity_type, $entity,
  139. $field, $instance, $langcode, $items, $display) {
  140. foreach($items as $delta => $item) {
  141. $element[$delta] = array(
  142. '#type' => 'markup',
  143. '#markup' => $item['value'],
  144. );
  145. }
  146. }
  147. /**
  148. * Provides the form for editing of this field.
  149. *
  150. * This form is diplayed when the user creates a new entity or edits an
  151. * existing entity. If the field is attached to the entity then the form
  152. * provided by this function will be displayed.
  153. *
  154. * At a minimum, the form must have a 'value' element. For Tripal, the
  155. * 'value' element of a field always corresponds to the value that is
  156. * presented to the end-user either directly on the page (with formatting)
  157. * or via web services, or some other mechanism. However, the 'value' is
  158. * sometimes not enough for a field. For example, the Tripal Chado module
  159. * maps fields to table columns and sometimes those columns are foreign keys
  160. * therefore, the Tripal Chado modules does not use the 'value' but adds
  161. * additional elements to help link records via FKs. But even in this case
  162. * the 'value' element must always be present in the returne form and in such
  163. * cases it's value should be set equal to that added in the 'load' function.
  164. *
  165. * @param $widget
  166. * @param $form
  167. * @param $form_state
  168. * @param $field
  169. * @param $instance
  170. * @param $langcode
  171. * @param $items
  172. * @param $delta
  173. * @param $element
  174. *
  175. * @return
  176. * A Drupal form. See the hook_field_widget_form() function for more information.
  177. */
  178. public function widget_form(&$widget, &$form, &$form_state, $field, $instance,
  179. $langcode, $items, $delta, $element) {
  180. $widget['value'] = array(
  181. '#type' => 'value',
  182. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  183. );
  184. }
  185. /**
  186. * Loads the field values from the underlying data store.
  187. *
  188. * @param $field
  189. * @param $entity
  190. * @param $details
  191. *
  192. * @return
  193. * An array of the following format:
  194. * $entity->{$field_name}['und'][0]['value'] = $value;
  195. * where:
  196. * - $entity is the enity object to which this field is attached.
  197. * - $field_name is the name of this field
  198. * - 'und' is the language code (in this case 'und' == undefined)
  199. * - 0 is the cardinality. Increment by 1 when more than one item is
  200. * available.
  201. * - 'value' is the key indicating the value of this field. It should
  202. * always be set. The value of the 'value' key will be the contents
  203. * used for web services and for downloadable content. The value
  204. * should be of the follow format types: 1) A single value (text,
  205. * numeric, etc.) 2) An array of key value pair. 3) If multiple entries
  206. * then cardinality should incremented and format types 1 and 2 should
  207. * be used for each item.
  208. * The array may contain as many other keys at the same level as 'value'
  209. * but those keys are for internal field use and are not considered the
  210. * value of the field.
  211. *
  212. *
  213. */
  214. public function load($field, $entity, $details) {
  215. }
  216. /**
  217. * Provides a form for the 'Field Settings' of the field management page.
  218. *
  219. * This is an optional hook function and is similar to the
  220. * hook_field_settings_form function().
  221. *
  222. * @param $field
  223. * The field structure being configured.
  224. * @param $instance
  225. * The instance structure being configured.
  226. * @param $has_data
  227. * TRUE if the field already has data, FALSE if not.
  228. */
  229. public function settings_form($field, $instance, $has_data) {
  230. $settings = $field['settings'];
  231. $element = array();
  232. // $element['semantic_web'] = array(
  233. // '#type' => 'textfield',
  234. // '#title' => 'Semantic Web',
  235. // '#description' => t('Each field must be associated with a term
  236. // from a controlled vocabulary. This allows computer programs to understand
  237. // the data provided on this site. Please be cautions changing these
  238. // values. Defaults are set by Tripal and sites that use the same
  239. // terms can exchange information.'),
  240. // '#collapsed' => TRUE,
  241. // '#collapsible' => TRUE,
  242. // '#tree' => TRUE,
  243. // );
  244. return $element;
  245. }
  246. /**
  247. * Describes this fields "data tables" to Views.
  248. *
  249. * This function is the equivalent of the hook_views_data() function of
  250. * the Drupal Views API. It provides the necessary details to allow
  251. * Views to integrate the field.
  252. *
  253. * @return
  254. * An associative array describing the data structure of the field.
  255. */
  256. public function views_data_alter(&$data, $field, $entity_info) {
  257. }
  258. }