chado_views_handler_field.inc

  1. 3.x tripal_chado/views_handlers/chado_views_handler_field.inc
  2. 1.x tripal_views/views/handlers/chado_views_handler_field.inc

A chado wrapper for the views_handler_field.

Handles fields which may be aggregated during the chado join process. This field will render an aggregated field as a pre_rendered list and will dynamically detect whether the field is aggregated or not.

File

tripal_views/views/handlers/chado_views_handler_field.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * A chado wrapper for the views_handler_field.
  5. *
  6. * Handles fields which may be aggregated during the chado join process. This field
  7. * will render an aggregated field as a pre_rendered list and will dynamically detect
  8. * whether the field is aggregated or not.
  9. */
  10. class chado_views_handler_field extends views_handler_field {
  11. function init(&$view, $options) {
  12. include_once('chado_wrapper_functions.inc');
  13. parent::init($view, $options);
  14. }
  15. /**
  16. * Defines the defaults for the options form
  17. */
  18. function option_definition() {
  19. $options = parent::option_definition();
  20. $options['type'] = array('default' => 'separator');
  21. $options['separator'] = array('default' => ', ');
  22. return $options;
  23. }
  24. /**
  25. * Defines the options form (form available to admin when they add a field to a view)
  26. */
  27. function options_form(&$form, &$form_state) {
  28. parent::options_form($form, $form_state);
  29. // Add a link to node checkbox
  30. // but only if this base table is linked to a node and this field is from the base_table
  31. if (tripal_core_is_tripal_node_type($this->table) && $this->table == $this->view->base_table) {
  32. // If there is a Node: NID field then show a link to node checkbox
  33. if (isset($this->view->display['default']->display_options['fields']['nid'])) {
  34. $form['link_to_node'] = array(
  35. '#type' => 'checkbox',
  36. '#title' => t('Link to Node'),
  37. '#description' => t('If a given row is associated with a drupal node then '
  38. .'this field will appear as a link, linking the user to that node. Otherwise,'
  39. .' no link will be displayed.'),
  40. '#default_value' => $this->options['link_to_node'],
  41. );
  42. }
  43. // Otherwise inform the user that they need to add a Node:Nid field
  44. // to get this functionality
  45. else {
  46. $form['link_to_node'] = array(
  47. '#type' => 'item',
  48. '#value' => "This field has the ability to link to it's corresponding node. "
  49. . "However, you first need to add the NID field associated with the node. "
  50. . "Simple set the NID field to hidden when adding it to ensure it's not "
  51. . "shown in the resulting view."
  52. );
  53. }
  54. }
  55. $form['type'] = array(
  56. '#type' => 'radios',
  57. '#title' => t('Display type'),
  58. '#options' => array(
  59. 'ul' => t('Unordered list'),
  60. 'ol' => t('Ordered list'),
  61. 'separator' => t('Simple separator'),
  62. ),
  63. '#default_value' => $this->options['type'],
  64. );
  65. $form['separator'] = array(
  66. '#type' => 'textfield',
  67. '#title' => t('Separator'),
  68. '#default_value' => $this->options['separator'],
  69. '#process' => array('views_process_dependency'),
  70. '#dependency' => array('radio:options[type]' => array('separator')),
  71. );
  72. }
  73. /**
  74. * Determines whether the current field is aggregated or not
  75. * Note: The parent::query() takes care of adding the field to the query, etc.
  76. */
  77. function query() {
  78. parent::query();
  79. $this->aggregated = chado_wrapper_is_aggregated_by_join($this);
  80. }
  81. /**
  82. * Splits the aggregated values up for use in rendering
  83. */
  84. function pre_render($values) {
  85. // further check the results to see if this field is a postgresql array
  86. $this->aggregated = chado_wrapper_is_aggregated_by_result($this, $values);
  87. // Split Aggregated Results
  88. chado_wrapper_split_array_agg_results($this, $values);
  89. }
  90. /**
  91. * Render the field.
  92. *
  93. * Note: Checks to see if we have an array or simple field. If we have an array, then
  94. * split it up and render each part using the parent render functionality.
  95. *
  96. * @param $values
  97. * The values retrieved from the database.
  98. */
  99. function render($values) {
  100. if ($this->options['link_to_node']) {
  101. $link_text = chado_wrapper_render_items($this, $values);
  102. return $this->render_node_link($link_text, $values);
  103. }
  104. else {
  105. return chado_wrapper_render_items($this, $values);
  106. }
  107. }
  108. /**
  109. * Will render the supplied text as a link to the node
  110. *
  111. * @param $link_text
  112. * The text to render as a link (ie; the text that would normally become underlined
  113. *
  114. * @return
  115. * A rendered link to the node based on the nid field
  116. */
  117. function render_node_link($link_text, $values) {
  118. $node_field = $this->view->field['nid'];
  119. if (isset($node_field->aliases['nid'])) {
  120. $nid = $values->{$node_field->aliases['nid']};
  121. }
  122. if ($nid) {
  123. return l($link_text, 'node/' . $nid);
  124. }
  125. else {
  126. return $link_text;
  127. }
  128. }
  129. function parent_render($val) {
  130. return parent::render($val);
  131. }
  132. }