schema__publication_formatter.inc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. class schema__publication_formatter extends ChadoFieldFormatter {
  3. // The default lable for this field.
  4. public static $default_label = 'Publication';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array('schema__publication');
  7. /**
  8. *
  9. * @see TripalFieldFormatter::view()
  10. */
  11. public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
  12. $list_items = array();
  13. $chado_table = $this->instance['settings']['chado_table'];
  14. foreach ($items as $delta => $item) {
  15. if (empty($item['value'])) {
  16. continue;
  17. }
  18. // Ignore the null publication.
  19. if ($item['uniquename'] == 'null') {
  20. continue;
  21. }
  22. $title = isset($item['value']['TPUB:0000039']) ? $item['value']['TPUB:0000039'] : '';
  23. $citation = isset($item['value']['TPUB:0000003']) ? $item['value']['TPUB:0000003'] : '';
  24. $entity = array_key_exists('entity', $item['value']) ? $item['value']['entity'] : '';
  25. if ($entity) {
  26. list($entity_type, $entity_id) = explode(':', $entity);
  27. $new_title = l($title, 'bio_data/' . $entity_id);
  28. // If a title has parenthesis we need to escape them for the
  29. // regular expression to work.
  30. $title = preg_replace('/\(/', '\(', $title);
  31. $title = preg_replace('/\)/', '\)', $title);
  32. // We also need to escape any forward slashes.
  33. $title = preg_replace('/\//', '\/', $title);
  34. $citation = preg_replace("/$title/", $new_title, $citation);
  35. }
  36. $list_items[] = $citation;
  37. }
  38. $list = '';
  39. krsort($list_items, SORT_NUMERIC);
  40. if (count($list_items) == 0) {
  41. $list = 'There are no publications associated with this record.';
  42. }
  43. if (count($list_items) == 1) {
  44. $list = $list_items[0];
  45. }
  46. if (count($list_items) > 1) {
  47. $list = array(
  48. 'title' => '',
  49. 'items' => $list_items,
  50. 'type' => 'ol',
  51. 'attributes' => array(),
  52. );
  53. $list = theme_item_list($list);
  54. }
  55. $element[0] = array(
  56. '#type' => 'markup',
  57. '#markup' => $list,
  58. );
  59. }
  60. }