schema__publication_formatter.inc

File

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