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