tripal_chado.fields.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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. $field_types = tripal_get_field_types('tripal_chado');
  19. foreach ($field_types as $field_type) {
  20. $info[$field_type] = $field_type::fieldInfo();
  21. }
  22. return $info;
  23. }
  24. /**
  25. * Implements hook_field_create_info().
  26. *
  27. * This is a Tripal defined hook that supports integration with the
  28. * TripalEntity field.
  29. */
  30. function tripal_chado_field_create_info($entity_type, $bundle) {
  31. // Get the table this bundle is mapped to.
  32. $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  33. $vocab = $term->vocab;
  34. $params = array(
  35. 'vocabulary' => $vocab->vocabulary,
  36. 'accession' => $term->accession,
  37. );
  38. $mapped_table = chado_get_cvterm_mapping($params);
  39. // Get the details about the mapping of this bundle to the Chado table:
  40. $details = array(
  41. 'chado_cv_id' => $mapped_table->cvterm->cv_id->cv_id,
  42. 'chado_cvterm_id' => $mapped_table->cvterm->cvterm_id,
  43. 'chado_table' => $mapped_table->chado_table,
  44. 'chado_type_table' => $mapped_table->chado_table,
  45. 'chado_type_column' => $mapped_table->chado_field,
  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. $fields = tripal_get_fields('tripal_chado', $entity_type, $bundle, $details);
  68. foreach ($fields as $field) {
  69. $field_name = $field->getFieldName();
  70. if ($field->canAttach()) {
  71. $info[$field_name] = $field->createInfo();
  72. }
  73. }
  74. return $info;
  75. }
  76. /**
  77. * Retrieves either the create_info or create_instance_info arrays.
  78. *
  79. * The logic for creating the fields for the base table is so similar for
  80. * both the create_info and create_instance_info arrays they are both
  81. * handled by this function to prevent duplication of code.
  82. *
  83. * @param $step
  84. * Set to 'create_info' to retrun the create_info array or
  85. * 'create_instance_info' to return the create_instance_info array.
  86. * @param $entity_type
  87. * The type of entity (e.g TripalEntity)
  88. * @param $bundle
  89. * The bundle object.
  90. * @param $details
  91. * An array containing the mapping of the bundle to the Chado table.
  92. *
  93. * @return
  94. * An array compabile with the tripal_chado_field_create_info() and
  95. * tripal_chado_field_create_instance_info() functions.
  96. */
  97. function tripal_chado_field_create_base($step, $entity_type, $bundle, $details) {
  98. $fields = array();
  99. // Get Chado information
  100. $table_name = $details['chado_table'];
  101. $type_table = $details['chado_type_table'];
  102. $type_field = $details['chado_type_column'];
  103. // Iterate through the columns of the table and see if fields have been
  104. // created for each one. If not, then create them.
  105. $schema = chado_get_schema($table_name);
  106. if (!$schema) {
  107. return $fields;
  108. }
  109. $columns = $schema['fields'];
  110. foreach ($columns as $column_name => $details) {
  111. $field_name = $table_name . '__' . $column_name;
  112. // Skip the primary key field.
  113. if ($column_name == $schema['primary key'][0]) {
  114. continue;
  115. }
  116. // Skip the type field.
  117. if ($table_name == $type_table and $column_name == $type_field) {
  118. continue;
  119. }
  120. // Get the field defaults for this column.
  121. $field_info = array();
  122. if ($step == 'create_info') {
  123. $field_info = tripal_chado_field_create_info_base_defaults($field_name,
  124. $table_name, $schema, $column_name);
  125. }
  126. if ($step == 'create_instance_info') {
  127. $field_info = tripal_chado_field_create_instance_info_base_defaults($bundle->name,
  128. $field_name, $table_name, $schema, $column_name);
  129. }
  130. // TODO: add in a call to drupal_alter to allow other modules to change
  131. // the field settings.
  132. // Add the field to the bundle.
  133. $fields[$field_name] = $field_info;
  134. }
  135. return $fields;
  136. }
  137. /**
  138. * A helper function for the tripal_chado_field_create_info() function.
  139. *
  140. * This function generates the default chado_info array for a column in
  141. * a base table of Chado. All of fields returned by this function use
  142. * default Drupal fields to manage the data in Chado columns. For
  143. * custom handling of columns there are custom TripalEntity extensions that
  144. * are added by the tripal_chado_field_create_info_custom() function. A
  145. * custom field will superceed any default base field of the same name
  146. * provided here.
  147. *
  148. * @param $field_name
  149. * The name for the new field.
  150. * @param $table_name
  151. * The Chado table
  152. * @param $schema
  153. * The Drupal schema array for the Chado table.
  154. * @param $column_name
  155. * The name of the column in the Chado table.
  156. * @return
  157. * An associative array compatible with the tripal_chado_field_create_info()
  158. * function.
  159. */
  160. function tripal_chado_field_create_info_base_defaults($field_name, $table_name,
  161. $schema, $column_name) {
  162. $details = $schema['fields'][$column_name];
  163. // Set some defaults for the field.
  164. $field = array(
  165. 'field_name' => $field_name,
  166. 'type' => '',
  167. 'cardinality' => 1,
  168. 'locked' => FALSE,
  169. 'storage' => array(
  170. 'type' => 'field_chado_storage',
  171. ),
  172. 'settings' => array(
  173. 'chado_table' => $table_name,
  174. 'chado_column' => $column_name,
  175. 'semantic_web' => tripal_get_chado_semweb_term($table_name, $column_name),
  176. ),
  177. );
  178. // Alter the field info array depending on the column details.
  179. switch($details['type']) {
  180. case 'char':
  181. $field['type'] = 'text';
  182. $field['settings']['max_length'] = $details['length'];
  183. break;
  184. case 'varchar':
  185. $field['type'] = 'text';
  186. $field['settings']['max_length'] = $details['length'];
  187. break;
  188. case 'text':
  189. $field['type'] = 'text';
  190. $field['settings']['max_length'] = 17179869184;
  191. $field['settings']['text_processing'] = 1;
  192. break;
  193. case 'blob':
  194. // not sure how to support a blob field.
  195. continue;
  196. break;
  197. case 'int':
  198. $field['type'] = 'number_integer';
  199. break;
  200. case 'float':
  201. $field['type'] = 'number_float';
  202. $field['settings']['precision'] = 10;
  203. $field['settings']['scale'] = 2;
  204. $field['settings']['decimal_separator'] = '.';
  205. break;
  206. case 'numeric':
  207. $field['type'] = 'number_decimal';
  208. break;
  209. case 'serial':
  210. // Serial fields are most likely not needed as a field.
  211. break;
  212. case 'boolean':
  213. $field['type'] = 'list_boolean';
  214. $field['settings']['allowed_values'] = array(0 => "No", 1 => "Yes");
  215. break;
  216. case 'datetime':
  217. // Use the Drupal Date and Date API to create the field/widget
  218. $field['type'] = 'datetime';
  219. break;
  220. }
  221. // Set some default semantic web information
  222. if ($column_name == 'uniquename') {
  223. $field['settings']['text_processing'] = 0;
  224. }
  225. //
  226. // PUB TABLE
  227. //
  228. elseif ($table_name == 'pub' and $column_name == 'uniquename') {
  229. $field['type'] = 'text';
  230. $field['settings']['text_processing'] = 0;
  231. }
  232. //
  233. // ANALYSIS TABLE
  234. //
  235. elseif ($table_name == 'analysis' and $column_name == 'sourceuri') {
  236. $field['type'] = 'text';
  237. $field['settings']['text_processing'] = 0;
  238. }
  239. return $field;
  240. }
  241. /**
  242. * Implements hook_field_create_instance_info().
  243. *
  244. * This is a Tripal defined hook that supports integration with the
  245. * TripalEntity field.
  246. */
  247. function tripal_chado_field_create_instance_info($entity_type, $bundle) {
  248. $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  249. $vocab = $term->vocab;
  250. $params = array(
  251. 'vocabulary' => $vocab->vocabulary,
  252. 'accession' => $term->accession,
  253. );
  254. $mapped_table = chado_get_cvterm_mapping($params);
  255. // Get the details about the mapping of this bundle to the Chado table:
  256. $details = array(
  257. 'chado_cv_id' => $mapped_table->cvterm->cv_id->cv_id,
  258. 'chado_cvterm_id' => $mapped_table->cvterm->cvterm_id,
  259. 'chado_table' => $mapped_table->chado_table,
  260. 'chado_type_table' => $mapped_table->chado_table,
  261. 'chado_type_column' => $mapped_table->chado_field,
  262. );
  263. $base_fields = tripal_chado_field_create_base('create_instance_info', $entity_type, $bundle, $details);
  264. $custom_fields = tripal_chado_field_create_instance_info_custom($entity_type, $bundle, $details);
  265. return array_merge($base_fields, $custom_fields);
  266. }
  267. /**
  268. * A helper function for the tripal_chado_field_create_instance_info() function.
  269. *
  270. * This function generates the default chado_instance_info array for a column in
  271. * a base table of Chado. All of fields returned by this function use
  272. * default Drupal fields to manage the data in Chado columns. For
  273. * custom handling of columns there are custom TripalEntity extensions that
  274. * are added by the tripal_chado_field_create_info_custom() function. A
  275. * custom field will superceed any default base field of the same name
  276. * provided here.
  277. *
  278. * @param $bundle_name
  279. * The name of the bundle to which this field will be attached.
  280. * @param $field_name
  281. * The name for the new field.
  282. * @param $table_name
  283. * The Chado table
  284. * @param $schema
  285. * The Drupal schema array for the Chado table.
  286. * @param $column_name
  287. * The name of the column in the Chado table.
  288. * @return
  289. * An associative array compatible with the tripal_chado_field_create_info()
  290. * function.
  291. */
  292. function tripal_chado_field_create_instance_info_base_defaults($bundle_name,
  293. $field_name, $table_name, $schema, $column_name) {
  294. $details = $schema['fields'][$column_name];
  295. $field = array(
  296. 'field_name' => $field_name,
  297. 'entity_type' => 'TripalEntity',
  298. 'bundle' => $bundle_name,
  299. 'label' => ucwords(preg_replace('/_/', ' ', $column_name)),
  300. 'description' => '',
  301. 'required' => FALSE,
  302. 'settings' => array(
  303. 'auto_attach' => TRUE,
  304. ),
  305. 'widget' => array(
  306. 'settings' => array(
  307. 'display_label' => 1,
  308. ),
  309. ),
  310. 'display' => array(
  311. 'default' => array(
  312. 'label' => 'inline',
  313. 'settings' => array(),
  314. ),
  315. ),
  316. );
  317. // Determine if the field is required.
  318. if (array_key_exists('not null', $details) and $details['not null'] === TRUE) {
  319. $field_info['required'] = TRUE;
  320. }
  321. // Alter the field info array depending on the column details.
  322. switch($details['type']) {
  323. case 'char':
  324. $field['widget']['type'] = 'text_textfield';
  325. break;
  326. case 'varchar':
  327. $field['widget']['type'] = 'text_textfield';
  328. break;
  329. case 'text':
  330. $field['widget']['type'] = 'text_textarea';
  331. $field['widget']['settings']['format'] = filter_default_format();
  332. break;
  333. case 'blob':
  334. // not sure how to support a blob field.
  335. continue;
  336. break;
  337. case 'int':
  338. $field['widget']['type'] = 'number';
  339. break;
  340. case 'float':
  341. $field['widget']['type'] = 'number';
  342. break;
  343. case 'numeric':
  344. $field['widget']['type'] = 'number';
  345. break;
  346. case 'serial':
  347. // Serial fields are most likely not needed as a field.
  348. break;
  349. case 'boolean':
  350. $field['widget']['type'] = 'options_onoff';
  351. break;
  352. case 'datetime':
  353. $field['widget']['type'] = 'date_select';
  354. $field['widget']['settings']['increment'] = 1;
  355. $field['widget']['settings']['tz_handling'] = 'none';
  356. $field['widget']['settings']['collapsible'] = TRUE;
  357. // TODO: Add settings so that the minutes increment by 1.
  358. // And turn off the timezone, as the Chado field doesn't support it.
  359. break;
  360. }
  361. // Set some default semantic web information
  362. if ($column_name == 'uniquename') {
  363. $field['label'] = 'Identifier';
  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. $fields = tripal_get_fields('tripal_chado', $entity_type, $bundle, $details);
  439. foreach ($fields as $field) {
  440. $field_name = $field->getFieldName();
  441. if ($field->canAttach()) {
  442. $info[$field_name] = $field->createInstanceInfo();
  443. }
  444. }
  445. return $info;
  446. }
  447. /**
  448. * Implements hook_field_widget_info().
  449. *
  450. * This function would normally provide a large info array for all of the
  451. * widgets provided by this module. But instead it will call a hook that
  452. * can be implmented within each individual field file. This will allow
  453. * all of the code for a single field to be self contained in a single file.
  454. */
  455. function tripal_chado_field_widget_info() {
  456. $info = array();
  457. $field_types = tripal_get_field_types('tripal_chado');
  458. foreach ($field_types as $field_type) {
  459. $info += $field_type::widgetInfo();
  460. }
  461. return $info;
  462. }
  463. /**
  464. * Implements hook_field_formatter_info().
  465. *
  466. * This function would normally provide a large info array for all of the
  467. * formatters provided by this module. But instead it will call a hook that
  468. * can be implmented within each individual field file. This will allow
  469. * all of the code for a single field to be self contained in a single file.
  470. */
  471. function tripal_chado_field_formatter_info() {
  472. $info = array();
  473. $field_types = tripal_get_field_types('tripal_chado');
  474. foreach ($field_types as $field_type) {
  475. $info += $field_type::formatterInfo();
  476. }
  477. return $info;
  478. }
  479. /**
  480. * Implements hook_field_settings_form()
  481. */
  482. function tripal_chado_field_settings_form($field, $instance, $has_data) {
  483. $form = array();
  484. $field_type = $field['type'];
  485. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  486. if (class_exists($field_type)) {
  487. $form = $field_type::settingsForm($field, $instance, $has_data);
  488. }
  489. return $form;
  490. }
  491. /**
  492. * Implements hook_field_formatter_settings_summary().
  493. */
  494. function tripal_chado_field_formatter_settings_summary($field, $instance, $view_mode) {
  495. $summary = '';
  496. $field_type = $field['type'];
  497. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  498. if (class_exists($field_type)) {
  499. $form = $field_type::formatterSettingsSummary($field, $instance, $view_mode);
  500. }
  501. return $summary;
  502. }
  503. /**
  504. * Implements hook_field_formatter_settings_form().
  505. */
  506. function tripal_chado_field_formatter_settings_form($field, $instance,
  507. $view_mode, $form, &$form_state) {
  508. $form = array();
  509. $field_type = $field['type'];
  510. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  511. if (class_exists($field_type)) {
  512. $form = $field_type::formatterSettingsForm(field, $instance, $view_mode, $form, $form_state);
  513. }
  514. return $form;
  515. }
  516. /**
  517. * Implements hook_field_formatter_view().
  518. */
  519. function tripal_chado_field_formatter_view($entity_type, $entity, $field,
  520. $instance, $langcode, $items, $display) {
  521. $element = array();
  522. $field_type = $field['type'];
  523. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  524. if (class_exists($field_type)) {
  525. $field_type::formatterView($element, $entity_type, $entity, $field, $instance, $langcode, $items, $display);
  526. }
  527. return $element;
  528. }
  529. /**
  530. * Implements hook_field_widget_form().
  531. */
  532. function tripal_chado_field_widget_form(&$form, &$form_state, $field,
  533. $instance, $langcode, $items, $delta, $element) {
  534. $widget = $element;
  535. $field_type = $field['type'];
  536. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  537. if (class_exists($field_type)) {
  538. $field_type::widgetForm($widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  539. }
  540. return $widget;
  541. }
  542. /**
  543. * Implements hook_field_widget_form_alter().
  544. */
  545. function tripal_chado_field_widget_form_alter(&$element, &$form_state, $context) {
  546. if (array_key_exists('#field_name', $element)) {
  547. $field_name = $element['#field_name'];
  548. $matches = array();
  549. if (preg_match('/(.+?)__(.+?)$/', $field_name, $matches)) {
  550. $tablename = $matches[1];
  551. $colname = $matches[2];
  552. $schema = chado_get_schema($tablename);
  553. if (!$schema) {
  554. return;
  555. }
  556. // The timelastmodified field exists in many Chado tables. We want
  557. // the form element to update to the most recent time rather than the time
  558. // in the database.
  559. if ($colname == 'timelastmodified' and $schema['fields'][$colname]['type'] == 'datetime') {
  560. // We want the default value for the field to be the current time.
  561. $element['#default_value']['value'] = format_date(time(), 'custom', "Y-m-d H:i:s", 'UTC');
  562. $element['#date_items']['value'] = $element['#default_value']['value'];
  563. }
  564. // We want the date combo fieldset to be collaspible so we will
  565. // add our own theme_wrapper to replace the one added by the date
  566. // module.
  567. if (array_key_exists($colname, $schema['fields']) and $schema['fields'][$colname]['type'] == 'datetime') {
  568. $element['#theme_wrappers'] = array('tripal_chado_date_combo');
  569. }
  570. }
  571. }
  572. }
  573. /**
  574. * Implements hook_field_validate()
  575. */
  576. function tripal_chado_field_validate($entity_type, $entity, $field, $instance,
  577. $langcode, $items, &$errors) {
  578. $field_type = $field['type'];
  579. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  580. if (class_exists($field_type)) {
  581. $field_obj = new $field_type($entity_type, $entity->bundle);
  582. $form = $field_obj::widgetFormValidate($entity_type, $entity, $field, $instance,
  583. $langcode, $items, $errors);
  584. }
  585. }
  586. /**
  587. * Implements hook_field_validate()
  588. *
  589. * This is a TripalEntity specific hook.
  590. */
  591. function tripal_chado_field_submit($entity_type, $entity, $field, $instance,
  592. $langcode, &$items, $form, &$form_state) {
  593. $field_type = $field['type'];
  594. module_load_include('inc', 'tripal_chado', 'includes/fields/' . $field_type);
  595. if (class_exists($field_type)) {
  596. $field_obj = new $field_type($entity_type, $entity->bundle);
  597. $form = $field_obj::widgetFormSubmit($entity_type, $entity, $field, $instance,
  598. $langcode, $items, $errors);
  599. }
  600. }
  601. /**
  602. * Implements hook_form_FORM_ID_alter().
  603. *
  604. * The field_ui_display_overview_form is used for formatting the display
  605. * or layout of fields attached to an entity and shown on the entity view page.
  606. *
  607. * This function removes the cvterm class and property adder field as those are
  608. * really not meant for users to show or manage.
  609. */
  610. function tripal_chado_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
  611. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  612. // on the add/edit form of an entity for adding new property fields.
  613. $fields_names = element_children($form['fields']);
  614. foreach ($fields_names as $field_name) {
  615. $field_info = field_info_field($field_name);
  616. if ($field_info['type'] == 'kvproperty_adder') {
  617. unset($form['fields'][$field_name]);
  618. }
  619. if ($field_info['type'] == 'cvterm_class_adder') {
  620. unset($form['fields'][$field_name]);
  621. }
  622. }
  623. }
  624. /**
  625. * Implements hook_form_FORM_ID_alter().
  626. *
  627. * The field_ui_field_overview_form is used for ordering and configuring the
  628. * fields attached to an entity.
  629. *
  630. * This function removes the property adder field as that is really not meant
  631. * for users to show or manage.
  632. */
  633. function tripal_chado_form_field_ui_field_overview_form_alter(&$form, &$form_state, $form_id) {
  634. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  635. // on the add/edit form of an entity for adding new property fields.
  636. $fields_names = element_children($form['fields']);
  637. foreach ($fields_names as $field_name) {
  638. $field_info = field_info_field($field_name);
  639. if ($field_info['type'] == 'kvproperty_adder') {
  640. unset($form['fields'][$field_name]);
  641. }
  642. if ($field_info['type'] == 'cvterm_class_adder') {
  643. unset($form['fields'][$field_name]);
  644. }
  645. }
  646. }
  647. /**
  648. * Implements hook_field_is_empty().
  649. */
  650. function tripal_chado_field_is_empty($item, $field) {
  651. // If there is no value field then the field is empty.
  652. if (!array_key_exists('value', $item)) {
  653. return TRUE;
  654. }
  655. // Iterate through all of the fields and if at least one has a value
  656. // the field is not empty.
  657. foreach ($item as $form_field_name => $value) {
  658. if (isset($value) and $value != NULL and $value != '') {
  659. return FALSE;
  660. }
  661. }
  662. // Otherwise, the field is empty.
  663. return TRUE;
  664. }