tripal.fields.api.inc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Executes a TripalFieldQuery using the provided conditions.
  4. *
  5. * This hook is called to find the entities having certain field
  6. * conditions and sort them in the given field order.
  7. *
  8. * @param $conditions
  9. * An array of filter representing the conditions to be applied to the query.
  10. * Each filter is an associative array whose keys include the following:
  11. * - field: an array representing the field identical to the output of the
  12. * field_info_field() function.
  13. * - filter: the name of the field on which the filter should be applied.
  14. * - value: the value of the filter.
  15. * - operator: the operation to apply: '=', '<>', '>', '>=', '<', '<=',
  16. * 'STARTS_WITH', 'CONTAINS': These operators expect $value to be a
  17. * literal of the same type as the column. 'IN', 'NOT IN': These operators
  18. * expect $value to be an array of literals of the same type as the column.
  19. * @param $orderBy
  20. * An array of sorting instructions. Each sort is an associative array with
  21. * the following keys:
  22. * - field: an array representing the field identical to the output of the
  23. * field_info_field() function.
  24. * - orderBy: the name of the field on which the filter should be applied.
  25. * - direction: either the string 'ASC' (for ascending sort) or 'DESC' (for
  26. * descending).
  27. */
  28. function hook_field_storage_tquery($conditions, $orderBy) {
  29. // See the tripal_chado_field_storage_tquery() function for an example.
  30. }
  31. /**
  32. * Retrieves a list of TripalField types.
  33. *
  34. * The TripalField classes can be added by a site developer and should be
  35. * placed in the [module]/includes/TripalFields directory. Tripal will support
  36. * any field as long as it is in this directory and extends the TripalField
  37. * class. To support dynamic inclusion of new fields this function
  38. * will look for TripalField class files and return a type for
  39. * each one.
  40. *
  41. * @return
  42. * A list of TripalField names.
  43. */
  44. function tripal_get_field_types() {
  45. $types = array();
  46. $modules = module_list(TRUE);
  47. foreach ($modules as $module) {
  48. // Find all of the files in the tripal_chado/includes/fields directory.
  49. $fields_path = drupal_get_path('module', $module) . '/includes/TripalFields';
  50. $field_files = file_scan_directory($fields_path, '/.*\.inc$/');
  51. // Iterate through the fields, include the file and run the info function.
  52. foreach ($field_files as $file) {
  53. $field_type = $file->name;
  54. module_load_include('inc', $module, 'includes/TripalFields/' . $field_type);
  55. if (class_exists($field_type) and is_subclass_of($field_type, 'TripalField')) {
  56. $types[] = $field_type;
  57. }
  58. }
  59. }
  60. return $types;
  61. }
  62. /**
  63. * Retrieves a list of TripalFieldWidgets.
  64. *
  65. * The TripalFieldWidget classes can be added by a site developer and should be
  66. * placed in the [module]/includes/TripalFields directory. Tripal will support
  67. * any widget as long as it is in this directory and extends the
  68. * TripalFieldWidget class.
  69. *
  70. * @return
  71. * A list of TripalFieldWidget names.
  72. */
  73. function tripal_get_field_widgets() {
  74. $widgets = array();
  75. $modules = module_list(TRUE);
  76. foreach ($modules as $module) {
  77. // Find all of the files in the tripal_chado/includes/fields directory.
  78. $fields_path = drupal_get_path('module', $module) . '/includes/TripalFields';
  79. $field_files = file_scan_directory($fields_path, '/.*\.inc$/');
  80. // Iterate through the fields, include the file and run the info function.
  81. foreach ($field_files as $file) {
  82. $field_type = $file->name;
  83. module_load_include('inc', $module, 'includes/TripalFields/' . $field_type);
  84. if (class_exists($field_type) and is_subclass_of($field_type, 'TripalFieldWidget')) {
  85. $widgets[] = $field_type;
  86. }
  87. }
  88. }
  89. return $widgets;
  90. }
  91. /**
  92. * Retrieves a list of TripalFieldFormatters.
  93. *
  94. * The TripalFieldFormatter classes can be added by a site developer and should
  95. * be placed in the [module]/includes/TripalFields directory. Tripal will
  96. * support any widget as long as it is in this directory and extends the
  97. * TripalFieldFormatter class.
  98. *
  99. * @return
  100. * A list of TripalFieldFormatter names.
  101. */
  102. function tripal_get_field_formatters() {
  103. $formatters = array();
  104. $modules = module_list(TRUE);
  105. foreach ($modules as $module) {
  106. // Find all of the files in the tripal_chado/includes/fields directory.
  107. $fields_path = drupal_get_path('module', $module) . '/includes/TripalFields';
  108. $field_files = file_scan_directory($fields_path, '/.*\.inc$/');
  109. // Iterate through the fields, include the file and run the info function.
  110. foreach ($field_files as $file) {
  111. $field_type = $file->name;
  112. module_load_include('inc', $module, 'includes/TripalFields/' . $field_type);
  113. if (class_exists($field_type) and is_subclass_of($field_type, 'TripalFieldFormatter')) {
  114. $formatters[] = $field_type;
  115. }
  116. }
  117. }
  118. return $formatters;
  119. }
  120. /**
  121. * Loads the TripalField class file into scope.
  122. *
  123. * @param $class
  124. * The class to instantiate
  125. *
  126. * @return
  127. * TRUE if the field type class file was found, FALSE otherwise.
  128. */
  129. function tripal_load_include_field_class($class) {
  130. $modules = module_list(TRUE);
  131. foreach ($modules as $module) {
  132. $file_path = realpath(".") . '/' . drupal_get_path('module', $module) . '/includes/TripalFields/' . $class . '.inc';
  133. if (file_exists($file_path)) {
  134. module_load_include('inc', $module, 'includes/TripalFields/' . $class);
  135. if (class_exists($class)) {
  136. return TRUE;
  137. }
  138. }
  139. }
  140. return FALSE;
  141. }
  142. /**
  143. * More easily get the value of a single item from a field's items array.
  144. *
  145. * A Drupal field attached to an entity may have multiple items (i.e. it has
  146. * a cardinality > 1). Each of these items will always have a 'value' key that
  147. * contains the data for that field. However, fields can also have other keys
  148. * with their own values. You can easily retreive the value of these keys
  149. * using this function. What makes this function useful is that you can
  150. * provide a default value to use if the key has no value. This is useful
  151. * when developing a TripalField::widgetForm function and you need to
  152. * retreive default values and you don't want to have to always check if the
  153. * value is set.
  154. *
  155. * @param $items
  156. * The fields $items array. Compatbile with that returned by field_get_items.
  157. * @param $delta
  158. * The index of the item.
  159. * @parm $key
  160. * The name of the key within the item.
  161. * @param $default
  162. * A default value to return if the key is not set. By default the empty
  163. * string is returned.
  164. *
  165. * @return
  166. * The value assigned to the item's key; FALSE if the key doesn't exist or
  167. * the $default argument if no value is associated with the key.
  168. */
  169. function tripal_get_field_item_keyval($items, $delta, $key, $default='') {
  170. if (!array_key_exists($delta, $items)) {
  171. return FALSE;
  172. }
  173. if (!array_key_exists($key, $items[$delta])) {
  174. return FALSE;
  175. }
  176. if (!$items[$delta][$key]) {
  177. return $default;
  178. }
  179. return $items[$delta][$key];
  180. }