tripal.fields.inc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. <?php
  2. /**
  3. * Implements hook_field_create_instance().
  4. */
  5. function tripal_field_create_instance($instance) {
  6. $field = field_info_field($instance['field_name']);
  7. $field_class = $field['type'];
  8. if (tripal_load_include_field_class($field_class)) {
  9. $field = new $field_class($field, $instance);
  10. return $field->createInstance();
  11. }
  12. }
  13. /**
  14. * Implements hook_field_info().
  15. *
  16. * We want the Tripal module to handle all TripalFields. This will allow
  17. * other modules to be more easily disabled/enabled because Drupal won't
  18. * let a module be disabled if it supports fields that are actively attached
  19. * to bundles. Therefore any module that provides a new TripalField will be
  20. * discovered and listed for Drupal by this function.
  21. */
  22. function tripal_field_info() {
  23. $info = array();
  24. $field_types = tripal_get_field_types();
  25. foreach ($field_types as $field_type) {
  26. $info[$field_type] = $field_type::info();
  27. }
  28. return $info;
  29. }
  30. /**
  31. * Implements hook_info_alter().
  32. */
  33. function tripal_field_info_alter(&$info) {
  34. // Make sure all fields have a term setting so we can map
  35. // all fields to a vocabulary term for the semantic web.
  36. foreach ($info as $field_name => $details) {
  37. if(array_key_exists('instance_settings', $details)) {
  38. if (!array_key_exists('term_vocabulary', $details['instance_settings'])) {
  39. $info[$field_name]['instance_settings']['term_vocabulary'] = '';
  40. }
  41. if (!array_key_exists('term_name', $details['instance_settings'])) {
  42. $info[$field_name]['instance_settings']['term_name'] = '';
  43. }
  44. if (!array_key_exists('term_accession', $details['instance_settings'])) {
  45. $info[$field_name]['instance_settings']['term_accession'] = '';
  46. }
  47. if (!array_key_exists('term_fixed', $details['instance_settings'])) {
  48. $info[$field_name]['instance_settings']['term_fixed'] = FALSE;
  49. }
  50. if (!array_key_exists('auto_attach', $details['instance_settings'])) {
  51. $info[$field_name]['instance_settings']['auto_attach'] = TRUE;
  52. }
  53. }
  54. else {
  55. $info[$field_name]['instance_settings']['term_vocabulary'] = '';
  56. $info[$field_name]['instance_settings']['term_name'] = '';
  57. $info[$field_name]['instance_settings']['term_accession'] = '';
  58. $info[$field_name]['instance_settings']['term_fixed'] = FALSE;
  59. $info[$field_name]['instance_settings']['auto_attach'] = TRUE;
  60. }
  61. }
  62. }
  63. /**
  64. * Implements hook_field_widget_info();
  65. */
  66. function tripal_field_widget_info() {
  67. $info = array();
  68. $widgets = tripal_get_field_widgets();
  69. foreach ($widgets as $widget) {
  70. $info[$widget] = $widget::info();
  71. }
  72. return $info;
  73. }
  74. /**
  75. * Implements hook_field_widget_info_alter();
  76. */
  77. function tripal_field_widget_info_alter(&$info) {
  78. }
  79. /**
  80. * Implements hook_field_views_data();
  81. */
  82. function tripal_field_views_data($field) {
  83. $data = array();
  84. $field_type = $field['type'];
  85. $field_types = tripal_get_field_types();
  86. // Iterate through the bundles to which this field is attached and
  87. // if it is a TripalField field then we'll call the viewsDataAlater function.
  88. $bundles = $field['bundles']['TripalEntity'];
  89. foreach ($bundles as $bundle_name) {
  90. $instance = field_info_instance('TripalEntity', $field['field_name'], $bundle_name);
  91. if (in_array($field_type, $field_types)) {
  92. $tfield = new $field_type($field, $instance);
  93. // Fields should be associated with the bundle's term identifier
  94. // (i.e. [vocab]__[accession].
  95. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  96. $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  97. $view_base_id = $term->vocab->vocabulary . '__' . $term->accession;
  98. $data += $tfield->viewsData($view_base_id);
  99. }
  100. }
  101. return $data;
  102. }
  103. /**
  104. * Implements hook_field_formatter_info().
  105. */
  106. function tripal_field_formatter_info() {
  107. $info = array();
  108. $formatters = tripal_get_field_formatters();
  109. foreach ($formatters as $formatter) {
  110. $info[$formatter] = $formatter::info();
  111. }
  112. return $info;
  113. }
  114. /**
  115. * Implements hook_field_formatter_info_alter();
  116. */
  117. function tripal_field_formatter_info_alter(&$info) {
  118. }
  119. /**
  120. * Implements hook_bundle_create().
  121. *
  122. * This is a Tripal defined hook and is called in the
  123. * TripalBundleController::create()
  124. * function to allow modules to perform tasks when a bundle is created.
  125. */
  126. function tripal_bundle_create($bundle, $args) {
  127. $field_type = 'rdfs__type';
  128. $field_name = 'rdfs__type';
  129. // Add the field, unless it already exists.
  130. if (!field_info_field($field_name)) {
  131. $field = field_create_field(array(
  132. 'field_name' => $field_name,
  133. 'type' => $field_type,
  134. 'cardinality' => 1,
  135. 'locked' => FALSE,
  136. 'storage' => array(
  137. 'type' => 'tripal_no_storage'
  138. ),
  139. ));
  140. }
  141. // Add an instance of the field to the bundle.
  142. if (!field_info_instance($bundle->type, $field_name, $bundle->name)) {
  143. $instance = field_create_instance(array(
  144. 'field_name' => $field_name,
  145. 'entity_type' => 'TripalEntity',
  146. 'bundle' => $bundle->name,
  147. 'label' => 'Resource Type',
  148. 'description' => 'The resource type',
  149. 'required' => FALSE,
  150. 'settings' => array(
  151. 'auto_attach' => TRUE,
  152. 'term_vocabulary' => 'rdfs',
  153. 'term_accession' => 'type',
  154. 'term_name' => 'type',
  155. ),
  156. 'widget' => array(
  157. 'type' => 'rdfs__type_widget',
  158. 'settings' => array(
  159. 'display_label' => 1,
  160. ),
  161. ),
  162. 'display' => array(
  163. 'default' => array(
  164. 'label' => 'inline',
  165. 'type' => 'rdfs__type_formatter',
  166. 'settings' => array(),
  167. ),
  168. ),
  169. ));
  170. }
  171. }
  172. /**
  173. * Implements hook_field_formatter_view().
  174. */
  175. function tripal_field_formatter_view($entity_type, $entity, $field,
  176. $instance, $langcode, $items, $display) {
  177. // Don't show any fields that don't have a controlled vocabulary term in
  178. // the database.
  179. $vocabulary = $instance['settings']['term_vocabulary'];
  180. $accession = $instance['settings']['term_accession'];
  181. $term = tripal_get_term_details($vocabulary, $accession);
  182. if (!$term) {
  183. tripal_set_message(t("The controlled vocabulary term, ':term (:term_name)', assigned to the
  184. field, ':field', is not in the database. The field cannot be shown.
  185. Please add the term and the field will appear below. ",
  186. array(':field' => $field['field_name'],
  187. ':term' => $vocabulary . ":" . $accession,
  188. ':term_name' => $instance['settings']['term_name']
  189. )),
  190. TRIPAL_WARNING);
  191. return;
  192. }
  193. // TODO: (Shawna) 1) Check the entity type (bundle) settings to see if empty fieleds
  194. // should be ignored by default. 2) Check the field itself to see if it
  195. // overides the bundle default. The idea being do not show fields when
  196. // people don't want them shown.
  197. $element = array();
  198. $formatter_class = $display['type'];
  199. $is_loaded = tripal_load_include_field_class($formatter_class);
  200. if ($is_loaded) {
  201. $formatter = new $formatter_class($field, $instance);
  202. $formatter->view($element, $entity_type, $entity, $langcode, $items, $display);
  203. }
  204. return $element;
  205. }
  206. /**
  207. * Simple provides a message indicating that the field cannot be deleted.
  208. *
  209. * This function is used in the tripal_menu_alter() function. We alter the
  210. * menu created for managing fields to use this call back which
  211. * prints a message that the field cannot be deleted.
  212. */
  213. function tripal_field_no_delete() {
  214. drupal_set_message('This field cannot be removed.', 'warning');
  215. return '';
  216. }
  217. /**
  218. *
  219. * Implements hook_form_FORM_ID_alter().
  220. *
  221. * The field_ui_field_overview_form_ is used for adding and reordering the
  222. * fields attached to a bundle. It also includes edit and delete links and
  223. * links for editing field types and widgets.
  224. *
  225. * This alter function is used to add a new 'Supported By' column to
  226. * the table to let the user know where fields are storing their data.
  227. */
  228. function tripal_form_field_ui_field_overview_form_alter(&$form, &$form_state, $form_id) {
  229. // Add the 'Storage Location' to the table header.
  230. $form['fields']['#header'][] = 'Term';
  231. $form['fields']['#header'][] = 'Supported By * ';
  232. // Add the storage location as the final column for each field.
  233. $storage_info = module_invoke_all('field_storage_info');
  234. foreach (element_children($form['fields']) as $field_name) {
  235. $field = field_info_field($field_name);
  236. $instance = field_info_instance('TripalEntity', $field_name, $form['#bundle']);
  237. // For rows in the tables that aren't fields, just add an empty value
  238. // for the storage column.
  239. if (!$field) {
  240. $form['fields'][$field_name][] = array(
  241. '#markup' => '',
  242. );
  243. $form['fields'][$field_name][] = array(
  244. '#markup' => '',
  245. );
  246. continue;
  247. }
  248. $term_info = '';
  249. if (array_key_exists('term_accession', $instance['settings']) and $instance['settings']['term_accession']) {
  250. $term = tripal_get_term_details($instance['settings']['term_vocabulary'], $instance['settings']['term_accession']);
  251. $term_info = $term['name'] . ' (' . $instance['settings']['term_vocabulary'] . ':' . $instance['settings']['term_accession'] . ')';
  252. }
  253. $form['fields'][$field_name][] = array(
  254. '#markup' => $term_info,
  255. );
  256. $storage_type = $field['storage']['type'];
  257. $storage_label = array_key_exists('label', $storage_info[$storage_type]) ? $storage_info[$storage_type]['label'] : '';
  258. if ($storage_type == 'field_sql_storage') {
  259. $storage_label = 'Drupal';
  260. }
  261. if (array_key_exists('logo_url', $storage_info[$storage_type])) {
  262. $logo_url = $storage_info[$storage_type]['logo_url'];
  263. $form['fields'][$field_name][] = array(
  264. '#markup' => '<img class="form-field-ui-field-overview-storage-logo" src="' . $logo_url . '">',
  265. );
  266. }
  267. else {
  268. $form['fields'][$field_name][] = array(
  269. '#markup' => $storage_label,
  270. );
  271. }
  272. }
  273. $form['note'] = array(
  274. '#markup' => '* Fields attached to this content type can use various
  275. storage backends. Please be sure when you add new fields that the
  276. storage backend is appropriate. For example, if you use Chado, and you
  277. want all biological content to be stored in Chado, be sure that the
  278. respective fields are "supported by" Chado.',
  279. );
  280. $form['#submit'] = array_merge(array('tripal_form_field_ui_field_overview_form_submit'), $form['#submit']);
  281. }
  282. /**
  283. * A submit function for the field_ui_field_overview_form.
  284. *
  285. */
  286. function tripal_form_field_ui_field_overview_form_submit($form, &$form_state) {
  287. $form_values = $form_state['values']['fields'];
  288. $admin_path = _field_ui_bundle_admin_path('TripalEntity', $form['#bundle']);
  289. $destinations = array();
  290. // If the form Field UI form is adding a new field to the bundle we want
  291. // to preempt Drupal from creating the field in it's field_sql_storage
  292. // backend. We want to create it.
  293. if (!empty($form_values['_add_new_field']['field_name'])) {
  294. try {
  295. // Is the field type a TripalField? If so then we want
  296. // to pass of creation of the field to the module that manages that field.
  297. $type = $form_values['_add_new_field']['type'];
  298. if (tripal_load_include_field_class($type)) {
  299. $module = $type::$module;
  300. $function = $module . '_bundle_create_user_field';
  301. $bundle = tripal_load_bundle_entity(array('name' => $form['#bundle']));
  302. $field_name = $form_values['_add_new_field']['field_name'];
  303. // If the module implements the hook then we'll have it create the
  304. // field and instance.
  305. if (function_exists($function)) {
  306. $function($form_values['_add_new_field'], $bundle);
  307. }
  308. $destinations[] = $admin_path . '/fields/' . $field_name . '/field-shcef';
  309. $destinations[] = $admin_path . '/fields/' . $field_name;
  310. // Store new field information for any additional submit handlers.
  311. $form_state['fields_added']['_add_new_field'] = $field_name;
  312. // Unset the the _add_new_field entry so Drupal doesn't try to
  313. // Create the field.
  314. unset($form_state['values']['fields']['_add_new_field']);
  315. drupal_set_message('Please set the controlled vocabulary that best describes the data of this field. See the "Controlled Vocabulary Term" section below.', 'warning');
  316. }
  317. }
  318. catch (Exception $e) {
  319. drupal_set_message(t('There was a problem creating field %label: !message', array('%label' => $instance['label'], '!message' => $e->getMessage())), 'error');
  320. }
  321. if ($destinations) {
  322. $destination = drupal_get_destination();
  323. $destinations[] = $destination['destination'];
  324. unset($_GET['destination']);
  325. $form_state['redirect'] = field_ui_get_destinations($destinations);
  326. }
  327. else {
  328. drupal_set_message(t('Your settings have been saved.'));
  329. }
  330. }
  331. }
  332. /**
  333. * Implements hook_module_implements_alter()
  334. *
  335. * We want our edits to the field_ui_field_overview_form form to occur after
  336. * all modules have implemented their changes.
  337. */
  338. function tripal_module_implements_alter(&$implementations, $hook) {
  339. if ($hook == 'form_alter') {
  340. $group = $implementations['tripal'];
  341. unset($implementations['tripal']);
  342. $implementations['tripal'] = $group;
  343. }
  344. }
  345. /**
  346. * Implements hook_field_settings_form()
  347. */
  348. function tripal_field_settings_form($field, $instance, $has_data) {
  349. $field_class = $field['type'];
  350. if (tripal_load_include_field_class($field_class)) {
  351. $field = new $field_class($field, $instance);
  352. return $field->settingsForm($has_data);
  353. }
  354. }
  355. /**
  356. * Implements hook_instance_settings_form()
  357. */
  358. function tripal_field_instance_settings_form($field, $instance) {
  359. $field_class = $field['type'];
  360. if (tripal_load_include_field_class($field_class)) {
  361. $field = new $field_class($field, $instance);
  362. return $field->instanceSettingsForm();
  363. }
  364. }
  365. /**
  366. * Validates the TripalField instance settings form.
  367. *
  368. * This function is called because the TripalField::instanceSettingsForm()
  369. * adds it to the form element. By default, Drupal does not provide a
  370. * validate hook for the instance settings form.
  371. */
  372. function tripal_field_instance_settings_form_validate($element, &$form_state, $form) {
  373. $field = $element['#field'];
  374. $instance = $element['#instance'];
  375. $field_class = $field['type'];
  376. if (tripal_load_include_field_class($field_class)) {
  377. $field = new $field_class($field, $instance);
  378. return $field->instanceSettingsForm();
  379. }
  380. }
  381. /**
  382. * Allows for altering of a field's instance setting form.
  383. *
  384. * This appears to be a Drupal hook but is actually a custom function created
  385. * by this module. It is called by the tripal_form_alter() function of this
  386. * module.
  387. *
  388. * Here we put additional form elements for any field, regardless if it is
  389. * a tripalField or not.
  390. *
  391. * @param $form
  392. * The form array. Alterations to the form can be made within this array.
  393. * @param $form_state
  394. * The form state array.
  395. */
  396. function tripal_field_instance_settings_form_alter(&$form, $form_state) {
  397. global $language;
  398. // It's not possible to add AJAX to a form element in the hook_form_alter
  399. // function. To make it work we have to add a process function. Inisde
  400. // of that process function is where the form additions get added that use
  401. // Ajax.
  402. $form['field_term'][$language->language][0]['#process'] = array('tripal_field_instance_settings_form_alter_process');
  403. $form['#submit'][] = 'tripal_field_instance_settings_form_submit';
  404. }
  405. /**
  406. * Implements a process function for the instance settings form.
  407. *
  408. * See the comment in the tripal_field_instance_settings_form_alter() for
  409. * more details.
  410. */
  411. function tripal_field_instance_settings_form_alter_process($element, &$form_state, $form) {
  412. $field = $form['#field'];
  413. $instance = $form['#instance'];
  414. // Get the term for this instance.
  415. $vocabulary = '';
  416. $accession = '';
  417. $term_name = '';
  418. $term = NULL;
  419. if (array_key_exists('settings', $instance) and array_key_exists('term_vocabulary', $instance['settings'])) {
  420. $vocabulary = $instance['settings']['term_vocabulary'];
  421. $accession = $instance['settings']['term_accession'];
  422. $term_name = $instance['settings']['term_name'];
  423. $term = tripal_get_term_details($vocabulary, $accession);
  424. }
  425. // Construct a table for the vocabulary information.
  426. $headers = array();
  427. $rows = array();
  428. $rows[] = array(
  429. array(
  430. 'data' => 'Vocabulary',
  431. 'header' => TRUE,
  432. 'width' => '20%',
  433. ),
  434. $term['vocabulary']['name'] . ' (' . $vocabulary. ') ' . $term['vocabulary']['description']
  435. );
  436. $rows[] = array(
  437. array(
  438. 'data' => 'Term',
  439. 'header' => TRUE,
  440. 'width' => '20%',
  441. ),
  442. $vocabulary . ':' . $accession
  443. );
  444. $rows[] = array(
  445. array(
  446. 'data' => 'Name',
  447. 'header' => TRUE,
  448. 'width' => '20%',
  449. ),
  450. $term['name']
  451. );
  452. $rows[] = array(
  453. array(
  454. 'data' => 'Definition',
  455. 'header' => TRUE,
  456. 'width' => '20%',
  457. ),
  458. $term['definition']
  459. );
  460. $table = array(
  461. 'header' => $headers,
  462. 'rows' => $rows,
  463. 'attributes' => array(),
  464. 'sticky' => FALSE,
  465. 'caption' => '',
  466. 'colgroups' => array(),
  467. 'empty' => '',
  468. );
  469. $description = t('All fields attached to a Tripal-based content type must
  470. be associated with a controlled vocabulary term. Please use caution
  471. when changing the term for this field as other sites may expect this term
  472. when querying web services.');
  473. if (array_key_exists('term_fixed', $instance['settings']) and $instance['settings']['term_fixed']) {
  474. $description = t('All fields attached to a Tripal-based content type must
  475. be associated with a controlled vocabulary term. This field mapping is
  476. required and cannot be changed');
  477. }
  478. $element['field_term'] = array(
  479. '#type' => 'fieldset',
  480. '#title' => 'Controlled Vocabulary Term',
  481. '#description' => $description,
  482. '#prefix' => '<div id = "tripal-field-term-fieldset">',
  483. '#suffix' => '</div>',
  484. );
  485. $element['field_term']['term_vocabulary'] = array(
  486. '#type' => 'value',
  487. '#value' => $vocabulary,
  488. );
  489. $element['field_term']['term_name'] = array(
  490. '#type' => 'value',
  491. '#value' => $term_name,
  492. );
  493. $element['field_term']['term_accession'] = array(
  494. '#type' => 'value',
  495. '#value' => $accession,
  496. );
  497. $element['field_term']['details'] = array(
  498. '#type' => 'item',
  499. '#title' => 'Current Term',
  500. '#markup' => theme_table($table),
  501. );
  502. // If this field mapping is fixed then don't let the user change it.
  503. if (!array_key_exists('term_fixed', $instance['settings']) or $instance['settings']['term_fixed'] != TRUE) {
  504. $element['field_term']['new_name'] = array(
  505. '#type' => 'textfield',
  506. '#title' => 'Change the term',
  507. // TODO: This autocomplete path should not use Chado.
  508. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/",
  509. );
  510. $element['field_term']['select_button'] = array(
  511. '#type' => 'button',
  512. '#value' => t('Lookup Term'),
  513. '#name' => 'select_cvterm',
  514. '#ajax' => array(
  515. 'callback' => "tripal_fields_select_term_form_ajax_callback",
  516. 'wrapper' => "tripal-field-term-fieldset",
  517. 'effect' => 'fade',
  518. 'method' => 'replace'
  519. ),
  520. );
  521. }
  522. // If a new term name has been specified by the user then give some extra
  523. // fields to clarify the term.
  524. $term_name = '';
  525. if (array_key_exists('values', $form_state) and array_key_exists('new_name', $form_state['values'])) {
  526. $term_name = array_key_exists('values', $form_state) ? $form_state['values']['new_name'] : '';
  527. }
  528. if (array_key_exists('input', $form_state) and array_key_exists('new_name', $form_state['input'])) {
  529. $term_name = array_key_exists('input', $form_state) ? $form_state['input']['new_name'] : '';
  530. }
  531. if ($term_name) {
  532. $element['field_term']['instructions'] = array(
  533. '#type' => 'item',
  534. '#title' => 'Matching terms',
  535. '#markup' => t('Please select the term the best matches the
  536. content type you want to associate with this field. If the same term exists in
  537. multiple vocabularies you will see more than one option below.')
  538. );
  539. $match = array(
  540. 'name' => $term_name,
  541. );
  542. $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
  543. $terms = chado_expand_var($terms, 'field', 'cvterm.definition');
  544. $num_terms = 0;
  545. foreach ($terms as $term) {
  546. // Save the user a click by setting the default value as 1 if there's
  547. // only one matching term.
  548. $default = FALSE;
  549. $attrs = array();
  550. if ($num_terms == 0 and count($terms) == 1) {
  551. $default = TRUE;
  552. $attrs = array('checked' => 'checked');
  553. }
  554. $element['field_term']['term-' . $term->cvterm_id] = array(
  555. '#type' => 'checkbox',
  556. '#title' => $term->name,
  557. '#default_value' => $default,
  558. '#attributes' => $attrs,
  559. '#description' => '<b>Vocabulary:</b> ' . $term->cv_id->name . ' (' . $term->dbxref_id->db_id->name . ') ' . $term->cv_id->definition .
  560. '<br><b>Term: </b> ' . $term->dbxref_id->db_id->name . ':' . $term->dbxref_id->accession . '. ' .
  561. '<br><b>Definition:</b> ' . $term->definition,
  562. );
  563. $num_terms++;
  564. }
  565. if ($num_terms == 0) {
  566. $element['field_term']['none'] = array(
  567. '#type' => 'item',
  568. '#markup' => '<i>' . t('There is no term that matches the entered text.') . '</i>'
  569. );
  570. }
  571. }
  572. $element['#element_validate'][] = 'tripal_field_instance_settings_form_alter_validate';
  573. return $element;
  574. }
  575. /**
  576. * Implements an AJAX callback for the tripal_field_vocab_select_term_form.
  577. */
  578. function tripal_fields_select_term_form_ajax_callback($form, $form_state) {
  579. return $form['field_term'];
  580. }
  581. /**
  582. * Validate our custom instance settings form fields.
  583. */
  584. function tripal_field_instance_settings_form_alter_validate($form, &$form_state) {
  585. // If the user clicked the submit button then we want set the
  586. // instance settings values accordingly.
  587. if (array_key_exists('clicked_button', $form_state) and $form_state['clicked_button']['#executes_submit_callback'] == TRUE) {
  588. $has_default = FALSE;
  589. if ($form_state['values']['term_vocabulary']) {
  590. $has_default = TRUE;
  591. }
  592. $started_new_term = FALSE;
  593. if (array_key_exists('new_name', $form_state['values']) and $form_state['values']['new_name']) {
  594. $started_new_term = TRUE;
  595. }
  596. $num_selected = 0;
  597. $selected_term = FALSE;
  598. foreach ($form_state['input'] as $key => $value) {
  599. $matches = array();
  600. if (preg_match("/^term-(\d+)$/", $key, $matches) and
  601. $form_state['input']['term-' . $matches[1]]) {
  602. $cvterm_id = $matches[1];
  603. // TODO: this should not call a Chado function.
  604. $term = chado_generate_var('cvterm', array('cvterm_id' => $cvterm_id));
  605. $form_state['values']['instance']['settings']['term_vocabulary'] = $term->dbxref_id->db_id->name;
  606. $form_state['values']['instance']['settings']['term_accession'] = $term->dbxref_id->accession;
  607. $form_state['values']['instance']['settings']['term_name'] = $term->name;
  608. $selected_term = TRUE;
  609. $num_selected++;
  610. $has_default = TRUE;
  611. }
  612. }
  613. if ($num_selected > 1) {
  614. form_set_error('term-', 'Please select only one term
  615. from the "Controlled Vocabulary Term" section below.');
  616. }
  617. if ($started_new_term and !$selected_term) {
  618. form_set_error('term-', 'Please select a controlled vocabulary term for
  619. from the "Controlled Vocabulary Term" section below.');
  620. }
  621. if (!$has_default) {
  622. form_set_error('new_name', 'Fields attached to this content type must ' .
  623. 'be associated with a controlled vocabulary term. Please ' .
  624. 'provide one below.');
  625. }
  626. }
  627. }
  628. /**
  629. * Custom submit function for instance settings form.
  630. */
  631. function tripal_field_instance_settings_form_submit($form, &$form_state) {
  632. }
  633. /**
  634. *
  635. */
  636. function tripal_field_widget_form_validate($element, &$form_state, $form) {
  637. $field = $element['#field'];
  638. $instance = $element['#instance'];
  639. $widget_class = $element['#field_name'] . '_widget';
  640. $langcode = $element['#language'];
  641. $delta = $element['#delta'];
  642. tripal_load_include_field_class($widget_class);
  643. if (class_exists($widget_class)) {
  644. $widget = new $widget_class($field, $instance);
  645. $widget->validate($element, $form, $form_state, $langcode, $delta);
  646. }
  647. }
  648. /**
  649. * Implements hook_field_settings_form_validate().
  650. *
  651. * This function is called because the TripalField::settingsForm()
  652. * adds it to the form element. By default, Drupal does not provide a
  653. * validate hook for the settings form.
  654. */
  655. function tripal_field_settings_form_validate($element, &$form_state, $form) {
  656. $field = $element['#field'];
  657. $instance = $element['#instance'];
  658. $field_class = $field['type'];
  659. if (tripal_load_include_field_class($field_class)) {
  660. $field = new $field_class($field, $instance);
  661. $field->settingsFormValidate($form, $form_state);
  662. }
  663. }
  664. /**
  665. * Implements hook_field_formatter_settings_summary().
  666. */
  667. function tripal_field_formatter_settings_summary($field, $instance, $view_mode) {
  668. $formatter_class = $field['type'] . '_formatter';
  669. if (tripal_load_include_field_class($formatter_class)) {
  670. $formatter = new $formatter_class($field, $instance);
  671. return $formatter->settingsSummary($view_mode);
  672. }
  673. }
  674. /**
  675. * Implements hook_field_formatter_settings_form().
  676. */
  677. function tripal_field_formatter_settings_form($field, $instance,
  678. $view_mode, $form, &$form_state) {
  679. if (tripal_load_include_field_class($formatter_class)) {
  680. $formatter = new $formatter_class($field, $instance);
  681. $elements = $formatter->settingsForm($view_mode, $form, $form_state);
  682. }
  683. return $elements;
  684. }
  685. /**
  686. * Implements hook_field_widget_form().
  687. */
  688. function tripal_field_widget_form(&$form, &$form_state, $field,
  689. $instance, $langcode, $items, $delta, $element) {
  690. $widget_class = $instance['widget']['type'];
  691. tripal_load_include_field_class($widget_class);
  692. if (class_exists($widget_class)) {
  693. $widget = new $widget_class($field, $instance);
  694. $widget->form($element, $form, $form_state, $langcode, $items, $delta, $element);
  695. }
  696. return $element;
  697. }
  698. /**
  699. * Implements hook_field_widget_form_alter().
  700. */
  701. function tripal_field_widget_form_alter(&$element, &$form_state, $context) {
  702. if (array_key_exists('#field_name', $element)) {
  703. $field_name = $element['#field_name'];
  704. $matches = array();
  705. if (preg_match('/(.+?)__(.+?)$/', $field_name, $matches)) {
  706. $tablename = $matches[1];
  707. $colname = $matches[2];
  708. $schema = chado_get_schema($tablename);
  709. if (!$schema) {
  710. return;
  711. }
  712. // The timelastmodified field exists in many Chado tables. We want
  713. // the form element to update to the most recent time rather than the time
  714. // in the database.
  715. if ($colname == 'timelastmodified' and $schema['fields'][$colname]['type'] == 'datetime') {
  716. // We want the default value for the field to be the current time.
  717. $element['#default_value']['value'] = format_date(time(), 'custom', "Y-m-d H:i:s", 'UTC');
  718. $element['#date_items']['value'] = $element['#default_value']['value'];
  719. }
  720. // We want the date combo fieldset to be collaspible so we will
  721. // add our own theme_wrapper to replace the one added by the date
  722. // module.
  723. if (array_key_exists($colname, $schema['fields']) and $schema['fields'][$colname]['type'] == 'datetime') {
  724. $element['#theme_wrappers'] = array('tripal_chado_date_combo');
  725. }
  726. }
  727. }
  728. }
  729. /**
  730. * Implements hook_field_validate()
  731. */
  732. function tripal_field_validate($entity_type, $entity, $field, $instance,
  733. $langcode, $items, &$errors) {
  734. $field_type = $field['type'];
  735. if (tripal_load_include_field_class($field_type)) {
  736. $tfield = new $field_type($field, $instance);
  737. $tfield->validate($entity_type, $entity, $langcode, $items, $errors);
  738. }
  739. }
  740. /**
  741. * Implements hook_form_FORM_ID_alter().
  742. *
  743. * The field_ui_display_overview_form is used for formatting the display
  744. * or layout of fields attached to an entity and shown on the entity view page.
  745. *
  746. * This function removes the cvterm class and property adder field as those are
  747. * really not meant for users to show or manage.
  748. */
  749. function tripal_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
  750. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  751. // on the add/edit form of an entity for adding new property fields.
  752. $fields_names = element_children($form['fields']);
  753. foreach ($fields_names as $field_name) {
  754. $field_info = field_info_field($field_name);
  755. if ($field_info['type'] == 'kvproperty_adder') {
  756. unset($form['fields'][$field_name]);
  757. }
  758. if ($field_info['type'] == 'cvterm_class_adder') {
  759. unset($form['fields'][$field_name]);
  760. }
  761. }
  762. }
  763. /**
  764. * Implements hook_field_is_empty().
  765. */
  766. function tripal_field_is_empty($item, $field) {
  767. // If the $item argument is empty then return TRUE.
  768. if (!$item) {
  769. return TRUE;
  770. }
  771. // If there is no value field then the field is empty.
  772. if (!array_key_exists('value', $item)) {
  773. return TRUE;
  774. }
  775. // If there is a value field but there's nothing in it, the the field is
  776. // empty.
  777. if (array_key_exists('value', $item) and empty($item['value'])){
  778. return TRUE;
  779. }
  780. // Otherwise, the field is not empty.
  781. return FALSE;
  782. }
  783. /**
  784. * Theme function for all TripalFieldWidget objects.
  785. *
  786. * @param $variables
  787. */
  788. function theme_tripal_field_default($variables) {
  789. $element = $variables['element'];
  790. $field = $element['#field'];
  791. $instance = $element['#instance'];
  792. $widget_class = $element['#field_name'] . '_widget';
  793. $langcode = $element['#language'];
  794. $delta = $element['#delta'];
  795. tripal_load_include_field_class($widget_class);
  796. if (class_exists($widget_class)) {
  797. $widget = new $widget_class($field, $instance);
  798. return $widget->theme($element);
  799. }
  800. }