tripal_chado.fields.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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_type = $file->name;
  24. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  25. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  26. $field_obj = new $field_type();
  27. $info[$field_type] = $field_obj->field_info();
  28. }
  29. }
  30. return $info;
  31. }
  32. /**
  33. * Implements hook_field_create_info().
  34. *
  35. * This is a Tripal defined hook that supports integration with the
  36. * TripalEntity field.
  37. */
  38. function tripal_chado_field_create_info($entity_type, $bundle, $term) {
  39. // Get the details about the mapping of this bundle to the Chado table:
  40. $details = array(
  41. 'chado_cv_id' => tripal_get_bundle_variable('chado_cv_id', $bundle->id),
  42. 'chado_cvterm_id' => tripal_get_bundle_variable('chado_cvterm_id', $bundle->id),
  43. 'chado_table' => tripal_get_bundle_variable('chado_table', $bundle->id),
  44. 'chado_type_table' => tripal_get_bundle_variable('chado_type_table', $bundle->id),
  45. 'chado_type_column' => tripal_get_bundle_variable('chado_type_column', $bundle->id),
  46. );
  47. $base_fields = tripal_chado_field_create_base('create_info', $entity_type, $bundle, $details);
  48. $custom_fields = tripal_chado_field_create_info_custom($entity_type, $bundle, $details);
  49. return array_merge($base_fields, $custom_fields);
  50. }
  51. /**
  52. * A helper function for the tripal_chado_field_create_info() function.
  53. *
  54. * This function adds in the custom fields info by instantiating the class
  55. * for the custom field, calling the create_info() function and
  56. * returning the info array.
  57. *
  58. * @param $entity_type
  59. * The type of entity (e.g TripalEntity)
  60. * @param $bundle
  61. * The bundle object.
  62. * @param $details
  63. * An array containing the mapping of the bundle to the Chado table.
  64. */
  65. function tripal_chado_field_create_info_custom($entity_type, $bundle, $details) {
  66. $info = array();
  67. // Find all of the files in the tripal_chado/includes/fields directory.
  68. $fields_path = drupal_get_path('module', 'tripal_chado') . '/includes/fields';
  69. $field_files = file_scan_directory($fields_path, '/^chado_.*\.inc$/');
  70. // Iterate through the fields, include the file and run the info function.
  71. foreach ($field_files as $file) {
  72. $field_type = $file->name;
  73. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  74. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  75. $field_obj = new $field_type();
  76. $result = $field_obj->create_info($entity_type, $bundle, $details);
  77. if (is_array($result)) {
  78. $info[$result['field_name']] = $result;
  79. }
  80. }
  81. }
  82. return $info;
  83. }
  84. /**
  85. * Retrieves either the create_info or create_instance_info arrays.
  86. *
  87. * The logic for creating the fields for the base table is so similar for
  88. * both the create_info and create_instance_info arrays they are both
  89. * handled by this function to prevent duplication of code.
  90. *
  91. * @param $step
  92. * Set to 'create_info' to retrun the create_info array or
  93. * 'create_instance_info' to return the create_instance_info array.
  94. * @param $entity_type
  95. * The type of entity (e.g TripalEntity)
  96. * @param $bundle
  97. * The bundle object.
  98. * @param $details
  99. * An array containing the mapping of the bundle to the Chado table.
  100. *
  101. * @return
  102. * An array compabile with the tripal_chado_field_create_info() and
  103. * tripal_chado_field_create_instance_info() functions.
  104. */
  105. function tripal_chado_field_create_base($step, $entity_type, $bundle, $details) {
  106. $fields = array();
  107. // Get Chado information
  108. $table_name = $details['chado_table'];
  109. $type_table = $details['chado_type_table'];
  110. $type_field = $details['chado_type_column'];
  111. // Iterate through the columns of the table and see if fields have been
  112. // created for each one. If not, then create them.
  113. $schema = chado_get_schema($table_name);
  114. if (!$schema) {
  115. return $fields();
  116. }
  117. $columns = $schema['fields'];
  118. foreach ($columns as $column_name => $details) {
  119. $field_name = $table_name . '__' . $column_name;
  120. // Skip the primary key field.
  121. if ($column_name == $schema['primary key'][0]) {
  122. continue;
  123. }
  124. // Skip the type field.
  125. if ($table_name == $type_table and $column_name == $type_field) {
  126. continue;
  127. }
  128. // Get the field defaults for this column.
  129. $field_info = array();
  130. if ($step == 'create_info') {
  131. $field_info = tripal_chado_field_create_info_base_defaults($field_name,
  132. $table_name, $schema, $column_name);
  133. }
  134. if ($step == 'create_instance_info') {
  135. $field_info = tripal_chado_field_create_instance_info_base_defaults($bundle->name,
  136. $field_name, $table_name, $schema, $column_name);
  137. }
  138. // TODO: add in a call to drupal_alter to allow other modules to change
  139. // the field settings.
  140. // Add the field to the bundle.
  141. $fields[$field_name] = $field_info;
  142. }
  143. return $fields;
  144. }
  145. /**
  146. * A helper function for the tripal_chado_field_create_info() function.
  147. *
  148. * This function generates the default chado_info array for a column in
  149. * a base table of Chado. All of fields returned by this function use
  150. * default Drupal fields to manage the data in Chado columns. For
  151. * custom handling of columns there are custom TripalEntity extensions that
  152. * are added by the tripal_chado_field_create_info_custom() function. A
  153. * custom field will superceed any default base field of the same name
  154. * provided here.
  155. *
  156. * @param $field_name
  157. * The name for the new field.
  158. * @param $table_name
  159. * The Chado table
  160. * @param $schema
  161. * The Drupal schema array for the Chado table.
  162. * @param $column_name
  163. * The name of the column in the Chado table.
  164. * @return
  165. * An associative array compatible with the tripal_chado_field_create_info()
  166. * function.
  167. */
  168. function tripal_chado_field_create_info_base_defaults($field_name, $table_name,
  169. $schema, $column_name) {
  170. $details = $schema['fields'][$column_name];
  171. // Set some defaults for the field.
  172. $field = array(
  173. 'field_name' => $field_name,
  174. 'type' => '',
  175. 'cardinality' => 1,
  176. 'locked' => FALSE,
  177. 'storage' => array(
  178. 'type' => 'field_chado_storage',
  179. ),
  180. 'settings' => array(
  181. 'chado_table' => $table_name,
  182. 'chado_column' => $column_name,
  183. 'semantic_web' => '',
  184. ),
  185. );
  186. // Alter the field info array depending on the column details.
  187. switch($details['type']) {
  188. case 'char':
  189. $field['type'] = 'text';
  190. $field['settings']['max_length'] = $details['length'];
  191. break;
  192. case 'varchar':
  193. $field['type'] = 'text';
  194. $field['settings']['max_length'] = $details['length'];
  195. break;
  196. case 'text':
  197. $field['type'] = 'text';
  198. $field['settings']['max_length'] = 17179869184;
  199. $field['settings']['text_processing'] = 1;
  200. break;
  201. case 'blob':
  202. // not sure how to support a blob field.
  203. continue;
  204. break;
  205. case 'int':
  206. $field['type'] = 'number_integer';
  207. break;
  208. case 'float':
  209. $field['type'] = 'number_float';
  210. $field['settings']['precision'] = 10;
  211. $field['settings']['scale'] = 2;
  212. $field['settings']['decimal_separator'] = '.';
  213. break;
  214. case 'numeric':
  215. $field['type'] = 'number_decimal';
  216. break;
  217. case 'serial':
  218. // Serial fields are most likely not needed as a field.
  219. break;
  220. case 'boolean':
  221. $field['type'] = 'list_boolean';
  222. $field['settings']['allowed_values'] = array(0 => "No", 1 => "Yes");
  223. break;
  224. case 'datetime':
  225. // Use the Drupal Date and Date API to create the field/widget
  226. $field['type'] = 'datetime';
  227. break;
  228. }
  229. // Set some default semantic web information
  230. if ($column_name == 'uniquename') {
  231. $field['settings']['text_processing'] = 0;
  232. }
  233. //
  234. // PUB TABLE
  235. //
  236. elseif ($table_name == 'pub' and $column_name == 'uniquename') {
  237. $field['type'] = 'text';
  238. $field['settings']['text_processing'] = 0;
  239. }
  240. //
  241. // ANALYSIS TABLE
  242. //
  243. elseif ($table_name == 'analysis' and $column_name == 'sourceuri') {
  244. $field['type'] = 'text';
  245. $field['settings']['text_processing'] = 0;
  246. }
  247. return $field;
  248. }
  249. /**
  250. * Implements hook_field_create_instance_info().
  251. *
  252. * This is a Tripal defined hook that supports integration with the
  253. * TripalEntity field.
  254. */
  255. function tripal_chado_field_create_instance_info($entity_type, $bundle, $term) {
  256. // Get the details about the mapping of this bundle to the Chado table:
  257. $details = array(
  258. 'chado_cv_id' => tripal_get_bundle_variable('chado_cv_id', $bundle->id),
  259. 'chado_cvterm_id' => tripal_get_bundle_variable('chado_cvterm_id', $bundle->id),
  260. 'chado_table' => tripal_get_bundle_variable('chado_table', $bundle->id),
  261. 'chado_type_table' => tripal_get_bundle_variable('chado_type_table', $bundle->id),
  262. 'chado_type_column' => tripal_get_bundle_variable('chado_type_column', $bundle->id),
  263. );
  264. $base_fields = tripal_chado_field_create_base('create_instance_info', $entity_type, $bundle, $details);
  265. $custom_fields = tripal_chado_field_create_instance_info_custom($entity_type, $bundle, $details);
  266. return array_merge($base_fields, $custom_fields);
  267. }
  268. /**
  269. * A helper function for the tripal_chado_field_create_instance_info() function.
  270. *
  271. * This function generates the default chado_instance_info array for a column in
  272. * a base table of Chado. All of fields returned by this function use
  273. * default Drupal fields to manage the data in Chado columns. For
  274. * custom handling of columns there are custom TripalEntity extensions that
  275. * are added by the tripal_chado_field_create_info_custom() function. A
  276. * custom field will superceed any default base field of the same name
  277. * provided here.
  278. *
  279. * @param $bundle_name
  280. * The name of the bundle to which this field will be attached.
  281. * @param $field_name
  282. * The name for the new field.
  283. * @param $table_name
  284. * The Chado table
  285. * @param $schema
  286. * The Drupal schema array for the Chado table.
  287. * @param $column_name
  288. * The name of the column in the Chado table.
  289. * @return
  290. * An associative array compatible with the tripal_chado_field_create_info()
  291. * function.
  292. */
  293. function tripal_chado_field_create_instance_info_base_defaults($bundle_name,
  294. $field_name, $table_name, $schema, $column_name) {
  295. $details = $schema['fields'][$column_name];
  296. $field = array(
  297. 'field_name' => $field_name,
  298. 'entity_type' => 'TripalEntity',
  299. 'bundle' => $bundle_name,
  300. 'label' => ucwords(preg_replace('/_/', ' ', $column_name)),
  301. 'description' => '',
  302. 'required' => FALSE,
  303. 'settings' => array(
  304. 'auto_attach' => TRUE,
  305. ),
  306. 'widget' => array(
  307. 'settings' => array(
  308. 'display_label' => 1,
  309. ),
  310. ),
  311. 'display' => array(
  312. 'default' => array(
  313. 'label' => 'above',
  314. 'settings' => array(),
  315. ),
  316. ),
  317. );
  318. // Determine if the field is required.
  319. if (array_key_exists('not null', $details) and $details['not null'] === TRUE) {
  320. $field_info['required'] = TRUE;
  321. }
  322. // Alter the field info array depending on the column details.
  323. switch($details['type']) {
  324. case 'char':
  325. $field['widget']['type'] = 'text_textfield';
  326. break;
  327. case 'varchar':
  328. $field['widget']['type'] = 'text_textfield';
  329. break;
  330. case 'text':
  331. $field['widget']['type'] = 'text_textarea';
  332. $field['widget']['settings']['format'] = filter_default_format();
  333. break;
  334. case 'blob':
  335. // not sure how to support a blob field.
  336. continue;
  337. break;
  338. case 'int':
  339. $field['widget']['type'] = 'number';
  340. break;
  341. case 'float':
  342. $field['widget']['type'] = 'number';
  343. break;
  344. case 'numeric':
  345. $field['widget']['type'] = 'number';
  346. break;
  347. case 'serial':
  348. // Serial fields are most likely not needed as a field.
  349. break;
  350. case 'boolean':
  351. $field['widget']['type'] = 'options_onoff';
  352. break;
  353. case 'datetime':
  354. $field['widget']['type'] = 'date_select';
  355. $field['widget']['settings']['increment'] = 1;
  356. $field['widget']['settings']['tz_handling'] = 'none';
  357. $field['widget']['settings']['collapsible'] = TRUE;
  358. // TODO: Add settings so that the minutes increment by 1.
  359. // And turn off the timezone, as the Chado field doesn't support it.
  360. break;
  361. }
  362. // Set some default semantic web information
  363. if ($column_name == 'uniquename') {
  364. $field['widget_type'] = 'text_textfield';
  365. }
  366. elseif ($field['label'] == 'Timeaccessioned') {
  367. $field['label'] = 'Time Accessioned';
  368. $field['description'] = 'Please enter the time that this record was first added to the database.';
  369. }
  370. elseif ($field['label'] == 'Timelastmodified') {
  371. $field['label'] = 'Time Last Modified';
  372. $field['description'] = 'Please enter the time that this record was last modified. The default is the current time.';
  373. }
  374. //
  375. // ORGANISM TABLE
  376. //
  377. elseif ($table_name == 'organism' and $column_name == 'comment') {
  378. $field['label'] = 'Description';
  379. }
  380. //
  381. // PUB TABLE
  382. //
  383. elseif ($table_name == 'pub' and $column_name == 'uniquename') {
  384. $field['widget_type'] = 'text_textfield';
  385. }
  386. //
  387. // ANALYSIS TABLE
  388. //
  389. elseif ($table_name == 'analysis' and $column_name == 'program') {
  390. $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.';
  391. $field['label'] = 'Program, Pipeline, Workflow or Method Name.';
  392. }
  393. elseif ($table_name == 'analysis' and $column_name == 'sourceuri') {
  394. $field['widget_type'] = 'text_textfield';
  395. $field['label'] = 'Source URL';
  396. $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.';
  397. }
  398. elseif ($table_name == 'analysis' and $column_name == 'sourcename') {
  399. $field['label'] = 'Source Name';
  400. $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.';
  401. }
  402. elseif ($table_name == 'analysis' and $column_name == 'sourceversion') {
  403. $field['label'] = 'Source Version';
  404. $field['description'] = 'If hte source data set has a version include it here.';
  405. }
  406. elseif ($table_name == 'analysis' and $column_name == 'algorithm') {
  407. $field['label'] = 'Source Version';
  408. $field['description'] = 'The name of the algorithm used to produce the dataset if different from the program.';
  409. }
  410. elseif ($table_name == 'analysis' and $column_name == 'programversion') {
  411. $field['label'] = 'Program Version';
  412. $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.';
  413. }
  414. //
  415. // PROJECT TABLE
  416. //
  417. elseif ($table_name == 'project' and $column_name == 'description') {
  418. $field['label'] = 'Short Description';
  419. }
  420. return $field;
  421. }
  422. /**
  423. * A helper function for the tripal_chado_field_create_instance_info() function.
  424. *
  425. * This function adds in the custom fields info by instantiating the class
  426. * for the custom field, calling the create_instance_info() function and
  427. * returning the info array.
  428. *
  429. * @param $entity_type
  430. * The type of entity (e.g TripalEntity)
  431. * @param $bundle
  432. * The bundle object.
  433. * @param $details
  434. * An array containing the mapping of the bundle to the Chado table.
  435. */
  436. function tripal_chado_field_create_instance_info_custom($entity_type, $bundle, $details) {
  437. $info = array();
  438. // Find all of the files in the tripal_chado/includes/fields directory.
  439. $fields_path = drupal_get_path('module', 'tripal_chado') . '/includes/fields';
  440. $field_files = file_scan_directory($fields_path, '/^chado_.*\.inc$/');
  441. // Iterate through the fields, include the file and run the info function.
  442. foreach ($field_files as $file) {
  443. $field_type = $file->name;
  444. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  445. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  446. $field_obj = new $field_type();
  447. $result = $field_obj->create_instance_info($entity_type, $bundle, $details);
  448. if (is_array($result)) {
  449. $info[$result['field_name']] = $result;
  450. }
  451. }
  452. }
  453. return $info;
  454. }
  455. /**
  456. * Implements hook_field_widget_info().
  457. *
  458. * This function would normally provide a large info array for all of the
  459. * widgets provided by this module. But instead it will call a hook that
  460. * can be implmented within each individual field file. This will allow
  461. * all of the code for a single field to be self contained in a single file.
  462. */
  463. function tripal_chado_field_widget_info() {
  464. $widgets = array();
  465. $fields = field_info_fields();
  466. foreach ($fields as $field) {
  467. $field_type = $field['type'];
  468. if ($field['storage']['type'] == 'field_chado_storage') {
  469. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  470. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  471. $field_obj = new $field_type();
  472. $widgets[$field_type . '_widget'] = $field_obj->widget_info();
  473. }
  474. }
  475. }
  476. return $widgets;
  477. }
  478. /**
  479. * Implements hook_field_formatter_info().
  480. *
  481. * This function would normally provide a large info array for all of the
  482. * formatters provided by this module. But instead it will call a hook that
  483. * can be implmented within each individual field file. This will allow
  484. * all of the code for a single field to be self contained in a single file.
  485. */
  486. function tripal_chado_field_formatter_info() {
  487. $formatters = array();
  488. $fields = field_info_fields();
  489. foreach ($fields as $field) {
  490. $field_type = $field['type'];
  491. if ($field['storage']['type'] == 'field_chado_storage') {
  492. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  493. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  494. $field_obj = new $field_type();
  495. $formatters[$field_type . '_formatter'] = $field_obj->formatter_info();
  496. }
  497. }
  498. }
  499. return $formatters;
  500. }
  501. /**
  502. * Implements hook_field_settings_form()
  503. */
  504. function tripal_chado_field_settings_form($field, $instance, $has_data) {
  505. $form = '';
  506. $field_type = $field['type'];
  507. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  508. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  509. $field_obj = new $field_type();
  510. $form = $field_obj->settings_form($field, $instance, $has_data);
  511. }
  512. return $form;
  513. }
  514. /**
  515. * Implements hook_field_formatter_settings_summary().
  516. */
  517. function tripal_chado_field_formatter_settings_summary($field, $instance, $view_mode) {
  518. $summary = '';
  519. $field_type = $field['type'];
  520. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  521. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  522. $field = new $field_type();
  523. $summary = $field->formatter_settings_summary($field, $instance, $view_mode);
  524. }
  525. return $summary;
  526. }
  527. /**
  528. * Implements hook_field_formatter_settings_form().
  529. */
  530. function tripal_chado_field_formatter_settings_form($field, $instance,
  531. $view_mode, $form, &$form_state) {
  532. $element = array();
  533. $field_type = $field['type'];
  534. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/' . $field_type);
  535. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  536. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  537. $field_obj = new $field_type();
  538. $element = $field_obj->formatter_settings_form($field, $instance, $view_mode, $form, $form_state);
  539. }
  540. $function = $field_type . '_formatter_settings_form';
  541. if (function_exists($function)) {
  542. $element = $function($field, $instance, $view_mode, $form, $form_state);
  543. }
  544. return $element;
  545. }
  546. /**
  547. * Implements hook_field_formatter_view().
  548. */
  549. function tripal_chado_field_formatter_view($entity_type, $entity, $field,
  550. $instance, $langcode, $items, $display) {
  551. $element = array();
  552. $field_type = $field['type'];
  553. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  554. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  555. $field_obj = new $field_type();
  556. $field_obj->formatter_view($element, $entity_type, $entity, $field, $instance, $langcode, $items, $display);
  557. }
  558. return $element;
  559. }
  560. /**
  561. * Implements hook_field_widget_form().
  562. */
  563. function tripal_chado_field_widget_form(&$form, &$form_state, $field,
  564. $instance, $langcode, $items, $delta, $element) {
  565. $widget = $element;
  566. $field_name = $instance['field_name'];
  567. $field_type = $field['type'];
  568. form_load_include($form_state, 'inc', 'tripal_chado', 'includes/fields/' . $field_type);
  569. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_name);
  570. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  571. $field_obj = new $field_type();
  572. $field_obj->widget_form($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  573. }
  574. return $widget;
  575. }
  576. /**
  577. * Implements hook_field_widget_form_alter().
  578. */
  579. function tripal_chado_field_widget_form_alter(&$element, &$form_state, $context) {
  580. if (array_key_exists('#field_name', $element)) {
  581. $field_name = $element['#field_name'];
  582. $matches = array();
  583. if (preg_match('/(.+?)__(.+?)$/', $field_name, $matches)) {
  584. $tablename = $matches[1];
  585. $colname = $matches[2];
  586. $schema = chado_get_schema($tablename);
  587. if (!$schema) {
  588. return;
  589. }
  590. // The timelastmodified field exists in many Chado tables. We want
  591. // the form element to update to the most recent time rather than the time
  592. // in the database.
  593. if ($colname == 'timelastmodified' and $schema['fields'][$colname]['type'] == 'datetime') {
  594. // We want the default value for the field to be the current time.
  595. $element['#default_value']['value'] = format_date(time(), 'custom', "Y-m-d H:i:s", 'UTC');
  596. $element['#date_items']['value'] = $element['#default_value']['value'];
  597. }
  598. // We want the date combo fieldset to be collaspible so we will
  599. // add our own theme_wrapper to replace the one added by the date
  600. // module.
  601. if (array_key_exists($colname, $schema['fields']) and $schema['fields'][$colname]['type'] == 'datetime') {
  602. $element['#theme_wrappers'] = array('tripal_chado_date_combo');
  603. }
  604. }
  605. }
  606. }
  607. /**
  608. * Implements hook_field_validate()
  609. */
  610. function tripal_chado_field_validate($entity_type, $entity, $field, $instance,
  611. $langcode, $items, &$errors) {
  612. $field_type = $field['type'];
  613. if ($field['storage']['type'] == 'field_chado_storage') {
  614. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  615. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  616. $field_obj = new $field_type();
  617. $field_obj->validate($entity_type, $entity, $field, $instance,
  618. $langcode, $items, $errors);
  619. }
  620. }
  621. }
  622. /**
  623. * Implements hook_field_validate()
  624. */
  625. function tripal_chado_field_submit($entity_type, $entity, $field, $instance,
  626. $langcode, &$items, $form, &$form_state) {
  627. $field_type = $field['type'];
  628. if ($field['storage']['type'] == 'field_chado_storage') {
  629. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  630. if (preg_match('/^chado/', $field_type) and class_exists($field_type)) {
  631. $field_obj = new $field_type();
  632. $field_obj->submit($entity_type, $entity, $field, $instance,
  633. $langcode, $items, $form, $form_state);
  634. }
  635. }
  636. }
  637. /**
  638. * Implements hook_form_FORM_ID_alter().
  639. *
  640. * The field_ui_display_overview_form is used for formatting the display
  641. * or layout of fields attached to an entity and shown on the entity view page.
  642. *
  643. * This function removes the cvterm class and property adder field as those are
  644. * really not meant for users to show or manage.
  645. */
  646. function tripal_chado_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
  647. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  648. // on the add/edit form of an entity for adding new property fields.
  649. $fields_names = element_children($form['fields']);
  650. foreach ($fields_names as $field_name) {
  651. $field_info = field_info_field($field_name);
  652. if ($field_info['type'] == 'kvproperty_adder') {
  653. unset($form['fields'][$field_name]);
  654. }
  655. if ($field_info['type'] == 'cvterm_class_adder') {
  656. unset($form['fields'][$field_name]);
  657. }
  658. }
  659. }
  660. /**
  661. * Implements hook_form_FORM_ID_alter().
  662. *
  663. * The field_ui_field_overview_form is used for ordering and configuring the
  664. * fields attached to an entity.
  665. *
  666. * This function removes the property adder field as that is really not meant
  667. * for users to show or manage.
  668. */
  669. function tripal_chado_form_field_ui_field_overview_form_alter(&$form, &$form_state, $form_id) {
  670. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  671. // on the add/edit form of an entity for adding new property fields.
  672. $fields_names = element_children($form['fields']);
  673. foreach ($fields_names as $field_name) {
  674. $field_info = field_info_field($field_name);
  675. if ($field_info['type'] == 'kvproperty_adder') {
  676. unset($form['fields'][$field_name]);
  677. }
  678. if ($field_info['type'] == 'cvterm_class_adder') {
  679. unset($form['fields'][$field_name]);
  680. }
  681. }
  682. }
  683. /**
  684. * Implements hook_field_is_empty().
  685. */
  686. function tripal_chado_field_is_empty($item, $field) {
  687. // If there is no value field then the field is empty.
  688. if (!array_key_exists('value', $item)) {
  689. return TRUE;
  690. }
  691. // Iterate through all of the fields and if at least one has a value
  692. // the field is not empty.
  693. foreach ($item as $form_field_name => $value) {
  694. if (isset($value) and $value != NULL and $value != '') {
  695. return FALSE;
  696. }
  697. }
  698. // Otherwise, the field is empty.
  699. return TRUE;
  700. }