tripal.fields.api.inc 18 KB

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