schema__publication_formatter.inc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. class schema__publication_formatter extends ChadoFieldFormatter {
  3. // The default label 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 = ['schema__publication'];
  7. /**
  8. *
  9. * @see TripalFieldFormatter::view()
  10. */
  11. public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
  12. $list_items = [];
  13. $chado_table = $this->instance['settings']['chado_table'];
  14. // If there are no items, we don't want to return any markup.
  15. if (count($items) == 0 or (count($items) == 1 and empty($items[0]['value']))) {
  16. $element[0] = [
  17. '#type' => 'markup',
  18. '#markup' => 'There are no publications associated with this record.',
  19. ];
  20. return;
  21. }
  22. foreach ($items as $delta => $item) {
  23. if (empty($item['value'])) {
  24. continue;
  25. }
  26. $title = isset($item['value']['TPUB:0000039']) ? $item['value']['TPUB:0000039'] : '';
  27. $citation = isset($item['value']['TPUB:0000003']) ? $item['value']['TPUB:0000003'] : '';
  28. $entity = (is_array($item['value']) && array_key_exists('entity', $item['value'])) ? $item['value']['entity'] : '';
  29. if ($entity) {
  30. list($entity_type, $entity_id) = explode(':', $entity);
  31. $new_title = l($title, 'bio_data/' . $entity_id);
  32. // Escape anything that isn't alphanumeric
  33. $title = preg_replace('/([^\w])/', '\\\\$1', $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 = [
  48. 'title' => '',
  49. 'items' => $list_items,
  50. 'type' => 'ol',
  51. 'attributes' => [],
  52. ];
  53. $list = theme_item_list($list);
  54. }
  55. $element[0] = [
  56. '#type' => 'markup',
  57. '#markup' => $list,
  58. ];
  59. }
  60. }