tripal.fields.api.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) for working with
  5. * fields attached to TripalEntity content types (bundles).
  6. *
  7. */
  8. /**
  9. * @defgroup tripal_fields_api Tripal Fields
  10. * @ingroup tripal_api
  11. * @{
  12. * Provides an application programming interface (API) for working with
  13. * fields attached to TripalEntity content types (bundles).
  14. *
  15. * Fields:
  16. * A field is a reusable "data container" that is attached to a Bundle.
  17. * Programmatically, each field provides one or more primitive data types, with
  18. * validators and widgets for editing and formatters for display. Each field
  19. * independently manages the data to which it assigned. Just like with Bundles,
  20. * Fields are also described using controlled vocabulary terms. For example, a
  21. * gene Bundle has a field attached that provides the name of the gene.
  22. * This field only provides the name and nothing more. Tripal uses the
  23. * schema:name vocabulary term to describe the field.
  24. *
  25. * Field Instances:
  26. * Fields describe "atomic" units of data that are associated with an entity.
  27. * For example, a "name" is an atomic unit of data about a Gene or Organism
  28. * entity. Fields can be reused for multiple Bundles. For example, gene, mRNA,
  29. * genetic markers and variants all have name data. Despite that all of these
  30. * Bundles provides a "name", we only need one field to describe that this data
  31. * is a "name". However, we may want to customize a field specific to each
  32. * bundle. Therefore, an Instance of a field is attached to a bundle, and
  33. * field instances can then be customized differently. The most important
  34. * customization is the one that defines the Chado table from which the data
  35. * for a field is retrieved. Despite that field instances are attached to
  36. * bundles, they become visible with Entities. When an entity is loaded for
  37. * display, Drupal examines all of the fields that are attached to the entity's
  38. * bundle, and then populates the fields instances with data specific to the
  39. * entity being loaded.
  40. * @}
  41. *
  42. */
  43. /**
  44. * @section
  45. * Hooks.
  46. */
  47. /**
  48. * Executes a TripalFieldQuery using the provided conditions.
  49. *
  50. * This hook is called to find the entities having certain field
  51. * conditions and sort them in the given field order.
  52. *
  53. * @param $conditions
  54. * An array of filter representing the conditions to be applied to the query.
  55. * Each filter is an associative array whose keys include the following:
  56. * - field: an array representing the field identical to the output of the
  57. * field_info_field() function.
  58. * - filter: the name of the field on which the filter should be applied.
  59. * - value: the value of the filter.
  60. * - operator: the operation to apply: '=', '<>', '>', '>=', '<', '<=',
  61. * 'STARTS_WITH', 'CONTAINS': These operators expect $value to be a
  62. * literal of the same type as the column. 'IN', 'NOT IN': These operators
  63. * expect $value to be an array of literals of the same type as the column.
  64. * @param $orderBy
  65. * An array of sorting instructions. Each sort is an associative array with
  66. * the following keys:
  67. * - field: an array representing the field identical to the output of the
  68. * field_info_field() function.
  69. * - orderBy: the name of the field on which the filter should be applied.
  70. * - direction: either the string 'ASC' (for ascending sort) or 'DESC' (for
  71. * descending).
  72. *
  73. * @ingroup tripal_fields_api
  74. */
  75. function hook_field_storage_tquery($conditions, $orderBy) {
  76. // See the tripal_chado_field_storage_tquery() function for an example.
  77. }
  78. /**
  79. * Allows a module to return a bundles field info.
  80. *
  81. * @param $entity_type
  82. * The name of the entity, like 'TripalEntity'.
  83. * @param $bundle
  84. * The bundle object.
  85. *
  86. * @ingroup tripal_fields_api
  87. */
  88. function hook_bundle_fields_info($entity_type, $bundle) {
  89. }
  90. /**
  91. * Allows a module to return the field instances of a bundle.
  92. * @param $entity_type
  93. * The name of the entity, most likely 'TripalEntity'.
  94. * @param $bundle
  95. * The bundle object.
  96. *
  97. * @ingroup tripal_fields_api
  98. */
  99. function hook_bundle_instances_info($entity_type, $bundle) {
  100. }
  101. /**
  102. * This doesn't appear to be integrated anywhere?
  103. *
  104. * @param $info
  105. * @param $bundle
  106. * @param $term
  107. *
  108. * @ingroup tripal_fields_api
  109. */
  110. function hook_bundle_fields_info_alter(&$info, $bundle, $term) {
  111. }
  112. /**
  113. * This doesn't appear to be integrated anywhere?
  114. *
  115. * @param $info
  116. * @param $bundle
  117. * @param $term
  118. *
  119. * @ingroup tripal_fields_api
  120. */
  121. function hook_bundle_instances_info_alter(&$info, $bundle, $term) {
  122. }
  123. /**
  124. * Retrieves a list of TripalField types.
  125. *
  126. * The TripalField classes can be added by a site developer and should be
  127. * placed in the [module]/includes/TripalFields directory. Tripal will support
  128. * any field as long as it is in this directory and extends the TripalField
  129. * class. To support dynamic inclusion of new fields this function
  130. * will look for TripalField class files and return a type for
  131. * each one.
  132. *
  133. * @return
  134. * A list of TripalField names.
  135. *
  136. * @ingroup tripal_fields_api
  137. */
  138. function tripal_get_field_types() {
  139. $types = array();
  140. $modules = module_list(TRUE);
  141. foreach ($modules as $module) {
  142. // Only run this for modules that are enabled.
  143. if (!module_exists($module)) {
  144. continue;
  145. }
  146. // Find all of the files in the tripal_chado/includes/TripalFields/ directory.
  147. $fields_path = drupal_get_path('module', $module) . '/includes/TripalFields';
  148. $field_files = file_scan_directory($fields_path, '/.*\.inc$/');
  149. // Iterate through the fields, include the file and run the info function.
  150. foreach ($field_files as $file) {
  151. // Ignore the formatter and widget classes for now.
  152. if (preg_match('/_formatter|_widget/', $file->name)) {
  153. continue;
  154. }
  155. $field_type = $file->name;
  156. module_load_include('inc', $module, 'includes/TripalFields/' . $field_type . '/' . $field_type);
  157. if (class_exists($field_type) and is_subclass_of($field_type, 'TripalField')) {
  158. $types[] = $field_type;
  159. }
  160. }
  161. }
  162. // If the libraries module is enabled then we want to look for a TripalFields
  163. // library folder and see if our field exist there.
  164. if (module_exists('libraries')) {
  165. $library_path = libraries_get_path('TripalFields');
  166. $fields_path = realpath(".") . '/' . $library_path;
  167. $field_files = file_scan_directory($fields_path, '/.*\.inc$/');
  168. foreach ($field_files as $file) {
  169. // Ignore the formatter and widget classes for now.
  170. if (preg_match('/_formatter|_widget/', $file->name)) {
  171. continue;
  172. }
  173. $field_type = $file->name;
  174. $file_path = realpath(".") . '/' . $library_path .'/' . $field_type . '/' . $field_type . '.inc';
  175. if (file_exists($file_path)) {
  176. require_once($file_path);
  177. if (class_exists($field_type) and is_subclass_of($field_type, 'TripalField')) {
  178. $types[] = $field_type;
  179. }
  180. }
  181. }
  182. }
  183. return $types;
  184. }
  185. /**
  186. * Retrieves a list of TripalFieldWidgets.
  187. *
  188. * The TripalFieldWidget classes can be added by a site developer and should be
  189. * placed in the [module]/includes/TripalFields directory. Tripal will support
  190. * any widget as long as it is in this directory and extends the
  191. * TripalFieldWidget class.
  192. *
  193. * @return
  194. * A list of TripalFieldWidget names.
  195. *
  196. * @ingroup tripal_fields_api
  197. */
  198. function tripal_get_field_widgets() {
  199. $widgets = array();
  200. $modules = module_list(TRUE);
  201. foreach ($modules as $module) {
  202. // Only run this for modules that are enabled.
  203. if (!module_exists($module)) {
  204. continue;
  205. }
  206. // Find all of the files in the tripal_chado/includes/fields directory.
  207. $fields_path = drupal_get_path('module', $module) . '/includes/TripalFields';
  208. $field_files = file_scan_directory($fields_path, '/.*_widget\.inc$/');
  209. // Iterate through the fields, include the file and run the info function.
  210. foreach ($field_files as $file) {
  211. $widget_type = $file->name;
  212. $field_type = preg_replace('/(^.*)_widget/', '$1', $widget_type);
  213. module_load_include('inc', $module, 'includes/TripalFields/' . $field_type . '/' . $widget_type);
  214. if (class_exists($widget_type) and is_subclass_of($widget_type, 'TripalFieldWidget')) {
  215. $widgets[] = $widget_type;
  216. }
  217. }
  218. }
  219. // If the libraries module is enabled then we want to look for a TripalFields
  220. // library folder and see if our field exist there.
  221. if (module_exists('libraries')) {
  222. $library_path = libraries_get_path('TripalFields');
  223. $fields_path = realpath(".") . '/' . $library_path;
  224. $field_files = file_scan_directory($fields_path, '/.*_widget\.inc$/');
  225. foreach ($field_files as $file) {
  226. $widget_type = $file->name;
  227. $field_type = preg_replace('/(^.*)_widget/', '$1', $widget_type);
  228. $file_path = realpath(".") . '/' . $library_path .'/' . $field_type . '/' . $widget_type . '.inc';
  229. if (file_exists($file_path)) {
  230. require_once($file_path);
  231. if (class_exists($widget_type) and is_subclass_of($widget_type, 'TripalFieldWidget')) {
  232. $widgets[] = $widget_type;
  233. }
  234. }
  235. }
  236. }
  237. return $widgets;
  238. }
  239. /**
  240. * Retrieves a list of field formatters compatible with a given field.
  241. *
  242. * @param $field
  243. * A field array as returned by the field_info_field() function.
  244. * @param $instance
  245. * A field instance array.
  246. * @return
  247. * A list of file formatter class names.
  248. */
  249. function tripal_get_field_field_formatters($field, $instance) {
  250. $field_name = $field['field_name'];
  251. $field_type = $field['type'];
  252. $downloaders = array();
  253. // If the field type is a TripalField then get information about the formatter.
  254. if (tripal_load_include_field_class($field_type)) {
  255. $formatters = $field_type::$download_formatters;
  256. foreach ($formatters as $class_name) {
  257. if (!array_key_exists($class_name, $downloaders)) {
  258. tripal_load_include_downloader_class($class_name);
  259. $downloaders[$class_name] = $class_name::$full_label;
  260. }
  261. }
  262. }
  263. else {
  264. // For non TripalFields we'll assume TAB and CSV.
  265. tripal_load_include_downloader_class('TripalTabDownloader');
  266. tripal_load_include_downloader_class('TripalCSVDownloader');
  267. $downloaders['TripalTabDownloader'] = TripalTabDownloader::$full_label;
  268. $downloaders['TripalCSVDownloader'] = TripalCSVDownloader::$full_label;
  269. }
  270. return $downloaders;
  271. }
  272. /**
  273. * Retrieves a list of TripalFieldFormatters.
  274. *
  275. * The TripalFieldFormatter classes can be added by a site developer and should
  276. * be placed in the [module]/includes/TripalFields directory. Tripal will
  277. * support any widget as long as it is in this directory and extends the
  278. * TripalFieldFormatter class.
  279. *
  280. * @return
  281. * A list of TripalFieldFormatter names.
  282. *
  283. * @ingroup tripal_fields_api
  284. */
  285. function tripal_get_field_formatters() {
  286. $formatters = array();
  287. $modules = module_list(TRUE);
  288. foreach ($modules as $module) {
  289. // Only run this for modules that are enabled.
  290. if (!module_exists($module)) {
  291. continue;
  292. }
  293. // Find all of the files in the tripal_chado/includes/fields directory.
  294. $fields_path = drupal_get_path('module', $module) . '/includes/TripalFields';
  295. $field_files = file_scan_directory($fields_path, '/.*_formatter\.inc$/');
  296. // Iterate through the fields, include the file and run the info function.
  297. foreach ($field_files as $file) {
  298. $formatter_type = $file->name;
  299. $field_type = preg_replace('/(^.*)_formatter/', '$1', $formatter_type);
  300. module_load_include('inc', $module, 'includes/TripalFields/' . $field_type . '/' . $formatter_type);
  301. if (class_exists($formatter_type) and is_subclass_of($formatter_type, 'TripalFieldFormatter')) {
  302. $formatters[] = $formatter_type;
  303. }
  304. }
  305. }
  306. // If the libraries module is enabled then we want to look for a TripalFields
  307. // library folder and see if our field exist there.
  308. if (module_exists('libraries')) {
  309. $library_path = libraries_get_path('TripalFields');
  310. $fields_path = realpath(".") . '/' . $library_path;
  311. $field_files = file_scan_directory($fields_path, '/.*_formatter\.inc$/');
  312. foreach ($field_files as $file) {
  313. $formatter_type = $file->name;
  314. $field_type = preg_replace('/(^.*)_formatter/', '$1', $formatter_type);
  315. $file_path = realpath(".") . '/' . $library_path .'/' . $field_type . '/' . $formatter_type . '.inc';
  316. if (file_exists($file_path)) {
  317. require_once($file_path);
  318. if (class_exists($formatter_type) and is_subclass_of($formatter_type, 'TripalFieldFormatter')) {
  319. $formatters[] = $formatter_type;
  320. }
  321. }
  322. }
  323. }
  324. return $formatters;
  325. }
  326. /**
  327. * Loads the TripalField class file into scope.
  328. *
  329. * @param $class
  330. * The class to include. This can be a TripalField, TripalFieldWidget or
  331. * TripalFieldFormatter class name.
  332. *
  333. * @return
  334. * TRUE if the field type class file was found, FALSE otherwise.
  335. *
  336. * @ingroup tripal_fields_api
  337. */
  338. function tripal_load_include_field_class($class) {
  339. $modules = module_list(TRUE);
  340. foreach ($modules as $module) {
  341. $field_type = preg_replace('/(^.*)_(formatter|widget)/', '$1', $class);
  342. $file_path = realpath(".") . '/' . drupal_get_path('module', $module) . '/includes/TripalFields/' . $field_type . '/' . $class . '.inc';
  343. if (file_exists($file_path)) {
  344. module_load_include('inc', $module, 'includes/TripalFields/' . $field_type . '/' . $class);
  345. if (class_exists($class)) {
  346. return TRUE;
  347. }
  348. }
  349. }
  350. // If the libraries module is enabled then we want to look for a TripalFields
  351. // library folder and see if our field exist there.
  352. if (module_exists('libraries')) {
  353. $library_path = libraries_get_path('TripalFields');
  354. $field_type = preg_replace('/(^.*)_(formatter|widget)/', '$1', $class);
  355. $file_path = realpath(".") . '/' . $library_path .'/' . $field_type . '/' . $class . '.inc';
  356. if (file_exists($file_path)) {
  357. require_once($file_path);
  358. if (class_exists($class)) {
  359. return TRUE;
  360. }
  361. }
  362. }
  363. return FALSE;
  364. }
  365. /**
  366. * Loads the TripalEntityDownloader file into scope.
  367. *
  368. * @param $class
  369. * The name of the class to include.
  370. *
  371. * @return
  372. * TRUE if the downloader class file was found, FALSE otherwise.
  373. *
  374. * @ingroup tripal_fields_api
  375. */
  376. function tripal_load_include_downloader_class($class) {
  377. $modules = module_list(TRUE);
  378. foreach ($modules as $module) {
  379. $file_path = realpath(".") . '/' . drupal_get_path('module', $module) . '/includes/TripalFieldDownloaders/' . $class . '.inc';
  380. if (file_exists($file_path)) {
  381. module_load_include('inc', $module, 'includes/TripalFieldDownloaders/' . $class);
  382. if (class_exists($class)) {
  383. return TRUE;
  384. }
  385. }
  386. }
  387. // If the libraries module is enabled then we want to look for a
  388. // TripalFieldDownloader library folder and see if our field exist there.
  389. if (module_exists('libraries')) {
  390. $library_path = libraries_get_path('TripalFieldDownloaders');
  391. $file_path = realpath(".") . '/' . $library_path .'/' . $class . '.inc';
  392. if (file_exists($file_path)) {
  393. require_once($file_path);
  394. if (class_exists($class)) {
  395. return TRUE;
  396. }
  397. }
  398. }
  399. return FALSE;
  400. }
  401. /**
  402. * More easily get the value of a single item from a field's items array.
  403. *
  404. * A Drupal field attached to an entity may have multiple items (i.e. it has
  405. * a cardinality > 1). Each of these items will always have a 'value' key that
  406. * contains the data for that field. However, fields can also have other keys
  407. * with their own values. You can easily retreive the value of these keys
  408. * using this function. What makes this function useful is that you can
  409. * provide a default value to use if the key has no value. This is useful
  410. * when developing a TripalField::widgetForm function and you need to
  411. * retreive default values and you don't want to have to always check if the
  412. * value is set.
  413. *
  414. * @param $items
  415. * The fields $items array. Compatbile with that returned by field_get_items.
  416. * @param $delta
  417. * The index of the item.
  418. * @parm $key
  419. * The name of the key within the item.
  420. * @param $default
  421. * A default value to return if the key is not set. By default the empty
  422. * string is returned.
  423. *
  424. * @return
  425. * The value assigned to the item's key; FALSE if the key doesn't exist or
  426. * the $default argument if no value is associated with the key.
  427. *
  428. * @ingroup tripal_fields_api
  429. */
  430. function tripal_get_field_item_keyval($items, $delta, $key, $default='') {
  431. if (!array_key_exists($delta, $items)) {
  432. return FALSE;
  433. }
  434. if (!array_key_exists($key, $items[$delta])) {
  435. return FALSE;
  436. }
  437. if (!$items[$delta][$key]) {
  438. return $default;
  439. }
  440. return $items[$delta][$key];
  441. }
  442. /**
  443. * Formats an element of a TripalField for use by Drupal Views.
  444. *
  445. * Sometimes the value of TripalField can be more than just a single scalar. In
  446. * this case the value is an array of key value pairs where each key is a
  447. * controlled vocabulary term. In order to support fields, filtering and
  448. * sorting by these sub elements using Drupal Views, the TripalField
  449. * implementation must provide some help to Views by describing these elements,
  450. * and then implementing a query() function to support them. However, the
  451. * naming of sub elements must follow a set convention. This function
  452. * guarantees proper naming for sub elements.
  453. *
  454. * @param $field_name
  455. * The name of the field to which the element belongs.
  456. * @param $term
  457. * The term object as returned by tripal_get_term_details();
  458. *
  459. * @ingroup tripal_fields_api
  460. */
  461. function tripal_format_views_field_element($field_name, $term) {
  462. $element_name = $term['vocabulary']['short_name'] . '__' . $term['accession'];
  463. return $field_name . '.' . $element_name;
  464. }