tripal.fields.inc 30 KB

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