tripal_chado.fields.inc 24 KB

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