schema__publication_formatter.inc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. // 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] = array(
  17. '#type' => 'markup',
  18. '#markup' => 'There are no publications associated with this record.',
  19. );
  20. return;
  21. }
  22. foreach ($items as $delta => $item) {
  23. $title = isset($item['value']['TPUB:0000039']) ? $item['value']['TPUB:0000039'] : '';
  24. $citation = isset($item['value']['TPUB:0000003']) ? $item['value']['TPUB:0000003'] : '';
  25. $entity = (is_array($item['value']) && array_key_exists('entity', $item['value'])) ? $item['value']['entity'] : '';
  26. if ($entity) {
  27. list($entity_type, $entity_id) = explode(':', $entity);
  28. $new_title = l($title, 'bio_data/' . $entity_id);
  29. // Escape anything that isn't alphanumeric
  30. $title = preg_replace('/([^\w])/', '\\\\$1', $title);
  31. $citation = preg_replace("/$title/", $new_title, $citation);
  32. }
  33. $list_items[] = $citation;
  34. }
  35. $list = '';
  36. krsort($list_items, SORT_NUMERIC);
  37. if (count($list_items) == 0) {
  38. $list = 'There are no publications associated with this record.';
  39. }
  40. if (count($list_items) == 1) {
  41. $list = $list_items[0];
  42. }
  43. if (count($list_items) > 1) {
  44. $list = array(
  45. 'title' => '',
  46. 'items' => $list_items,
  47. 'type' => 'ol',
  48. 'attributes' => array(),
  49. );
  50. $list = theme_item_list($list);
  51. }
  52. $element[0] = array(
  53. '#type' => 'markup',
  54. '#markup' => $list,
  55. );
  56. }
  57. }