views_handler_field_links.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_field_links.
  5. */
  6. /**
  7. * A abstract handler which provides a collection of links.
  8. *
  9. * @ingroup views_field_handlers
  10. */
  11. class views_handler_field_links extends views_handler_field {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function option_definition() {
  16. $options = parent::option_definition();
  17. $options['fields'] = array('default' => array());
  18. $options['check_access'] = array('default' => FALSE);
  19. $options['destination'] = array('default' => TRUE, 'bool' => TRUE);
  20. return $options;
  21. }
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function options_form(&$form, &$form_state) {
  26. parent::options_form($form, $form_state);
  27. $all_fields = $this->view->display_handler->get_field_labels();
  28. // Offer to include only those fields that follow this one.
  29. $field_options = array_slice($all_fields, 0, array_search($this->options['id'], array_keys($all_fields)));
  30. $form['fields'] = array(
  31. '#type' => 'checkboxes',
  32. '#title' => t('Fields'),
  33. '#description' => t('Fields to be included as links.'),
  34. '#options' => $field_options,
  35. '#default_value' => $this->options['fields'],
  36. '#required' => TRUE,
  37. );
  38. $form['check_access'] = array(
  39. '#type' => 'checkbox',
  40. '#title' => t('Evaluate router path for access'),
  41. '#default_value' => $this->options['check_access'],
  42. '#description' => t('Will check if the path exists and is accessible for the current user. Might be useful, might be slow.'),
  43. );
  44. $form['destination'] = array(
  45. '#type' => 'checkbox',
  46. '#title' => t('Include destination'),
  47. '#description' => t('Include a "destination" parameter in the link to return the user to the original view upon completing the link action.'),
  48. '#default_value' => $this->options['destination'],
  49. );
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function options_submit(&$form, &$form_state) {
  55. // Remove unselected options.
  56. $form_state['values']['options']['fields'] = array_filter($form_state['values']['options']['fields']);
  57. parent::options_submit($form, $form_state);
  58. }
  59. /**
  60. * Return the list of links of this field.
  61. *
  62. * @return array
  63. * The links which are used by the render function.
  64. */
  65. public function get_links() {
  66. $links = array();
  67. foreach ($this->options['fields'] as $field) {
  68. if (empty($this->view->field[$field]->last_render_text)) {
  69. continue;
  70. }
  71. $title = $this->view->field[$field]->last_render_text;
  72. // Use the alter settings for the link field source not this links field.
  73. $alter = $this->view->field[$field]->options['alter'];
  74. $url = array('query' => array());
  75. // Copy code from views_handler_field::render_as_link().
  76. $path = $alter['path'];
  77. if (!empty($path) && $path != '<front>') {
  78. // Leave path alone on <front> as strip_tags() would remove this.
  79. // Replace tokens and strip any HTML tags in the path.
  80. $tokens = $this->get_render_tokens(array());
  81. $path = strip_tags(decode_entities(strtr($path, $tokens)));
  82. if (!empty($alter['path_case']) && $alter['path_case'] != 'none') {
  83. $path = $this->case_transform($path, $alter['path_case']);
  84. }
  85. if (!empty($alter['replace_spaces'])) {
  86. $path = str_replace(' ', '-', $path);
  87. }
  88. $url = drupal_parse_url($path);
  89. if (empty($url)) {
  90. // Seriously malformed URLs may return FALSE or empty arrays.
  91. continue;
  92. }
  93. $path = $url['path'];
  94. // Check router menu item access for the current user.
  95. if ($this->options['check_access']) {
  96. $menu_item = menu_get_item($path);
  97. if (!$menu_item || empty($menu_item['access'])) {
  98. continue;
  99. }
  100. }
  101. if (!empty($this->options['destination']) && empty($alter['external'])) {
  102. // Override any destination argument included in URL.
  103. $url['query'] = array_merge($url['query'], drupal_get_destination());
  104. }
  105. // Omit tweaks of query, fragment, and link_class.
  106. $alt = strtr($alter['alt'], $tokens);
  107. if ($alt && $alt != $title) {
  108. // Set the title attribute only if it improves accessibility.
  109. $url['attributes']['title'] = decode_entities($alt);
  110. }
  111. if (!empty($alter['rel']) && $rel = strtr($alter['rel'], $tokens)) {
  112. $url['attributes']['rel'] = $rel;
  113. }
  114. $target = check_plain(trim(strtr($alter['target'], $tokens)));
  115. if (!empty($target)) {
  116. $url['attributes']['target'] = $target;
  117. }
  118. }
  119. $links[$field] = array(
  120. 'href' => $path,
  121. 'title' => $title,
  122. ) + $url;
  123. }
  124. return $links;
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function query() {
  130. }
  131. }