tripal_views_handler_field_entity.inc 3.8 KB

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