tripal.fields.inc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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. foreach ($info as $field_name => $details) {
  35. // Make sure all fields have a term setting so we can map
  36. // all fields to a vocabulary term for the semantic web.
  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. $element = array();
  194. $formatter_class = $display['type'];
  195. $is_loaded = tripal_load_include_field_class($formatter_class);
  196. if ($is_loaded) {
  197. $formatter = new $formatter_class($field, $instance);
  198. $formatter->view($element, $entity_type, $entity, $langcode, $items, $display);
  199. }
  200. return $element;
  201. }
  202. /**
  203. * Simple provides a message indicating that the field cannot be deleted.
  204. *
  205. * This function is used in the tripal_menu_alter() function. We alter the
  206. * menu created for managing fields to use this call back which
  207. * prints a message that the field cannot be deleted.
  208. */
  209. function tripal_field_no_delete() {
  210. drupal_set_message('This field cannot be removed.', 'warning');
  211. return '';
  212. }
  213. /**
  214. *
  215. * Implements hook_form_FORM_ID_alter().
  216. *
  217. * The field_ui_field_overview_form_ is used for adding and reordering the
  218. * fields attached to a bundle. It also includes edit and delete links and
  219. * links for editing field types and widgets.
  220. *
  221. * This alter function is used to add a new 'Supported By' column to
  222. * the table to let the user know where fields are storing their data.
  223. */
  224. function tripal_form_field_ui_field_overview_form_alter(&$form, &$form_state, $form_id) {
  225. // If this isn't a TripalEntity content type then skip this form.
  226. if ($form['#entity_type'] != 'TripalEntity') {
  227. return;
  228. }
  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. // Otherwise, we should make a good attempt on our own.
  309. // Especially in the case of fields added via the Library directory.
  310. else {
  311. $new_field = $form_values['_add_new_field'];
  312. // Create the field.
  313. $field = array(
  314. 'field_name' => $new_field['field_name'],
  315. 'type' => $new_field['type'],
  316. 'cardinality' => FIELD_CARDINALITY_UNLIMITED, // @hard-coded
  317. 'locked' => FALSE,
  318. 'storage' => array(
  319. 'type' => $type::$default_settings['storage']
  320. ),
  321. );
  322. field_create_field($field);
  323. // Then create the instance.
  324. $instance = array(
  325. 'field_name' => $new_field['field_name'],
  326. 'entity_type' => $bundle->type,
  327. 'bundle' => $bundle->name,
  328. 'label' => $new_field['label'],
  329. 'description' => $type::$default_description,
  330. 'required' => FALSE,
  331. 'settings' => array(
  332. 'auto_attach' => $type::$default_instance_settings['auto_attach'],
  333. ),
  334. 'widget' => array(
  335. 'type' => $new_field['widget_type'],
  336. 'settings' => array(),
  337. ),
  338. 'display' => array(
  339. 'default' => array(
  340. 'label' => 'hidden',
  341. 'type' => $type::$default_formatter,
  342. 'settings' => array(),
  343. ),
  344. ),
  345. );
  346. field_create_instance($instance);
  347. }
  348. $destinations[] = $admin_path . '/fields/' . $field_name . '/field-shcef';
  349. $destinations[] = $admin_path . '/fields/' . $field_name;
  350. // Store new field information for any additional submit handlers.
  351. $form_state['fields_added']['_add_new_field'] = $field_name;
  352. // Unset the the _add_new_field entry so Drupal doesn't try to
  353. // Create the field.
  354. unset($form_state['values']['fields']['_add_new_field']);
  355. drupal_set_message('Please set the controlled vocabulary that best describes the data of this field. See the "Controlled Vocabulary Term" section below.', 'warning');
  356. }
  357. }
  358. catch (Exception $e) {
  359. drupal_set_message(t('There was a problem creating field %label: !message', array('%label' => $instance['label'], '!message' => $e->getMessage())), 'error');
  360. }
  361. if ($destinations) {
  362. $destination = drupal_get_destination();
  363. $destinations[] = $destination['destination'];
  364. unset($_GET['destination']);
  365. $form_state['redirect'] = field_ui_get_destinations($destinations);
  366. }
  367. else {
  368. drupal_set_message(t('Your settings have been saved.'));
  369. }
  370. }
  371. }
  372. /**
  373. * Implements hook_module_implements_alter()
  374. *
  375. * We want our edits to the field_ui_field_overview_form form to occur after
  376. * all modules have implemented their changes.
  377. */
  378. function tripal_module_implements_alter(&$implementations, $hook) {
  379. if ($hook == 'form_alter') {
  380. $group = $implementations['tripal'];
  381. unset($implementations['tripal']);
  382. $implementations['tripal'] = $group;
  383. }
  384. }
  385. /**
  386. * Implements hook_field_settings_form()
  387. */
  388. function tripal_field_settings_form($field, $instance, $has_data) {
  389. $field_class = $field['type'];
  390. if (tripal_load_include_field_class($field_class)) {
  391. $field = new $field_class($field, $instance);
  392. return $field->settingsForm($has_data);
  393. }
  394. }
  395. /**
  396. * Implements hook_instance_settings_form()
  397. */
  398. function tripal_field_instance_settings_form($field, $instance) {
  399. $field_class = $field['type'];
  400. if (tripal_load_include_field_class($field_class)) {
  401. $field = new $field_class($field, $instance);
  402. return $field->instanceSettingsForm();
  403. }
  404. }
  405. /**
  406. * Validates the TripalField instance settings form.
  407. *
  408. * This function is called because the TripalField::instanceSettingsForm()
  409. * adds it to the form element. By default, Drupal does not provide a
  410. * validate hook for the instance settings form.
  411. */
  412. function tripal_field_instance_settings_form_validate($element, &$form_state, $form) {
  413. $field = $element['#field'];
  414. $instance = $element['#instance'];
  415. $field_class = $field['type'];
  416. if (tripal_load_include_field_class($field_class)) {
  417. $field = new $field_class($field, $instance);
  418. return $field->instanceSettingsFormValidate($form, $form_state);
  419. }
  420. }
  421. /**
  422. * Allows for altering of a field's instance setting form.
  423. *
  424. * This appears to be a Drupal hook but is actually a custom function created
  425. * by this module. It is called by the tripal_form_alter() function of this
  426. * module.
  427. *
  428. * Here we put additional form elements for any field, regardless if it is
  429. * a tripalField or not.
  430. *
  431. * @param $form
  432. * The form array. Alterations to the form can be made within this array.
  433. * @param $form_state
  434. * The form state array.
  435. */
  436. function tripal_field_instance_settings_form_alter(&$form, $form_state) {
  437. global $language;
  438. // It's not possible to add AJAX to a form element in the hook_form_alter
  439. // function. To make it work we have to add a process function. Inisde
  440. // of that process function is where the form additions get added that use
  441. // Ajax.
  442. $form['field_term'][$language->language][0]['#process'] = array('tripal_field_instance_settings_form_alter_process');
  443. $form['#submit'][] = 'tripal_field_instance_settings_form_submit';
  444. }
  445. /**
  446. * Implements a process function for the instance settings form.
  447. *
  448. * See the comment in the tripal_field_instance_settings_form_alter() for
  449. * more details.
  450. */
  451. function tripal_field_instance_settings_form_alter_process($element, &$form_state, $form) {
  452. $field = $form['#field'];
  453. $instance = $form['#instance'];
  454. // Get the term for this instance.
  455. $vocabulary = '';
  456. $accession = '';
  457. $term_name = '';
  458. $term = NULL;
  459. if (array_key_exists('settings', $instance) and
  460. array_key_exists('term_vocabulary', $instance['settings'])) {
  461. $vocabulary = $instance['settings']['term_vocabulary'];
  462. $accession = $instance['settings']['term_accession'];
  463. $term_name = $instance['settings']['term_name'];
  464. $term = tripal_get_term_details($vocabulary, $accession);
  465. }
  466. // Construct a table for the vocabulary information.
  467. $headers = array();
  468. $rows = array();
  469. $rows[] = array(
  470. array(
  471. 'data' => 'Vocabulary',
  472. 'header' => TRUE,
  473. 'width' => '20%',
  474. ),
  475. $term['vocabulary']['name'] . ' (' . $vocabulary. ') ' . $term['vocabulary']['description']
  476. );
  477. $rows[] = array(
  478. array(
  479. 'data' => 'Term',
  480. 'header' => TRUE,
  481. 'width' => '20%',
  482. ),
  483. $vocabulary . ':' . $accession
  484. );
  485. $rows[] = array(
  486. array(
  487. 'data' => 'Name',
  488. 'header' => TRUE,
  489. 'width' => '20%',
  490. ),
  491. $term['name']
  492. );
  493. $rows[] = array(
  494. array(
  495. 'data' => 'Definition',
  496. 'header' => TRUE,
  497. 'width' => '20%',
  498. ),
  499. $term['definition']
  500. );
  501. $table = array(
  502. 'header' => $headers,
  503. 'rows' => $rows,
  504. 'attributes' => array(),
  505. 'sticky' => FALSE,
  506. 'caption' => '',
  507. 'colgroups' => array(),
  508. 'empty' => '',
  509. );
  510. $description = t('All fields attached to a Tripal-based content type must
  511. be associated with a controlled vocabulary term. Please use caution
  512. when changing the term for this field as other sites may expect this term
  513. when querying web services.');
  514. if (array_key_exists('term_fixed', $instance['settings']) and $instance['settings']['term_fixed']) {
  515. $description = t('All fields attached to a Tripal-based content type must
  516. be associated with a controlled vocabulary term. This field mapping is
  517. required and cannot be changed');
  518. }
  519. $element['term_vocabulary'] = array(
  520. '#type' => 'value',
  521. '#value' => $vocabulary,
  522. );
  523. $element['term_name'] = array(
  524. '#type' => 'value',
  525. '#value' => $term_name,
  526. );
  527. $element['term_accession'] = array(
  528. '#type' => 'value',
  529. '#value' => $accession,
  530. );
  531. $element['field_term'] = array(
  532. '#type' => 'fieldset',
  533. '#title' => 'Controlled Vocabulary Term',
  534. '#description' => $description,
  535. '#prefix' => '<div id = "tripal-field-term-fieldset">',
  536. '#suffix' => '</div>',
  537. );
  538. $element['field_term']['details'] = array(
  539. '#type' => 'item',
  540. '#title' => 'Current Term',
  541. '#markup' => theme_table($table),
  542. );
  543. // If this field mapping is fixed then don't let the user change it.
  544. if (!array_key_exists('term_fixed', $instance['settings']) or $instance['settings']['term_fixed'] != TRUE) {
  545. $element['field_term']['new_name'] = array(
  546. '#type' => 'textfield',
  547. '#title' => 'Change the term',
  548. // TODO: This autocomplete path should not use Chado.
  549. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/",
  550. );
  551. $element['field_term']['select_button'] = array(
  552. '#type' => 'button',
  553. '#value' => t('Lookup Term'),
  554. '#name' => 'select_cvterm',
  555. '#ajax' => array(
  556. 'callback' => "tripal_fields_select_term_form_ajax_callback",
  557. 'wrapper' => "tripal-field-term-fieldset",
  558. 'effect' => 'fade',
  559. 'method' => 'replace'
  560. ),
  561. );
  562. }
  563. // If a new term name has been specified by the user then give some extra
  564. // fields to clarify the term.
  565. $term_name = '';
  566. if (array_key_exists('values', $form_state) and array_key_exists('new_name', $form_state['values'])) {
  567. $term_name = array_key_exists('values', $form_state) ? $form_state['values']['new_name'] : '';
  568. }
  569. if (array_key_exists('input', $form_state) and array_key_exists('new_name', $form_state['input'])) {
  570. $term_name = array_key_exists('input', $form_state) ? $form_state['input']['new_name'] : '';
  571. }
  572. if ($term_name) {
  573. $element['field_term']['instructions'] = array(
  574. '#type' => 'item',
  575. '#title' => 'Matching terms',
  576. '#markup' => t('Please select the term the best matches the
  577. content type you want to associate with this field. If the same term exists in
  578. multiple vocabularies you will see more than one option below.')
  579. );
  580. $match = array(
  581. 'name' => $term_name,
  582. );
  583. $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
  584. $terms = chado_expand_var($terms, 'field', 'cvterm.definition');
  585. $num_terms = 0;
  586. foreach ($terms as $term) {
  587. // Save the user a click, by setting the default value as 1 if there's
  588. // only one matching term.
  589. $default = FALSE;
  590. $attrs = array();
  591. if ($num_terms == 0 and count($terms) == 1) {
  592. $default = TRUE;
  593. $attrs = array('checked' => 'checked');
  594. }
  595. $element['field_term']['term-' . $term->cvterm_id] = array(
  596. '#type' => 'checkbox',
  597. '#title' => $term->name,
  598. '#default_value' => $default,
  599. '#attributes' => $attrs,
  600. '#description' => '<b>Vocabulary:</b> ' . $term->cv_id->name . ' (' . $term->dbxref_id->db_id->name . ') ' . $term->cv_id->definition .
  601. '<br><b>Term: </b> ' . $term->dbxref_id->db_id->name . ':' . $term->dbxref_id->accession . '. ' .
  602. '<br><b>Definition:</b> ' . $term->definition,
  603. );
  604. $num_terms++;
  605. }
  606. if ($num_terms == 0) {
  607. $element['field_term']['none'] = array(
  608. '#type' => 'item',
  609. '#markup' => '<i>' . t('There is no term that matches the entered text.') . '</i>'
  610. );
  611. }
  612. }
  613. $element['#element_validate'][] = 'tripal_field_instance_settings_form_alter_validate';
  614. return $element;
  615. }
  616. /**
  617. * Implements an AJAX callback for the tripal_field_vocab_select_term_form.
  618. */
  619. function tripal_fields_select_term_form_ajax_callback($form, $form_state) {
  620. return $form['field_term'];
  621. }
  622. /**
  623. * Validate our custom instance settings form fields.
  624. */
  625. function tripal_field_instance_settings_form_alter_validate($form, &$form_state) {
  626. // If the user clicked the submit button then we want set the
  627. // instance settings values accordingly.
  628. if (array_key_exists('clicked_button', $form_state) and $form_state['clicked_button']['#executes_submit_callback'] == TRUE) {
  629. $has_default = FALSE;
  630. if ($form_state['values']['term_vocabulary']) {
  631. $form_state['values']['instance']['settings']['term_vocabulary'] = $form_state['values']['term_vocabulary'];
  632. $form_state['values']['instance']['settings']['term_accession'] = $form_state['values']['term_accession'];
  633. $form_state['values']['instance']['settings']['term_name'] = $form_state['values']['term_name'];
  634. $has_default = TRUE;
  635. }
  636. $started_new_term = FALSE;
  637. if (array_key_exists('new_name', $form_state['values']) and $form_state['values']['new_name']) {
  638. $started_new_term = TRUE;
  639. }
  640. $num_selected = 0;
  641. $selected_term = FALSE;
  642. foreach ($form_state['input'] as $key => $value) {
  643. $matches = array();
  644. if (preg_match("/^term-(\d+)$/", $key, $matches) and
  645. $form_state['input']['term-' . $matches[1]]) {
  646. $cvterm_id = $matches[1];
  647. // TODO: this should not call a Chado function, but the autocomplete
  648. // currently uses chado cvterm IDs.
  649. $term = chado_generate_var('cvterm', array('cvterm_id' => $cvterm_id));
  650. if ($term) {
  651. $form_state['values']['instance']['settings']['term_vocabulary'] = $term->dbxref_id->db_id->name;
  652. $form_state['values']['instance']['settings']['term_accession'] = $term->dbxref_id->accession;
  653. $form_state['values']['instance']['settings']['term_name'] = $term->name;
  654. $selected_term = TRUE;
  655. $num_selected++;
  656. $has_default = TRUE;
  657. }
  658. }
  659. }
  660. if ($num_selected > 1) {
  661. form_set_error('term-', 'Please select only one term
  662. from the "Controlled Vocabulary Term" section below.');
  663. }
  664. if ($started_new_term and !$selected_term) {
  665. form_set_error('term-', 'Please select a controlled vocabulary term for
  666. from the "Controlled Vocabulary Term" section below.');
  667. }
  668. if (!$has_default) {
  669. form_set_error('new_name', 'Fields attached to this content type must ' .
  670. 'be associated with a controlled vocabulary term. Please ' .
  671. 'provide one below.');
  672. }
  673. }
  674. }
  675. /**
  676. * Custom submit function for instance settings form.
  677. */
  678. function tripal_field_instance_settings_form_submit($form, &$form_state) {
  679. }
  680. /**
  681. *
  682. */
  683. function tripal_field_widget_form_validate($element, &$form_state, $form) {
  684. $field = $element['#field'];
  685. $instance = $element['#instance'];
  686. $langcode = $element['#language'];
  687. $delta = $element['#delta'];
  688. $widget_class = $instance['widget']['type'];
  689. tripal_load_include_field_class($widget_class);
  690. if (class_exists($widget_class)) {
  691. $widget = new $widget_class($field, $instance);
  692. $widget->validate($element, $form, $form_state, $langcode, $delta);
  693. }
  694. }
  695. /**
  696. * Implements hook_field_settings_form_validate().
  697. *
  698. * This function is called because the TripalField::settingsForm()
  699. * adds it to the form element. By default, Drupal does not provide a
  700. * validate hook for the settings form.
  701. */
  702. function tripal_field_settings_form_validate($element, &$form_state, $form) {
  703. $field = $element['#field'];
  704. $instance = $element['#instance'];
  705. $field_class = $field['type'];
  706. if (tripal_load_include_field_class($field_class)) {
  707. $field = new $field_class($field, $instance);
  708. $field->settingsFormValidate($form, $form_state);
  709. }
  710. }
  711. /**
  712. * Implements hook_field_formatter_settings_summary().
  713. */
  714. function tripal_field_formatter_settings_summary($field, $instance, $view_mode) {
  715. $formatter_class = $instance['display']['default']['type'];
  716. if (tripal_load_include_field_class($formatter_class)) {
  717. $formatter = new $formatter_class($field, $instance);
  718. return $formatter->settingsSummary($view_mode);
  719. }
  720. }
  721. /**
  722. * Implements hook_field_formatter_settings_form().
  723. */
  724. function tripal_field_formatter_settings_form($field, $instance,
  725. $view_mode, $form, &$form_state) {
  726. $formatter_class = $instance['display']['default']['type'];
  727. if (tripal_load_include_field_class($formatter_class)) {
  728. $formatter = new $formatter_class($field, $instance);
  729. $elements = $formatter->settingsForm($view_mode, $form, $form_state);
  730. }
  731. return $elements;
  732. }
  733. /**
  734. * Implements hook_field_widget_form().
  735. */
  736. function tripal_field_widget_form(&$form, &$form_state, $field,
  737. $instance, $langcode, $items, $delta, $element) {
  738. $widget_class = $instance['widget']['type'];
  739. tripal_load_include_field_class($widget_class);
  740. if (class_exists($widget_class)) {
  741. $widget = new $widget_class($field, $instance);
  742. $widget->form($element, $form, $form_state, $langcode, $items, $delta, $element);
  743. }
  744. return $element;
  745. }
  746. /**
  747. * Implements hook_field_widget_form_alter().
  748. */
  749. function tripal_field_widget_form_alter(&$element, &$form_state, $context) {
  750. if (array_key_exists('#field_name', $element)) {
  751. $field_name = $element['#field_name'];
  752. $matches = array();
  753. if (preg_match('/(.+?)__(.+?)$/', $field_name, $matches)) {
  754. $tablename = $matches[1];
  755. $colname = $matches[2];
  756. $schema = chado_get_schema($tablename);
  757. if (!$schema) {
  758. return;
  759. }
  760. // The timelastmodified field exists in many Chado tables. We want
  761. // the form element to update to the most recent time rather than the time
  762. // in the database.
  763. if ($colname == 'timelastmodified' and $schema['fields'][$colname]['type'] == 'datetime') {
  764. // We want the default value for the field to be the current time.
  765. $element['#default_value']['value'] = format_date(time(), 'custom', "Y-m-d H:i:s", 'UTC');
  766. $element['#date_items']['value'] = $element['#default_value']['value'];
  767. }
  768. // We want the date combo fieldset to be collaspible so we will
  769. // add our own theme_wrapper to replace the one added by the date
  770. // module.
  771. if (array_key_exists($colname, $schema['fields']) and $schema['fields'][$colname]['type'] == 'datetime') {
  772. $element['#theme_wrappers'] = array('tripal_chado_date_combo');
  773. }
  774. }
  775. }
  776. }
  777. /**
  778. * Implements hook_field_validate()
  779. */
  780. function tripal_field_validate($entity_type, $entity, $field, $instance,
  781. $langcode, $items, &$errors) {
  782. $field_type = $field['type'];
  783. if (tripal_load_include_field_class($field_type)) {
  784. $tfield = new $field_type($field, $instance);
  785. $tfield->validate($entity_type, $entity, $langcode, $items, $errors);
  786. }
  787. }
  788. /**
  789. * Implements hook_form_FORM_ID_alter().
  790. *
  791. * The field_ui_display_overview_form is used for formatting the display
  792. * or layout of fields attached to an entity and shown on the entity view page.
  793. *
  794. * This function removes the cvterm class and property adder field as those are
  795. * really not meant for users to show or manage.
  796. */
  797. function tripal_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
  798. // Remove the kvproperty_addr field as it isn't ever displayed. It's just used
  799. // on the add/edit form of an entity for adding new property fields.
  800. $fields_names = element_children($form['fields']);
  801. foreach ($fields_names as $field_name) {
  802. $field_info = field_info_field($field_name);
  803. if ($field_info['type'] == 'kvproperty_adder') {
  804. unset($form['fields'][$field_name]);
  805. }
  806. if ($field_info['type'] == 'cvterm_class_adder') {
  807. unset($form['fields'][$field_name]);
  808. }
  809. }
  810. }
  811. /**
  812. * Implements hook_field_is_empty().
  813. */
  814. function tripal_field_is_empty($item, $field) {
  815. // If the $item argument is empty then return TRUE.
  816. if (!$item) {
  817. return TRUE;
  818. }
  819. // If there is no value field then the field is empty.
  820. if (!array_key_exists('value', $item)) {
  821. return TRUE;
  822. }
  823. // If there is a value field but there's nothing in it, the the field is
  824. // empty.
  825. if (array_key_exists('value', $item) and empty($item['value'])){
  826. return TRUE;
  827. }
  828. // Otherwise, the field is not empty.
  829. return FALSE;
  830. }
  831. /**
  832. * Theme function for all TripalFieldWidget objects.
  833. *
  834. * @param $variables
  835. */
  836. function theme_tripal_field_default($variables) {
  837. $element = $variables['element'];
  838. $field = $element['#field'];
  839. $instance = $element['#instance'];
  840. $widget_class = $element['#field_name'] . '_widget';
  841. $langcode = $element['#language'];
  842. $delta = $element['#delta'];
  843. tripal_load_include_field_class($widget_class);
  844. if (class_exists($widget_class)) {
  845. $widget = new $widget_class($field, $instance);
  846. return $widget->theme($element);
  847. }
  848. }