tripal.fields.api.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. *
  93. * @param $entity_type
  94. * The name of the entity, most likely 'TripalEntity'.
  95. * @param $bundle
  96. * The bundle object.
  97. *
  98. * @ingroup tripal_fields_api
  99. */
  100. function hook_bundle_instances_info($entity_type, $bundle) {
  101. }
  102. /**
  103. * Indicate if a field has an empty value.
  104. *
  105. * By default, all field values are attached to an entity in the form
  106. * $entity->{field_name}[{language}][{delta}]. Typically a field will then
  107. * have a 'value' element: $entity->{field_name}[{language}][{delta}]['value']
  108. * and if that value is empty then the field is considered empty by Tripal.
  109. * By default the tripal_field_is_empty() function is used to check all
  110. * fields to see if they are empty. However, this hook can be implemented by
  111. * any module to override that behavior.
  112. *
  113. * @param $field
  114. * A field array.
  115. * @param $items
  116. * The array of items attached to entity.
  117. * @param $delta
  118. * The specific value to check. For fields with cardinality greater than
  119. * 1 then each value can be checked. Defaults to 0 indicating it will check
  120. * the first value.
  121. *
  122. * @return
  123. * TRUE if the field value is empty for the given delta, and FALSE if not
  124. * empty.
  125. *
  126. * @ingroup tripal_fields_api
  127. */
  128. function tripal_field_is_empty($field, $items, $delta = 0) {
  129. // If the $items argument is empty then return TRUE.
  130. if (!$items) {
  131. return TRUE;
  132. }
  133. // If the field is a tripal field storage API field and there
  134. // is no value field then the field is empty.
  135. if (array_key_exists('storage', $field) and
  136. array_key_exists('tripal_storage_api', $field['storage']['settings']) and
  137. !array_key_exists('value', $items[$delta])) {
  138. return TRUE;
  139. }
  140. // If there is a value field but there's nothing in it, the the field is
  141. // empty.
  142. if (array_key_exists($delta, $items) and
  143. array_key_exists('value', $items[$delta]) and
  144. empty($items[$delta]['value'])) {
  145. return TRUE;
  146. }
  147. // Otherwise, the field is not empty.
  148. return FALSE;
  149. }
  150. /**
  151. * Retrieves a list of all fields implementing Tripal Fields/Formatters/Widgets.
  152. *
  153. * The TripalField classes can be added by the site developer and should be
  154. * placed in the [module]/includes/TripalFields directory. Tripal will support
  155. * any field as long as:
  156. * - it's in the [modules]/includes/TripalFields directory,
  157. * - extends TripalField, TripalWidget or TripalFormatter,
  158. * - matches the [cvname]__[termname] convention.
  159. *
  160. * @return
  161. * An array of files containing Tripal fields, widgets or formatters where
  162. * each element has a name, filename, and uri. The URI is relative to your
  163. * drupal root.
  164. */
  165. function tripal_get_tripalfield_files() {
  166. $field_files = &drupal_static(__FUNCTION__);
  167. if (!isset($field_files)) {
  168. $field_files = [];
  169. // Check module directories.
  170. $modules = module_list(TRUE);
  171. foreach ($modules as $module) {
  172. // Only run this for modules that are enabled.
  173. if (!module_exists($module)) {
  174. continue;
  175. }
  176. // Find all of the files in the tripal_chado/includes/fields directory.
  177. $fields_path = drupal_get_path('module', $module) . '/includes/TripalFields';
  178. $tmp = file_scan_directory($fields_path, '/.*__.*\.inc$/');
  179. if (!empty($tmp)) {
  180. $field_files = array_merge($field_files, $tmp);
  181. }
  182. }
  183. // Check the library directory.
  184. if (module_exists('libraries')) {
  185. $library_path = libraries_get_path('TripalFields');
  186. $fields_path = $library_path;
  187. $tmp = file_scan_directory($fields_path, '/.*__.*\.inc$/');
  188. if (!empty($tmp)) {
  189. $field_files = array_merge($field_files, $tmp);
  190. }
  191. }
  192. }
  193. return $field_files;
  194. }
  195. /**
  196. * Retrieves a list of TripalField types.
  197. *
  198. * The TripalField classes can be added by a site developer and should be
  199. * placed in the [module]/includes/TripalFields directory. Tripal will support
  200. * any field as long as it is in this directory and extends the TripalField
  201. * class. To support dynamic inclusion of new fields this function
  202. * will look for TripalField class files and return a type for
  203. * each one.
  204. *
  205. * @return
  206. * A list of TripalField names.
  207. *
  208. * @ingroup tripal_fields_api
  209. */
  210. function tripal_get_field_types() {
  211. $types = [];
  212. $field_files = tripal_get_tripalfield_files();
  213. // Iterate through the fields, include the file and run the info function.
  214. foreach ($field_files as $file) {
  215. // Ignore the formatter and widget classes for now.
  216. if (preg_match('/_formatter|_widget/', $file->name)) {
  217. continue;
  218. }
  219. $field_type = $file->name;
  220. require_once($file->uri);
  221. if (class_exists($field_type) and is_subclass_of($field_type, 'TripalField')) {
  222. $types[] = $field_type;
  223. }
  224. }
  225. return $types;
  226. }
  227. /**
  228. * Retrieves a list of TripalFieldWidgets.
  229. *
  230. * The TripalFieldWidget classes can be added by a site developer and should be
  231. * placed in the [module]/includes/TripalFields directory. Tripal will support
  232. * any widget as long as it is in this directory and extends the
  233. * TripalFieldWidget class.
  234. *
  235. * @return
  236. * A list of TripalFieldWidget names.
  237. *
  238. * @ingroup tripal_fields_api
  239. */
  240. function tripal_get_field_widgets() {
  241. $widgets = [];
  242. $field_files = tripal_get_tripalfield_files();
  243. // Iterate through the fields, include the file and run the info function.
  244. foreach ($field_files as $file) {
  245. if (preg_match('/_widget/', $file->name)) {
  246. $widget_type = $file->name;
  247. require_once($file->uri);
  248. if (class_exists($widget_type) and is_subclass_of($widget_type, 'TripalFieldWidget')) {
  249. $widgets[] = $widget_type;
  250. }
  251. }
  252. }
  253. return $widgets;
  254. }
  255. /**
  256. * Retrieves a list of field formatters compatible with a given field.
  257. *
  258. * @param $field
  259. * A field array as returned by the field_info_field() function.
  260. * @param $instance
  261. * A field instance array.
  262. *
  263. * @return
  264. * A list of file formatter class names.
  265. */
  266. function tripal_get_field_field_formatters($field, $instance) {
  267. $field_name = $field['field_name'];
  268. $field_type = $field['type'];
  269. $downloaders = [];
  270. // If the field type is a TripalField then get information about the formatter.
  271. if (tripal_load_include_field_class($field_type)) {
  272. $formatters = $field_type::$download_formatters;
  273. foreach ($formatters as $class_name) {
  274. if (!array_key_exists($class_name, $downloaders)) {
  275. tripal_load_include_downloader_class($class_name);
  276. $downloaders[$class_name] = $class_name::$full_label;
  277. }
  278. }
  279. }
  280. else {
  281. // For non TripalFields we'll assume TAB and CSV.
  282. tripal_load_include_downloader_class('TripalTabDownloader');
  283. tripal_load_include_downloader_class('TripalCSVDownloader');
  284. $downloaders['TripalTabDownloader'] = TripalTabDownloader::$full_label;
  285. $downloaders['TripalCSVDownloader'] = TripalCSVDownloader::$full_label;
  286. }
  287. return $downloaders;
  288. }
  289. /**
  290. * Retrieves a list of TripalFieldFormatters.
  291. *
  292. * The TripalFieldFormatter classes can be added by a site developer and should
  293. * be placed in the [module]/includes/TripalFields directory. Tripal will
  294. * support any widget as long as it is in this directory and extends the
  295. * TripalFieldFormatter class.
  296. *
  297. * @return
  298. * A list of TripalFieldFormatter names.
  299. *
  300. * @ingroup tripal_fields_api
  301. */
  302. function tripal_get_field_formatters() {
  303. $formatters = [];
  304. $field_files = tripal_get_tripalfield_files();
  305. // Iterate through the fields, include the file and run the info function.
  306. foreach ($field_files as $file) {
  307. if (preg_match('/_formatter/', $file->name)) {
  308. $formatter_type = $file->name;
  309. require_once($file->uri);
  310. if (class_exists($formatter_type) and is_subclass_of($formatter_type, 'TripalFieldFormatter')) {
  311. $formatters[] = $formatter_type;
  312. }
  313. }
  314. }
  315. return $formatters;
  316. }
  317. /**
  318. * Loads the TripalField class file into scope.
  319. *
  320. * @param $class
  321. * The class to include. This can be a TripalField, TripalFieldWidget or
  322. * TripalFieldFormatter class name.
  323. *
  324. * @return
  325. * TRUE if the field type class file was found, FALSE otherwise.
  326. *
  327. * @ingroup tripal_fields_api
  328. */
  329. function tripal_load_include_field_class($class) {
  330. $modules = module_list(TRUE);
  331. foreach ($modules as $module) {
  332. $field_type = preg_replace('/(^.*)_(formatter|widget)/', '$1', $class);
  333. $file_path = realpath(".") . '/' . drupal_get_path('module', $module) . '/includes/TripalFields/' . $field_type . '/' . $class . '.inc';
  334. if (file_exists($file_path)) {
  335. module_load_include('inc', $module, 'includes/TripalFields/' . $field_type . '/' . $class);
  336. if (class_exists($class)) {
  337. return TRUE;
  338. }
  339. }
  340. }
  341. // If the libraries module is enabled then we want to look for a TripalFields
  342. // library folder and see if our field exist there.
  343. if (module_exists('libraries')) {
  344. $library_path = libraries_get_path('TripalFields');
  345. $field_type = preg_replace('/(^.*)_(formatter|widget)/', '$1', $class);
  346. $file_path = realpath(".") . '/' . $library_path . '/' . $field_type . '/' . $class . '.inc';
  347. if (file_exists($file_path)) {
  348. require_once($file_path);
  349. if (class_exists($class)) {
  350. return TRUE;
  351. }
  352. }
  353. }
  354. return FALSE;
  355. }
  356. /**
  357. * Loads the TripalEntityDownloader file into scope.
  358. *
  359. * @param $class
  360. * The name of the class to include.
  361. *
  362. * @return
  363. * TRUE if the downloader class file was found, FALSE otherwise.
  364. *
  365. * @ingroup tripal_fields_api
  366. */
  367. function tripal_load_include_downloader_class($class) {
  368. $modules = module_list(TRUE);
  369. foreach ($modules as $module) {
  370. $file_path = realpath(".") . '/' . drupal_get_path('module', $module) . '/includes/TripalFieldDownloaders/' . $class . '.inc';
  371. if (file_exists($file_path)) {
  372. module_load_include('inc', $module, 'includes/TripalFieldDownloaders/' . $class);
  373. if (class_exists($class)) {
  374. return TRUE;
  375. }
  376. }
  377. }
  378. // If the libraries module is enabled then we want to look for a
  379. // TripalFieldDownloader library folder and see if our field exist there.
  380. if (module_exists('libraries')) {
  381. $library_path = libraries_get_path('TripalFieldDownloaders');
  382. $file_path = realpath(".") . '/' . $library_path . '/' . $class . '.inc';
  383. if (file_exists($file_path)) {
  384. require_once($file_path);
  385. if (class_exists($class)) {
  386. return TRUE;
  387. }
  388. }
  389. }
  390. return FALSE;
  391. }
  392. /**
  393. * More easily get the value of a single item from a field's items array.
  394. *
  395. * A Drupal field attached to an entity may have multiple items (i.e. it has
  396. * a cardinality > 1). Each of these items will always have a 'value' key that
  397. * contains the data for that field. However, fields can also have other keys
  398. * with their own values. You can easily retreive the value of these keys
  399. * using this function. What makes this function useful is that you can
  400. * provide a default value to use if the key has no value. This is useful
  401. * when developing a TripalField::widgetForm function and you need to
  402. * retreive default values and you don't want to have to always check if the
  403. * value is set.
  404. *
  405. * @param $items
  406. * The fields $items array. Compatbile with that returned by field_get_items.
  407. * @param $delta
  408. * The index of the item.
  409. * @parm $key
  410. * The name of the key within the item.
  411. * @param $default
  412. * A default value to return if the key is not set. By default the empty
  413. * string is returned.
  414. *
  415. * @return
  416. * The value assigned to the item's key; FALSE if the key doesn't exist or
  417. * the $default argument if no value is associated with the key.
  418. *
  419. * @ingroup tripal_fields_api
  420. */
  421. function tripal_get_field_item_keyval($items, $delta, $key, $default = '') {
  422. if (!array_key_exists($delta, $items)) {
  423. return FALSE;
  424. }
  425. if (!array_key_exists($key, $items[$delta])) {
  426. return FALSE;
  427. }
  428. if (!$items[$delta][$key]) {
  429. return $default;
  430. }
  431. return $items[$delta][$key];
  432. }
  433. /**
  434. * Formats an element of a TripalField for use by Drupal Views.
  435. *
  436. * Sometimes the value of TripalField can be more than just a single scalar. In
  437. * this case the value is an array of key value pairs where each key is a
  438. * controlled vocabulary term. In order to support fields, filtering and
  439. * sorting by these sub elements using Drupal Views, the TripalField
  440. * implementation must provide some help to Views by describing these elements,
  441. * and then implementing a query() function to support them. However, the
  442. * naming of sub elements must follow a set convention. This function
  443. * guarantees proper naming for sub elements.
  444. *
  445. * @param $field_name
  446. * The name of the field to which the element belongs.
  447. * @param $term
  448. * The term object as returned by tripal_get_term_details();
  449. *
  450. * @ingroup tripal_fields_api
  451. */
  452. function tripal_format_views_field_element($field_name, $term) {
  453. $element_name = $term['vocabulary']['short_name'] . '__' . $term['accession'];
  454. return $field_name . '.' . $element_name;
  455. }