tripal_views_handler_field_entity.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @file
  4. * Contains the basic 'entity' field handler.
  5. */
  6. /**
  7. * Field handler to provide simple renderer that allows linking to a entity.
  8. * Definition terms:
  9. * - link_to_entity default: Should this field have the checkbox "link to entity" enabled by default.
  10. *
  11. * @ingroup views_field_handlers
  12. */
  13. class tripal_views_handler_field_entity extends tripal_views_handler_field {
  14. function init(&$view, &$options) {
  15. parent::init($view, $options);
  16. // Don't add the additional fields to groupby
  17. if (!empty($this->options['link_to_entity'])) {
  18. $this->additional_fields['id'] = array('table' => 'tripal_entity', 'field' => 'id');
  19. if (module_exists('translation')) {
  20. $this->additional_fields['language'] = array('table' => 'entity', 'field' => 'language');
  21. }
  22. }
  23. }
  24. function option_definition() {
  25. $options = parent::option_definition();
  26. $options['link_to_entity'] = array('default' => isset($this->definition['link_to_entity default']) ? $this->definition['link_to_entity default'] : FALSE, 'bool' => TRUE);
  27. return $options;
  28. }
  29. /**
  30. * Provide link to entity option
  31. */
  32. function options_form(&$form, &$form_state) {
  33. $form['link_to_entity'] = array(
  34. '#title' => t('Link this field to the original piece of content'),
  35. '#description' => t("Enable to override this field's links."),
  36. '#type' => 'checkbox',
  37. '#default_value' => !empty($this->options['link_to_entity']),
  38. );
  39. parent::options_form($form, $form_state);
  40. }
  41. /**
  42. * Get the value that's supposed to be rendered.
  43. *
  44. * This api exists so that other modules can easy set the values of the field
  45. * without having the need to change the render method as well.
  46. *
  47. * @param $values
  48. * An object containing all retrieved values.
  49. * @param $field
  50. * Optional name of the field where the value is stored.
  51. */
  52. function get_value($values, $field = NULL) {
  53. $alias = isset($field) ? $this->aliases[$field] : $this->field_alias;
  54. // For some reason the alias isn't being added to the $this->aliases
  55. // variable when the user clicks the checkbox 'Link this field to the
  56. // original piece of content'. That may need to be investigated and is
  57. // probably a side effect of using the views_handler_field as a parent
  58. // class that is expecting something more for SQL. We don't use aliases
  59. // for fields so the following if statement will fix the problem.
  60. if (!$alias) {
  61. $alias = $this->field_alias;
  62. }
  63. if (isset($values->{$alias})) {
  64. return $values->{$alias}->id;
  65. }
  66. }
  67. /**
  68. * Render whatever the data is as a link to the entity.
  69. *
  70. * Data should be made XSS safe prior to calling this function.
  71. */
  72. function render_link($data, $values) {
  73. if (!empty($this->options['link_to_entity']) && !empty($this->additional_fields['id'])) {
  74. if ($data !== NULL && $data !== '') {
  75. $alias = $this->field_alias;
  76. if (entity_access('view', 'TripalEntity', $values->{$alias})) {
  77. $this->options['alter']['make_link'] = TRUE;
  78. $this->options['alter']['path'] = "bio_data/" . $this->get_value($values, 'id');
  79. if (isset($this->aliases['language'])) {
  80. $languages = language_list();
  81. $language = $this->get_value($values, 'language');
  82. if (isset($languages[$language])) {
  83. $this->options['alter']['language'] = $languages[$language];
  84. }
  85. else {
  86. unset($this->options['alter']['language']);
  87. }
  88. }
  89. }
  90. }
  91. else {
  92. $this->options['alter']['make_link'] = FALSE;
  93. }
  94. }
  95. return $data;
  96. }
  97. function render($values) {
  98. $value = $this->get_value($values);
  99. return $this->render_link($this->sanitize_value($value), $values);
  100. }
  101. }