tripal_chado.fields.inc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. if ($field['label'] == 'Timeaccessioned') {
  318. $field['label'] = 'Time Accessioned';
  319. $field['description'] = 'Please enter the time that this record was first added to the database.';
  320. }
  321. elseif ($field['label'] == 'Timelastmodified') {
  322. $field['label'] = 'Time Last Modified';
  323. $field['description'] = 'Please enter the time that this record was last modified. The default is the current time.';
  324. }
  325. //
  326. // ORGANISM TABLE
  327. //
  328. elseif ($field['field_settings']['chado_table'] == 'organism' and $field['field_settings']['chado_column'] == 'comment') {
  329. $field['label'] = 'Description';
  330. }
  331. //
  332. // FEATURE TABLE
  333. //
  334. elseif ($field['field_settings']['chado_table'] == 'feature' and $field['field_settings']['chado_column'] == 'uniquename') {
  335. $field['field_type'] = 'text';
  336. $field['widget_type'] = 'text_textfield';
  337. $field['field_settings']['text_processing'] = 0;
  338. $field['field_settings']['semantic_web']['type'] = 'name';
  339. $field['field_settings']['semantic_web']['ns'] = 'foaf';
  340. $field['field_settings']['semantic_web']['nsurl'] = 'http://xmlns.com/foaf/0.1/';
  341. }
  342. //
  343. // ANALYSIS TABLE
  344. //
  345. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'program') {
  346. $field['field_settings']['semantic_web']['type'] = 'SoftwareApplication';
  347. $field['field_settings']['semantic_web']['ns'] = 'schema';
  348. $field['field_settings']['semantic_web']['nsurl'] = 'https://schema.org/';
  349. $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.';
  350. $field['label'] = 'Program, Pipeline, Workflow or Method Name.';
  351. }
  352. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'sourceuri') {
  353. $field['field_type'] = 'text';
  354. $field['widget_type'] = 'text_textfield';
  355. $field['field_settings']['text_processing'] = 0;
  356. $field['label'] = 'Source URL';
  357. $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.';
  358. }
  359. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'sourcename') {
  360. $field['label'] = 'Source Name';
  361. $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.';
  362. }
  363. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'sourceversion') {
  364. $field['label'] = 'Source Version';
  365. $field['description'] = 'If hte source data set has a version include it here.';
  366. }
  367. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'algorithm') {
  368. $field['label'] = 'Source Version';
  369. $field['description'] = 'The name of the algorithm used to produce the dataset if different from the program.';
  370. }
  371. elseif ($field['field_settings']['chado_table'] == 'analysis' and $field['field_settings']['chado_column'] == 'programversion') {
  372. $field['label'] = 'Program Version';
  373. $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.';
  374. }
  375. //
  376. // PROJECT TABLE
  377. //
  378. elseif ($field['field_settings']['chado_table'] == 'project' and $field['field_settings']['chado_column'] == 'description') {
  379. $field['label'] = 'Short Description';
  380. }
  381. return $field;
  382. }
  383. /**
  384. * Implements hook_form_FORM_ID_alter().
  385. *
  386. * The field_ui_display_overview_form is used for formatting the display
  387. * or layout of fields attached to an entity and shown on the entity view page.
  388. *
  389. * This function removes the cvterm class and property adder field as those are
  390. * really not meant for users to show or manage.
  391. */
  392. function tripal_chado_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
  393. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  394. // on the add/edit form of an entity for adding new property fields.
  395. $fields_names = element_children($form['fields']);
  396. foreach ($fields_names as $field_name) {
  397. $field_info = field_info_field($field_name);
  398. if ($field_info['type'] == 'kvproperty_adder') {
  399. unset($form['fields'][$field_name]);
  400. }
  401. if ($field_info['type'] == 'cvterm_class_adder') {
  402. unset($form['fields'][$field_name]);
  403. }
  404. }
  405. }
  406. /**
  407. * Implements hook_form_FORM_ID_alter().
  408. *
  409. * The field_ui_field_overview_form is used for ordering and configuring the
  410. * fields attached to an entity.
  411. *
  412. * This function removes the property adder field as that is really not meant
  413. * for users to show or manage.
  414. */
  415. function tripal_chado_form_field_ui_field_overview_form_alter(&$form, &$form_state, $form_id) {
  416. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  417. // on the add/edit form of an entity for adding new property fields.
  418. $fields_names = element_children($form['fields']);
  419. foreach ($fields_names as $field_name) {
  420. $field_info = field_info_field($field_name);
  421. if ($field_info['type'] == 'kvproperty_adder') {
  422. unset($form['fields'][$field_name]);
  423. }
  424. if ($field_info['type'] == 'cvterm_class_adder') {
  425. unset($form['fields'][$field_name]);
  426. }
  427. }
  428. }
  429. /**
  430. * Implements hook_field_ws_formatter().
  431. *
  432. * The hook function is called by the tripal_ws module which provides
  433. * the RESTful web services. If that module is not installed this function
  434. * is never use.d
  435. */
  436. function tripal_chado_field_ws_formatter($entity_type, $entity, $field_info,
  437. $field, $items){
  438. $field_name = $field_info['field_name'];
  439. $field_type = $field_info['type'];
  440. $chado_table = $field_info['settings']['chado_table'];
  441. $chado_column = $field_info['settings']['chado_column'];
  442. // Check to see if this field is associated with a different entity. If so
  443. // we want to return the entity_id and not let the field customize the
  444. // display as we need to allow the tripal_ws module to link to the other
  445. // entity.
  446. if (property_exists($entity->chado_record, $chado_column) and
  447. property_exists($entity->chado_record->$chado_column, 'entity_id')) {
  448. $fk_entity_id = $entity->chado_record->$chado_column->entity_id;
  449. $value = array(
  450. 'entity_id' => $fk_entity_id,
  451. 'entity_type' => 'TripalEntity'
  452. );
  453. return $value;
  454. }
  455. // See if the field file defines a formatter.
  456. $function = $field_type . '_ws_formatter';
  457. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  458. $value = '';
  459. if (function_exists($function)) {
  460. $value = $function($entity_type, $entity, $field_info, $field, $items);
  461. }
  462. return $value;
  463. }
  464. /**
  465. * Implements hook_field_is_empty().
  466. */
  467. function tripal_chado_field_is_empty($item, $field) {
  468. // If there is no value field then the field is empty.
  469. if (!array_key_exists('value', $item)) {
  470. return TRUE;
  471. }
  472. // Iterate through all of the fields and if at least one has a value
  473. // the field is not empty.
  474. foreach ($item as $form_field_name => $value) {
  475. if (isset($value) and $value != NULL and $value != '') {
  476. return FALSE;
  477. }
  478. }
  479. // Otherwise, the field is empty.
  480. return TRUE;
  481. }
  482. /**
  483. * Implements hook_add_bundle_fields().
  484. */
  485. function tripal_chado_add_bundle_fields($entity_type, $bundle, $term) {
  486. $bundle_name = $bundle->name;
  487. // This array will hold details that map the bundle to tables in Chado.
  488. $bundle_data = array();
  489. // Get the cvterm that corresponds to this TripalTerm object.
  490. $vocab = entity_load('TripalVocab', array($term->vocab_id));
  491. $vocab = reset($vocab);
  492. $match = array(
  493. 'dbxref_id' => array(
  494. 'db_id' => array(
  495. 'name' => $vocab->namespace,
  496. ),
  497. 'accession' => $term->accession
  498. ),
  499. );
  500. $cvterm = chado_generate_var('cvterm', $match);
  501. // The organism table does not have a type_id so we won't ever find
  502. // a record for it in the tripal_cv_defaults table.
  503. if ($cvterm->name == 'organism') {
  504. $bundle_data = array(
  505. 'cv_id' => $cvterm->cv_id->cv_id,
  506. 'cvterm_id' => $cvterm->cvterm_id,
  507. 'data_table' => 'organism',
  508. 'type_table' => 'organism',
  509. 'field' => '',
  510. );
  511. }
  512. // The analysis table does not have a type_id so we won't ever find
  513. // a record for it in the tripalcv_defaults table.
  514. else if ($cvterm->name == 'analysis') {
  515. $bundle_data = array(
  516. 'cv_id' => $cvterm->cv_id->cv_id,
  517. 'cvterm_id' => $cvterm->cvterm_id,
  518. 'data_table' => 'analysis',
  519. 'type_table' => 'analysis',
  520. 'field' => '',
  521. );
  522. }
  523. else if ($cvterm->name == 'project') {
  524. $bundle_data = array(
  525. 'cv_id' => $cvterm->cv_id->cv_id,
  526. 'cvterm_id' => $cvterm->cvterm_id,
  527. 'data_table' => 'project',
  528. 'type_table' => 'project',
  529. 'field' => '',
  530. );
  531. }
  532. else {
  533. // TODO: WHAT TO DO IF A VOCABULARY IS USED AS A DEFAULT FOR MULTIPLE
  534. // TABLES.
  535. // Look to see if this vocabulary is used as a default for any table.
  536. $default = db_select('tripal_cv_defaults', 't')
  537. ->fields('t')
  538. ->condition('cv_id', $cvterm->cv_id->cv_id)
  539. ->execute()
  540. ->fetchObject();
  541. if ($default) {
  542. $bundle_data = array(
  543. 'cv_id' => $cvterm->cv_id->cv_id,
  544. 'cvterm_id' => $cvterm->cvterm_id,
  545. 'data_table' => $default->table_name,
  546. 'type_table' => $default->table_name,
  547. 'field' => $default->field_name,
  548. );
  549. }
  550. }
  551. // Save the mapping information so that we can reuse it when we need to
  552. // look things up for later for an entity
  553. tripal_set_bundle_variable('chado_cvterm_id', $bundle->id, $bundle_data['cvterm_id']);
  554. tripal_set_bundle_variable('chado_table', $bundle->id, $bundle_data['data_table']);
  555. tripal_set_bundle_variable('chado_column', $bundle->id, $bundle_data['field']);
  556. // Call the hook_attach_info() for all Chado fields to see if any of them
  557. // want to attach themsevles to this bundle.
  558. // Iterate through the fields, include the file and run the info function.
  559. $fields_path = drupal_get_path('module', 'tripal_chado') . '/includes/fields';
  560. $field_files = file_scan_directory($fields_path, '/^chado_.*\.inc$/');
  561. foreach ($field_files as $file) {
  562. $field_type = $file->name;
  563. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  564. $function = $field_type . '_attach_info';
  565. if (function_exists($function)) {
  566. // Get the field info.
  567. $field_info = $function($entity_type, $bundle, $bundle_data);
  568. if (!is_array($field_info) or count(array_keys($field_info)) == 0) {
  569. continue;
  570. }
  571. $field_name = $field_info['field_name'];
  572. tripal_add_bundle_field($field_name, $field_info, $entity_type, $bundle_name);
  573. }
  574. }
  575. // Adds any remaining base fields that may not have been dealt with
  576. // by a custom field.
  577. tripal_chado_add_bundle_fields_base__fields($entity_type, $bundle_name, $bundle_data);
  578. }
  579. /**
  580. * Adds the fields for the base table to the entity.
  581. */
  582. function tripal_chado_add_bundle_fields_base__fields($entity_type_name, $bundle_name, $bundle_data) {
  583. $table_name = $bundle_data['data_table'];
  584. $type_table = $bundle_data['type_table'];
  585. $type_field = $bundle_data['field'];
  586. // Iterate through the columns of the table and see if fields have been
  587. // created for each one. If not, then create them.
  588. $schema = chado_get_schema($table_name);
  589. $columns = $schema['fields'];
  590. foreach ($columns as $column_name => $details) {
  591. $field_name = $table_name . '__' . $column_name;
  592. // Skip the primary key field.
  593. if ($column_name == $schema['primary key'][0]) {
  594. continue;
  595. }
  596. // Skip the type field.
  597. if ($table_name == $type_table and $column_name == $type_field) {
  598. continue;
  599. }
  600. // Get the field defaults for this column.
  601. $field_info = tripal_chado_add_bundle_fields_base__fields_defaults($table_name, $schema, $column_name);
  602. // TODO: add in a call to drupal_alter to allow other modules to change
  603. // the field settings.
  604. // Determine if the field is required.
  605. if (array_key_exists('not null', $details) and $details['not null'] === TRUE) {
  606. $field_info['is_required'] = array_key_exists('default', $details) ? 0 : 1;
  607. }
  608. // If we don't have a field type then we don't need to create a field.
  609. if (!$field_info['field_type']) {
  610. // If we don't have a field type but it is required and doesn't have
  611. // a default value then we are in trouble.
  612. if ($field_info['is_required'] and !array_key_exists('default', $details)) {
  613. throw new Exception(t('The %table.%field type, %type, is not yet supported for Entity fields, but it is required,',
  614. array('%table' => $table_name, '%field' => $column_name, '%type' => $details['type'])));
  615. }
  616. continue;
  617. }
  618. // If this field is a foreign key field then we will have a custom field.
  619. $is_fk = FALSE;
  620. if (array_key_exists('foreign keys', $schema)) {
  621. foreach ($schema['foreign keys'] as $remote_table => $fk_details) {
  622. if (array_key_exists($column_name, $fk_details['columns'])) {
  623. $is_fk = TRUE;
  624. }
  625. }
  626. }
  627. // Add the field to the bundle.
  628. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  629. }
  630. }
  631. /**
  632. * Implements hook_field_validate().
  633. *
  634. * This function is used to validate any field. Fields with custom
  635. * widgets will most likely have their own validate function but for all
  636. * others we need a way to validate them.
  637. *
  638. */
  639. function tripal_chado_field_validate($entity_type, $entity, $field, $instance,
  640. $langcode, $items, &$errors) {
  641. }
  642. /**
  643. * Implements hook_field_attach_validate().
  644. *
  645. * This function is used to validate entity-wide validation any of the fields
  646. * attached to an entity.
  647. */
  648. function tripal_chado_field_attach_validate($entity_type, $entity, &$errors) {
  649. $bundle_name = $entity->bundle;
  650. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  651. $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  652. // Provide some higher-level validation for the organism type.
  653. if ($term->name == 'organism') {
  654. }
  655. }