tripal.fields.api.inc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. function hook_bundle_fields_info($entity_type, $bundle) {
  32. }
  33. function hook_bundle_instances_info($entity_type, $bundle) {
  34. }
  35. function hook_bundle_fields_info_alter(&$info, $bundle, $term) {
  36. }
  37. function hook_bundle_instances_info_alter(&$info, $bundle, $term) {
  38. }
  39. /**
  40. * Retrieves a list of TripalField types.
  41. *
  42. * The TripalField classes can be added by a site developer and should be
  43. * placed in the [module]/includes/TripalFields directory. Tripal will support
  44. * any field as long as it is in this directory and extends the TripalField
  45. * class. To support dynamic inclusion of new fields this function
  46. * will look for TripalField class files and return a type for
  47. * each one.
  48. *
  49. * @return
  50. * A list of TripalField names.
  51. */
  52. function tripal_get_field_types() {
  53. $types = array();
  54. $modules = module_list(TRUE);
  55. foreach ($modules as $module) {
  56. // Only run this for modules that are enabled.
  57. if (!module_exists($module)) {
  58. continue;
  59. }
  60. // Find all of the files in the tripal_chado/includes/TripalFields/ directory.
  61. $fields_path = drupal_get_path('module', $module) . '/includes/TripalFields';
  62. $field_files = file_scan_directory($fields_path, '/.*\.inc$/');
  63. // Iterate through the fields, include the file and run the info function.
  64. foreach ($field_files as $file) {
  65. // Ignore the formatter and widget classes for now.
  66. if (preg_match('/_formatter|_widget/', $file->name)) {
  67. continue;
  68. }
  69. $field_type = $file->name;
  70. module_load_include('inc', $module, 'includes/TripalFields/' . $field_type . '/' . $field_type);
  71. if (class_exists($field_type) and is_subclass_of($field_type, 'TripalField')) {
  72. $types[] = $field_type;
  73. }
  74. }
  75. }
  76. return $types;
  77. }
  78. /**
  79. * Retrieves a list of TripalFieldWidgets.
  80. *
  81. * The TripalFieldWidget classes can be added by a site developer and should be
  82. * placed in the [module]/includes/TripalFields directory. Tripal will support
  83. * any widget as long as it is in this directory and extends the
  84. * TripalFieldWidget class.
  85. *
  86. * @return
  87. * A list of TripalFieldWidget names.
  88. */
  89. function tripal_get_field_widgets() {
  90. $widgets = array();
  91. $modules = module_list(TRUE);
  92. foreach ($modules as $module) {
  93. // Only run this for modules that are enabled.
  94. if (!module_exists($module)) {
  95. continue;
  96. }
  97. // Find all of the files in the tripal_chado/includes/fields directory.
  98. $fields_path = drupal_get_path('module', $module) . '/includes/TripalFields';
  99. $field_files = file_scan_directory($fields_path, '/.*_widget\.inc$/');
  100. // Iterate through the fields, include the file and run the info function.
  101. foreach ($field_files as $file) {
  102. $widget_type = $file->name;
  103. $field_type = preg_replace('/(^.*)_widget/', '$1', $widget_type);
  104. module_load_include('inc', $module, 'includes/TripalFields/' . $field_type . '/' . $widget_type);
  105. if (class_exists($widget_type) and is_subclass_of($widget_type, 'TripalFieldWidget')) {
  106. $widgets[] = $widget_type;
  107. }
  108. }
  109. }
  110. return $widgets;
  111. }
  112. /**
  113. * Retrieves a list of TripalFieldFormatters.
  114. *
  115. * The TripalFieldFormatter classes can be added by a site developer and should
  116. * be placed in the [module]/includes/TripalFields directory. Tripal will
  117. * support any widget as long as it is in this directory and extends the
  118. * TripalFieldFormatter class.
  119. *
  120. * @return
  121. * A list of TripalFieldFormatter names.
  122. */
  123. function tripal_get_field_formatters() {
  124. $formatters = array();
  125. $modules = module_list(TRUE);
  126. foreach ($modules as $module) {
  127. // Only run this for modules that are enabled.
  128. if (!module_exists($module)) {
  129. continue;
  130. }
  131. // Find all of the files in the tripal_chado/includes/fields directory.
  132. $fields_path = drupal_get_path('module', $module) . '/includes/TripalFields';
  133. $field_files = file_scan_directory($fields_path, '/.*_formatter\.inc$/');
  134. // Iterate through the fields, include the file and run the info function.
  135. foreach ($field_files as $file) {
  136. $formatter_type = $file->name;
  137. $field_type = preg_replace('/(^.*)_formatter/', '$1', $formatter_type);
  138. module_load_include('inc', $module, 'includes/TripalFields/' . $field_type . '/' . $formatter_type);
  139. if (class_exists($formatter_type) and is_subclass_of($formatter_type, 'TripalFieldFormatter')) {
  140. $formatters[] = $formatter_type;
  141. }
  142. }
  143. }
  144. return $formatters;
  145. }
  146. /**
  147. * Loads the TripalField class file into scope.
  148. *
  149. * @param $class
  150. * The class to include. This can be a TripalField, TripalFieldWidget or
  151. * TripalFieldFormatter class name.
  152. *
  153. * @return
  154. * TRUE if the field type class file was found, FALSE otherwise.
  155. */
  156. function tripal_load_include_field_class($class) {
  157. $modules = module_list(TRUE);
  158. foreach ($modules as $module) {
  159. $field_type = preg_replace('/(^.*)_(formatter|widget)/', '$1', $class);
  160. $file_path = realpath(".") . '/' . drupal_get_path('module', $module) . '/includes/TripalFields/' . $field_type . '/' . $class . '.inc';
  161. if (file_exists($file_path)) {
  162. module_load_include('inc', $module, 'includes/TripalFields/' . $field_type . '/' . $class);
  163. if (class_exists($class)) {
  164. return TRUE;
  165. }
  166. }
  167. }
  168. return FALSE;
  169. }
  170. /**
  171. * More easily get the value of a single item from a field's items array.
  172. *
  173. * A Drupal field attached to an entity may have multiple items (i.e. it has
  174. * a cardinality > 1). Each of these items will always have a 'value' key that
  175. * contains the data for that field. However, fields can also have other keys
  176. * with their own values. You can easily retreive the value of these keys
  177. * using this function. What makes this function useful is that you can
  178. * provide a default value to use if the key has no value. This is useful
  179. * when developing a TripalField::widgetForm function and you need to
  180. * retreive default values and you don't want to have to always check if the
  181. * value is set.
  182. *
  183. * @param $items
  184. * The fields $items array. Compatbile with that returned by field_get_items.
  185. * @param $delta
  186. * The index of the item.
  187. * @parm $key
  188. * The name of the key within the item.
  189. * @param $default
  190. * A default value to return if the key is not set. By default the empty
  191. * string is returned.
  192. *
  193. * @return
  194. * The value assigned to the item's key; FALSE if the key doesn't exist or
  195. * the $default argument if no value is associated with the key.
  196. */
  197. function tripal_get_field_item_keyval($items, $delta, $key, $default='') {
  198. if (!array_key_exists($delta, $items)) {
  199. return FALSE;
  200. }
  201. if (!array_key_exists($key, $items[$delta])) {
  202. return FALSE;
  203. }
  204. if (!$items[$delta][$key]) {
  205. return $default;
  206. }
  207. return $items[$delta][$key];
  208. }