tripal_chado.fields.inc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. <?php
  2. /**
  3. * Implements hook_field_info().
  4. *
  5. * This function would normally provide a large info array for all of the
  6. * fields provided by this module. But instead it will call a hook that
  7. * can be implmented within each individual field file. This will allow
  8. * all of the code for a single field to be self contained in a single file.
  9. *
  10. * New fields can be added automatically by including a new file in the
  11. * tripal_chado/includes/fields directory. The file must be named with a
  12. * 'chado_' prefix and end with a '.inc' suffix. After adding the file,
  13. * the cache must be cleared.
  14. *
  15. */
  16. function tripal_chado_field_info() {
  17. $info = array();
  18. // Find all of the files in the tripal_chado/includes/fields directory.
  19. $fields_path = drupal_get_path('module', 'tripal_chado') . '/includes/fields';
  20. $field_files = file_scan_directory($fields_path, '/^chado_.*\.inc$/');
  21. // Iterate through the fields, include the file and run the info function.
  22. foreach ($field_files as $file) {
  23. $field_name = $file->name;
  24. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_name);
  25. $function = $field_name . '_info';
  26. if (function_exists($function)) {
  27. $info[$field_name] = $function();
  28. }
  29. }
  30. return $info;
  31. }
  32. /**
  33. * Implements hook_field_widget_info().
  34. *
  35. * This function would normally provide a large info array for all of the
  36. * widgets provided by this module. But instead it will call a hook that
  37. * can be implmented within each individual field file. This will allow
  38. * all of the code for a single field to be self contained in a single file.
  39. */
  40. function tripal_chado_field_widget_info() {
  41. $widgets = array();
  42. $fields = field_info_fields();
  43. foreach ($fields as $field) {
  44. $field_type = $field['type'];
  45. if ($field['storage']['type'] == 'field_chado_storage') {
  46. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  47. $function = $field_type . '_widget_info';
  48. if (function_exists($function)) {
  49. $widgets[$field_type . '_widget'] = $function();
  50. }
  51. }
  52. }
  53. return $widgets;
  54. }
  55. /**
  56. * Implements hook_field_formatter_info().
  57. *
  58. * This function would normally provide a large info array for all of the
  59. * formatters provided by this module. But instead it will call a hook that
  60. * can be implmented within each individual field file. This will allow
  61. * all of the code for a single field to be self contained in a single file.
  62. */
  63. function tripal_chado_field_formatter_info() {
  64. $formatters = array();
  65. $fields = field_info_fields();
  66. foreach ($fields as $field) {
  67. $field_type = $field['type'];
  68. if ($field['storage']['type'] == 'field_chado_storage') {
  69. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  70. $function = $field_type . '_formatter_info';
  71. if (function_exists($function)) {
  72. $formatters[$field_type . '_formatter'] = $function();
  73. }
  74. }
  75. }
  76. return $formatters;
  77. }
  78. /**
  79. * Implements hook_field_formatter_settings_summary().
  80. */
  81. function tripal_chado_field_formatter_settings_summary($field, $instance, $view_mode) {
  82. $summary = '';
  83. $field_type = $field['type'];
  84. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  85. $function = $field_type . '_formatter_settings_summary';
  86. if (function_exists($function)) {
  87. $summary = $function($field, $instance, $view_mode);
  88. }
  89. return $summary;
  90. }
  91. /**
  92. * Implements hook_field_formatter_settings_form().
  93. */
  94. function tripal_chado_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  95. $element = array();
  96. $field_type = $field['type'];
  97. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/' . $field_type);
  98. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  99. $function = $field_type . '_formatter_settings_form';
  100. if (function_exists($function)) {
  101. $element = $function($field, $instance, $view_mode, $form, $form_state);
  102. }
  103. return $element;
  104. }
  105. /**
  106. * Implements hook_field_formatter_view().
  107. */
  108. function tripal_chado_field_formatter_view($entity_type, $entity, $field,
  109. $instance, $langcode, $items, $display) {
  110. $element = array();
  111. $field_type = $field['type'];
  112. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  113. $function = $display['type'];
  114. if (function_exists($function)) {
  115. $function($element, $entity_type, $entity, $field, $instance, $langcode, $items, $display);
  116. }
  117. return $element;
  118. }
  119. /**
  120. * Implements hook_field_widget_form().
  121. */
  122. function tripal_chado_field_widget_form(&$form, &$form_state, $field,
  123. $instance, $langcode, $items, $delta, $element) {
  124. $widget = $element;
  125. $field_name = $instance['field_name'];
  126. $field_type = $field['type'];
  127. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/' . $field_type);
  128. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_name);
  129. $function = $field_type . '_widget';
  130. if (function_exists($function)) {
  131. $function($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  132. }
  133. return $widget;
  134. }
  135. /**
  136. * Returns the values of the field from the $form_state.
  137. */
  138. function tripal_chado_get_field_form_values($field_name, $form_state, $delta = 0, $child = NULL) {
  139. $value = NULL;
  140. // The form_state must have the 'values' key. If not then just return.
  141. if (!array_key_exists('values', $form_state)) {
  142. return $value;
  143. }
  144. // If the field name is not in the form_state['values'] then return.
  145. if (!array_key_exists($field_name, $form_state['values'])) {
  146. return $value;
  147. }
  148. // Iterate through the values looking for the field_name provided.
  149. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  150. if (!array_key_exists($delta, $items)) {
  151. continue;
  152. }
  153. $item = $items[$delta];
  154. if ($child){
  155. if(array_key_exists($child, $item) and $item[$child] != '') {
  156. $value = $item[$child];
  157. }
  158. }
  159. else {
  160. $value = $item['value'];
  161. }
  162. }
  163. return $value;
  164. }
  165. /**
  166. * Sets the values of the field from the $form_state.
  167. */
  168. function tripal_chado_set_field_form_values($field_name, &$form_state, $newvalue, $delta = 0, $child = NULL) {
  169. // The form_state must have the 'values' key. If not then just return.
  170. if (!array_key_exists('values', $form_state)) {
  171. return FALSE;
  172. }
  173. // If the field name is not in the form_state['values'] then reutrn.
  174. if (!array_key_exists($field_name, $form_state['values'])) {
  175. return FALSE;
  176. }
  177. foreach ($form_state['values'][$field_name] as $langcode => $items) {
  178. if ($child) {
  179. $form_state['values'][$field_name][$langcode][$delta][$child] = $newvalue;
  180. }
  181. else {
  182. $form_state['values'][$field_name][$langcode][$delta]['value'] = $newvalue;
  183. }
  184. }
  185. return TRUE;
  186. }
  187. /**
  188. * Implements hook_field_widget_form_alter().
  189. */
  190. function tripal_chado_field_widget_form_alter(&$element, &$form_state, $context) {
  191. if (array_key_exists('#field_name', $element)) {
  192. $field_name = $element['#field_name'];
  193. $matches = array();
  194. if (preg_match('/(.+?)__(.+?)$/', $field_name, $matches)) {
  195. $tablename = $matches[1];
  196. $colname = $matches[2];
  197. $schema = chado_get_schema($tablename);
  198. // The timelastmodified field exists in many Chado tables. We want
  199. // the form element to update to the most recent time rather than the time
  200. // in the database.
  201. if ($colname == 'timelastmodified' and $schema['fields'][$colname]['type'] == 'datetime') {
  202. // We want the default value for the field to be the current time.
  203. $element['#default_value']['value'] = format_date(time(), 'custom', "Y-m-d H:i:s", 'UTC');
  204. $element['#date_items']['value'] = $element['#default_value']['value'];
  205. }
  206. // We want the date combo fieldset to be collaspible so we will
  207. // add our own theme_wrapper to replace the one added by the date
  208. // module.
  209. if (array_key_exists($colname, $schema['fields']) and $schema['fields'][$colname]['type'] == 'datetime') {
  210. $element['#theme_wrappers'] = array('tripal_chado_date_combo');
  211. }
  212. }
  213. }
  214. }
  215. /**
  216. * Returns a $field_info array for a field based on a database column.
  217. *
  218. */
  219. function tripal_chado_add_bundle_fields_base__fields_defaults($table_name, $schema, $column_name) {
  220. $details = $schema['fields'][$column_name];
  221. // Create an array with information about this field.
  222. $field = array(
  223. 'field_type' => '',
  224. 'widget_type' => '',
  225. 'description' => '',
  226. 'label' => ucwords(preg_replace('/_/', ' ', $column_name)),
  227. 'is_required' => 0,
  228. 'storage' => 'field_chado_storage',
  229. 'widget_settings' => array(
  230. 'display_label' => 1
  231. ),
  232. 'field_settings' => array(
  233. // The table in Chado where this field maps to.
  234. 'chado_table' => $table_name,
  235. // The column in the Chado table that this field maps to.
  236. 'chado_column' => $column_name,
  237. 'semantic_web' => array(
  238. // The type is the term from a vocabulary that desribes this field..
  239. 'type' => '',
  240. // The namepsace for the vocabulary (e.g. 'foaf').
  241. 'ns' => '',
  242. // The URL for the namespace. It must be that the type can be
  243. // appended to the URL.
  244. 'nsurl' => '',
  245. ),
  246. ),
  247. );
  248. // Alter the field info array depending on the column details.
  249. switch($details['type']) {
  250. case 'char':
  251. $field['field_type'] = 'text';
  252. $field['widget_type'] = 'text_textfield';
  253. $field['field_settings']['max_length'] = $details['length'];
  254. break;
  255. case 'varchar':
  256. $field['field_type'] = 'text';
  257. $field['widget_type'] = 'text_textfield';
  258. $field['field_settings']['max_length'] = $details['length'];
  259. break;
  260. case 'text':
  261. $field['field_type'] = 'text';
  262. $field['widget_type'] = 'text_textarea';
  263. $field['field_settings']['max_length'] = 17179869184;
  264. $field['field_settings']['text_processing'] = 1;
  265. $field['format'] = filter_default_format();
  266. break;
  267. case 'blob':
  268. // not sure how to support a blob field.
  269. continue;
  270. break;
  271. case 'int':
  272. $field['field_type'] = 'number_integer';
  273. $field['widget_type'] = 'number';
  274. break;
  275. case 'float':
  276. $field['field_type'] = 'number_float';
  277. $field['widget_type'] = 'number';
  278. $field['field_settings']['precision'] = 10;
  279. $field['field_settings']['scale'] = 2;
  280. $field['field_settings']['decimal_separator'] = '.';
  281. break;
  282. case 'numeric':
  283. $field['field_type'] = 'number_decimal';
  284. $field['widget_type'] = 'number';
  285. break;
  286. case 'serial':
  287. // Serial fields are most likely not needed as a field.
  288. break;
  289. case 'boolean':
  290. $field['field_type'] = 'list_boolean';
  291. $field['widget_type'] = 'options_onoff';
  292. $field['field_settings']['allowed_values'] = array(0 => "No", 1 => "Yes");
  293. break;
  294. case 'datetime':
  295. // Use the Drupal Date and Date API to create the field/widget
  296. $field['field_type'] = 'datetime';
  297. $field['widget_type'] = 'date_select';
  298. $field['widget_settings']['increment'] = 1;
  299. $field['widget_settings']['tz_handling'] = 'none';
  300. $field['widget_settings']['collapsible'] = TRUE;
  301. // TODO: Add settings so that the minutes increment by 1.
  302. // And turn off the timezone, as the Chado field doesn't support it.
  303. break;
  304. }
  305. // Set some default semantic web information
  306. if ($column_name == 'name') {
  307. $field['field_settings']['semantic_web']['type'] = 'name';
  308. $field['field_settings']['semantic_web']['ns'] = 'foaf';
  309. $field['field_settings']['semantic_web']['nsurl'] = 'http://xmlns.com/foaf/0.1/';
  310. }
  311. if ($column_name == 'description' or $column_name == 'definition' or
  312. $column_name == 'comment') {
  313. $field['field_settings']['semantic_web']['type'] = 'description';
  314. $field['field_settings']['semantic_web']['ns'] = 'hydra';
  315. $field['field_settings']['semantic_web']['nsurl'] = 'http://www.w3.org/ns/hydra/core#';
  316. }
  317. //
  318. // GENERIC COLUMNS
  319. //
  320. // if ($field['field_settings']['chado_column'] == 'organism_id') {
  321. // $field['field_type'] = 'chado_base__organism_id';
  322. // $field['widget_type'] = 'chado_base__organism_id_widget';
  323. // $field['label'] = 'Organism';
  324. // $field['description'] = 'Select an organism.';
  325. // $field['field_settings']['semantic_web']['type'] = 'organism';
  326. // $field['field_settings']['semantic_web']['ns'] = 'local';
  327. // $field['field_settings']['semantic_web']['nsurl'] = '';
  328. // }
  329. // elseif ($field['field_settings']['chado_column'] =='dbxref_id') {
  330. // $field['field_type'] = 'chado_base__dbxref_id';
  331. // $field['widget_type'] = 'chado_base_dbxref_id_widget';
  332. // $field['label'] = 'Cross Reference';
  333. // $field['description'] = 'This record can be cross referenced with a ' .
  334. // 'record in another online database. The primary reference is for the ' .
  335. // 'most prominent reference. At a minimum, the database and accession ' .
  336. // 'must be provided. To remove a set reference, change the database ' .
  337. // 'field to "Select a Database".';
  338. // }
  339. if ($field['label'] == 'Timeaccessioned') {
  340. $field['label'] = 'Time Accessioned';
  341. $field['description'] = 'Please enter the time that this record was first added to the database.';
  342. }
  343. elseif ($field['label'] == 'Timelastmodified') {
  344. $field['label'] = 'Time Last Modified';
  345. $field['description'] = 'Please enter the time that this record was last modified. The default is the current time.';
  346. }
  347. //
  348. // ORGANISM TABLE
  349. //
  350. elseif ($field['field_settings']['chado_table'] == 'organism' and $field['field_settings']['chado_column'] == 'comment') {
  351. $field['label'] = 'Description';
  352. }
  353. //
  354. // FEATURE TABLE
  355. //
  356. elseif ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'uniquename') {
  357. $field['field_type'] = 'text';
  358. $field['widget_type'] = 'text_textfield';
  359. $field['field_settings']['text_processing'] = 0;
  360. $field['field_settings']['semantic_web']['type'] = 'name';
  361. $field['field_settings']['semantic_web']['ns'] = 'foaf';
  362. $field['field_settings']['semantic_web']['nsurl'] = 'http://xmlns.com/foaf/0.1/';
  363. }
  364. /* elseif ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'md5checksum') {
  365. $field['field_type'] = 'chado_feature__md5checksum';
  366. $field['widget_type'] = 'chado_feature__md5checksum_widget';
  367. $field['label'] = 'MD5 Checksum';
  368. $field['description'] = 'Generate an MD5 checksum for the sequence.';
  369. }
  370. elseif ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'seqlen') {
  371. $field['field_type'] = 'chado_feature__seqlen';
  372. $field['widget_type'] = 'chado_feature__seqlen_widget';
  373. $field['label'] = 'Seqlen';
  374. $field['description'] = 'The length of the residues.';
  375. }
  376. elseif ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'residues') {
  377. $field['field_type'] = 'chado_feature__residues';
  378. $field['widget_type'] = 'chado_feature__residues_widget';
  379. $field['label'] = 'Residues';
  380. $field['description'] = 'Please provide an IUPAC compatible residues for this feature. Spaces and new lines are allowed.';
  381. } */
  382. //
  383. // ANALYSIS TABLE
  384. //
  385. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'program') {
  386. $field['field_settings']['semantic_web']['type'] = 'SoftwareApplication';
  387. $field['field_settings']['semantic_web']['ns'] = 'schema';
  388. $field['field_settings']['semantic_web']['nsurl'] = 'https://schema.org/';
  389. $field['description'] = 'The program name (e.g. blastx, blastp, sim4, genscan. If the analysis was not derived from a software package then provide a very brief description of the pipeline, workflow or method.';
  390. $field['label'] = 'Program, Pipeline, Workflow or Method Name.';
  391. }
  392. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'sourceuri') {
  393. $field['field_type'] = 'text';
  394. $field['widget_type'] = 'text_textfield';
  395. $field['field_settings']['text_processing'] = 0;
  396. $field['label'] = 'Source URL';
  397. $field['description'] = 'The URL where the original source data was derived. Ideally, this should link to the page where more information about the source data can be found.';
  398. }
  399. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'sourcename') {
  400. $field['label'] = 'Source Name';
  401. $field['description'] = 'The name of the source data. This could be a file name, data set or a small description for how the data was collected. For long descriptions use the larger description field.';
  402. }
  403. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'sourceversion') {
  404. $field['label'] = 'Source Version';
  405. $field['description'] = 'If hte source data set has a version include it here.';
  406. }
  407. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'algorithm') {
  408. $field['label'] = 'Source Version';
  409. $field['description'] = 'The name of the algorithm used to produce the dataset if different from the program.';
  410. }
  411. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'programversion') {
  412. $field['label'] = 'Program Version';
  413. $field['description'] = 'The version of the program used to perform this analysis. (e.g. TBLASTX 2.0MP-WashU [09-Nov-2000]. Enter "n/a" if no version is available or applicable.';
  414. }
  415. //
  416. // PROJECT TABLE
  417. //
  418. elseif ($field['field_settings']['chado_table'] == 'project' and $field['field_settings']['chado_column'] == 'description') {
  419. $field['label'] = 'Short Description';
  420. }
  421. return $field;
  422. }
  423. /**
  424. * Implements hook_form_FORM_ID_alter().
  425. *
  426. * The field_ui_display_overview_form is used for formatting the display
  427. * or layout of fields attached to an entity and shown on the entity view page.
  428. *
  429. * This function removes the cvterm class and property adder field as those are
  430. * really not meant for users to show or manage.
  431. */
  432. function tripal_chado_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
  433. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  434. // on the add/edit form of an entity for adding new property fields.
  435. $fields_names = element_children($form['fields']);
  436. foreach ($fields_names as $field_name) {
  437. $field_info = field_info_field($field_name);
  438. if ($field_info['type'] == 'kvproperty_adder') {
  439. unset($form['fields'][$field_name]);
  440. }
  441. if ($field_info['type'] == 'cvterm_class_adder') {
  442. unset($form['fields'][$field_name]);
  443. }
  444. }
  445. }
  446. /**
  447. * Implements hook_form_FORM_ID_alter().
  448. *
  449. * The field_ui_field_overview_form is used for ordering and configuring the
  450. * fields attached to an entity.
  451. *
  452. * This function removes the property adder field as that is really not meant
  453. * for users to show or manage.
  454. */
  455. function tripal_chado_form_field_ui_field_overview_form_alter(&$form, &$form_state, $form_id) {
  456. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  457. // on the add/edit form of an entity for adding new property fields.
  458. $fields_names = element_children($form['fields']);
  459. foreach ($fields_names as $field_name) {
  460. $field_info = field_info_field($field_name);
  461. if ($field_info['type'] == 'kvproperty_adder') {
  462. unset($form['fields'][$field_name]);
  463. }
  464. if ($field_info['type'] == 'cvterm_class_adder') {
  465. unset($form['fields'][$field_name]);
  466. }
  467. }
  468. }
  469. /**
  470. * Implements hook_field_ws_formatter().
  471. *
  472. * The hook function is called by the tripal_ws module which provides
  473. * the RESTful web services. If that module is not installed this function
  474. * is never use.d
  475. */
  476. function tripal_chado_field_ws_formatter($entity_type, $entity, $field_info,
  477. $field, $items){
  478. $values = array();
  479. // Only deal with fields that were created by this module.
  480. if ($field_info['storage']['type'] != 'field_chado_storage') {
  481. return $values;
  482. }
  483. // See if the field file defines a formatter.
  484. $field_type = $field_info['type'];
  485. $function = $field_type . '_ws_formatter';
  486. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  487. if (function_exists($function)) {
  488. $values = $function($entity_type, $entity, $field_info, $field, $items);
  489. }
  490. // If no customizations exist then perform some default formatting
  491. if (count($values) == 0) {
  492. }
  493. return $values;
  494. }
  495. /**
  496. * Implements hook_field_is_empty().
  497. */
  498. function tripal_chado_field_is_empty($item, $field) {
  499. // If there is no value field then the field is empty.
  500. if (!array_key_exists('value', $item)) {
  501. return TRUE;
  502. }
  503. // Iterate through all of the fields and if at least one has a value
  504. // the field is not empty.
  505. foreach ($item as $form_field_name => $value) {
  506. if (isset($value) and $value != NULL and $value != '') {
  507. return FALSE;
  508. }
  509. }
  510. // Otherwise, the field is empty.
  511. return TRUE;
  512. }
  513. /**
  514. * Implements hook_add_bundle_fields().
  515. */
  516. function tripal_chado_add_bundle_fields($entity_type, $bundle, $term) {
  517. $bundle_name = $bundle->name;
  518. // This array will hold details that map the bundle to tables in Chado.
  519. $bundle_data = array();
  520. // Get the cvterm that corresponds to this TripalTerm object.
  521. $vocab = entity_load('TripalVocab', array($term->vocab_id));
  522. $vocab = reset($vocab);
  523. $match = array(
  524. 'dbxref_id' => array(
  525. 'db_id' => array(
  526. 'name' => $vocab->namespace,
  527. ),
  528. 'accession' => $term->accession
  529. ),
  530. );
  531. $cvterm = chado_generate_var('cvterm', $match);
  532. // The organism table does not have a type_id so we won't ever find
  533. // a record for it in the tripal_cv_defaults table.
  534. if ($cvterm->name == 'organism') {
  535. $bundle_data = array(
  536. 'cv_id' => $cvterm->cv_id->cv_id,
  537. 'cvterm_id' => $cvterm->cvterm_id,
  538. 'data_table' => 'organism',
  539. 'type_table' => 'organism',
  540. 'field' => '',
  541. );
  542. }
  543. // The analysis table does not have a type_id so we won't ever find
  544. // a record for it in the tripalcv_defaults table.
  545. else if ($cvterm->name == 'analysis') {
  546. $bundle_data = array(
  547. 'cv_id' => $cvterm->cv_id->cv_id,
  548. 'cvterm_id' => $cvterm->cvterm_id,
  549. 'data_table' => 'analysis',
  550. 'type_table' => 'analysis',
  551. 'field' => '',
  552. );
  553. }
  554. else if ($cvterm->name == 'project') {
  555. $bundle_data = array(
  556. 'cv_id' => $cvterm->cv_id->cv_id,
  557. 'cvterm_id' => $cvterm->cvterm_id,
  558. 'data_table' => 'project',
  559. 'type_table' => 'project',
  560. 'field' => '',
  561. );
  562. }
  563. else {
  564. // TODO: WHAT TO DO IF A VOCABULARY IS USED AS A DEFAULT FOR MULTIPLE
  565. // TABLES.
  566. // Look to see if this vocabulary is used as a default for any table.
  567. $default = db_select('tripal_cv_defaults', 't')
  568. ->fields('t')
  569. ->condition('cv_id', $cvterm->cv_id->cv_id)
  570. ->execute()
  571. ->fetchObject();
  572. if ($default) {
  573. $bundle_data = array(
  574. 'cv_id' => $cvterm->cv_id->cv_id,
  575. 'cvterm_id' => $cvterm->cvterm_id,
  576. 'data_table' => $default->table_name,
  577. 'type_table' => $default->table_name,
  578. 'field' => $default->field_name,
  579. );
  580. }
  581. }
  582. // Save the mapping information so that we can reuse it when we need to
  583. // look things up for later for an entity
  584. tripal_set_bundle_variable('chado_cvterm_id', $bundle->id, $bundle_data['cvterm_id']);
  585. tripal_set_bundle_variable('chado_table', $bundle->id, $bundle_data['data_table']);
  586. tripal_set_bundle_variable('chado_column', $bundle->id, $bundle_data['field']);
  587. // Call the hook_attach_info() for all Chado fields to see if any of them
  588. // want to attach themsevles to this bundle.
  589. // Iterate through the fields, include the file and run the info function.
  590. $fields_path = drupal_get_path('module', 'tripal_chado') . '/includes/fields';
  591. $field_files = file_scan_directory($fields_path, '/^chado_.*\.inc$/');
  592. foreach ($field_files as $file) {
  593. $field_type = $file->name;
  594. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  595. $function = $field_type . '_attach_info';
  596. if (function_exists($function)) {
  597. // Get the field info.
  598. $field_info = $function($entity_type, $bundle, $bundle_data);
  599. if (!is_array($field_info) or count(array_keys($field_info)) == 0) {
  600. continue;
  601. }
  602. $field_name = $field_info['field_name'];
  603. tripal_add_bundle_field($field_name, $field_info, $entity_type, $bundle_name);
  604. }
  605. }
  606. // Adds any remaining base fields that may not have been dealt with
  607. // by a custom field.
  608. tripal_chado_add_bundle_fields_base__fields($entity_type, $bundle_name, $bundle_data);
  609. }
  610. /**
  611. * Adds the fields for the base table to the entity.
  612. */
  613. function tripal_chado_add_bundle_fields_base__fields($entity_type_name, $bundle_name, $bundle_data) {
  614. $table_name = $bundle_data['data_table'];
  615. $type_table = $bundle_data['type_table'];
  616. $type_field = $bundle_data['field'];
  617. // Iterate through the columns of the table and see if fields have been
  618. // created for each one. If not, then create them.
  619. $schema = chado_get_schema($table_name);
  620. $columns = $schema['fields'];
  621. foreach ($columns as $column_name => $details) {
  622. $field_name = $table_name . '__' . $column_name;
  623. // Skip the primary key field.
  624. if ($column_name == $schema['primary key'][0]) {
  625. continue;
  626. }
  627. // Skip the type field.
  628. if ($table_name == $type_table and $column_name == $type_field) {
  629. continue;
  630. }
  631. // Get the field defaults for this column.
  632. $field_info = tripal_chado_add_bundle_fields_base__fields_defaults($table_name, $schema, $column_name);
  633. // TODO: add in a call to drupal_alter to allow other modules to change
  634. // the field settings.
  635. // Determine if the field is required.
  636. if (array_key_exists('not null', $details) and $details['not null'] === TRUE) {
  637. $field_info['is_required'] = array_key_exists('default', $details) ? 0 : 1;
  638. }
  639. // If we don't have a field type then we don't need to create a field.
  640. if (!$field_info['field_type']) {
  641. // If we don't have a field type but it is required and doesn't have
  642. // a default value then we are in trouble.
  643. if ($field_info['is_required'] and !array_key_exists('default', $details)) {
  644. throw new Exception(t('The %table.%field type, %type, is not yet supported for Entity fields, but it is required,',
  645. array('%table' => $table_name, '%field' => $column_name, '%type' => $details['type'])));
  646. }
  647. continue;
  648. }
  649. // If this field is a foreign key field then we will have a custom field.
  650. $is_fk = FALSE;
  651. if (array_key_exists('foreign keys', $schema)) {
  652. foreach ($schema['foreign keys'] as $remote_table => $fk_details) {
  653. if (array_key_exists($column_name, $fk_details['columns'])) {
  654. $is_fk = TRUE;
  655. }
  656. }
  657. }
  658. // Add the field to the bundle.
  659. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  660. }
  661. }
  662. /**
  663. * Implements hook_field_validate().
  664. *
  665. * This function is used to validate any field. Fields with custom
  666. * widgets will most likely have their own validate function but for all
  667. * others we need a way to validate them.
  668. *
  669. */
  670. function tripal_chado_field_validate($entity_type, $entity, $field, $instance,
  671. $langcode, $items, &$errors) {
  672. }
  673. /**
  674. * Implements hook_field_attach_validate().
  675. *
  676. * This function is used to validate entity-wide validation any of the fields
  677. * attached to an entity.
  678. */
  679. function tripal_chado_field_attach_validate($entity_type, $entity, &$errors) {
  680. $bundle_name = $entity->bundle;
  681. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  682. $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  683. // Provide some higher-level validation for the organism type.
  684. if ($term->name == 'organism') {
  685. }
  686. }