views_plugin_style_jump_menu.inc

  1. 3.x plugins/views_plugin_style_jump_menu.inc
  2. 2.x plugins/views_plugin_style_jump_menu.inc

Contains the table style plugin.

File

plugins/views_plugin_style_jump_menu.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains the table style plugin.
  5. */
  6. /**
  7. * Style plugin to render each item as a row in a table.
  8. *
  9. * @ingroup views_style_plugins
  10. */
  11. class views_plugin_style_jump_menu extends views_plugin_style {
  12. function option_definition() {
  13. $options = parent::option_definition();
  14. $options['hide'] = array('default' => FALSE, 'bool' => TRUE);
  15. $options['path'] = array('default' => '');
  16. $options['text'] = array('default' => 'Go', 'translatable' => TRUE);
  17. $options['label'] = array('default' => '', 'translatable' => TRUE);
  18. $options['choose'] = array('default' => '- Choose -', 'translatable' => TRUE);
  19. $options['inline'] = array('default' => TRUE, 'bool' => TRUE);
  20. $options['default_value'] = array('default' => FALSE, 'bool' => TRUE);
  21. return $options;
  22. }
  23. /**
  24. * Render the given style.
  25. */
  26. function options_form(&$form, &$form_state) {
  27. parent::options_form($form, $form_state);
  28. $handlers = $this->display->handler->get_handlers('field');
  29. if (empty($handlers)) {
  30. $form['error_markup'] = array(
  31. '#markup' => t('You need at least one field before you can configure your jump menu settings'),
  32. '#prefix' => '<div class="error messages">',
  33. '#suffix' => '</div>',
  34. );
  35. return;
  36. }
  37. $form['markup'] = array(
  38. '#markup' => t('To properly configure a jump menu, you must select one field that will represent the path to utilize. You should then set that field to exclude. All other displayed fields will be part of the menu. Please note that all HTML will be stripped from this output as select boxes cannot show HTML.'),
  39. '#prefix' => '<div class="form-item description">',
  40. '#suffix' => '</div>',
  41. );
  42. foreach ($handlers as $id => $handler) {
  43. $options[$id] = $handler->ui_name();
  44. }
  45. $form['path'] = array(
  46. '#type' => 'select',
  47. '#title' => t('Path field'),
  48. '#options' => $options,
  49. '#default_value' => $this->options['path'],
  50. );
  51. $form['hide'] = array(
  52. '#type' => 'checkbox',
  53. '#title' => t('Hide the "Go" button'),
  54. '#default_value' => !empty($this->options['hide']),
  55. '#description' => t('If hidden, this button will only be hidden for users with javascript and the page will automatically jump when the select is changed.'),
  56. );
  57. $form['text'] = array(
  58. '#type' => 'textfield',
  59. '#title' => t('Button text'),
  60. '#default_value' => $this->options['text'],
  61. );
  62. $form['label'] = array(
  63. '#type' => 'textfield',
  64. '#title' => t('Selector label'),
  65. '#default_value' => $this->options['label'],
  66. '#description' => t('The text that will appear as the the label of the selector element. If blank no label tag will be used.'),
  67. );
  68. $form['choose'] = array(
  69. '#type' => 'textfield',
  70. '#title' => t('Choose text'),
  71. '#default_value' => $this->options['choose'],
  72. '#description' => t('The text that will appear as the selected option in the jump menu.'),
  73. );
  74. $form['inline'] = array(
  75. '#type' => 'checkbox',
  76. '#title' => t('Set this field to display inline'),
  77. '#default_value' => !empty($this->options['inline']),
  78. );
  79. $form['default_value'] = array(
  80. '#type' => 'checkbox',
  81. '#title' => t('Select the current contextual filter value'),
  82. '#default_value' => !empty($this->options['default_value']),
  83. '#description' => t('If checked, the current path will be displayed as the default option in the jump menu, if applicable.'),
  84. );
  85. }
  86. /**
  87. * Render the display in this style.
  88. *
  89. * This is overridden so that we can render our grouping specially.
  90. */
  91. function render() {
  92. $sets = $this->render_grouping($this->view->result, $this->options['grouping']);
  93. // Turn this all into an $options array for the jump menu.
  94. $this->view->row_index = 0;
  95. $options = array();
  96. $paths = array();
  97. foreach ($sets as $title => $records) {
  98. foreach ($records as $row_index => $row) {
  99. $this->view->row_index = $row_index;
  100. $path = strip_tags(decode_entities($this->get_field($this->view->row_index, $this->options['path'])));
  101. // Putting a '/' in front messes up url() so let's take that out
  102. // so users don't shoot themselves in the foot.
  103. $base_path = base_path();
  104. if (strpos($path, $base_path) === 0) {
  105. $path = drupal_substr($path, drupal_strlen($base_path));
  106. }
  107. // use drupal_parse_url() to preserve query and fragment in case the user
  108. // wants to do fun tricks.
  109. $url_options = drupal_parse_url($path);
  110. $path = url($url_options['path'], $url_options);
  111. $field = strip_tags(decode_entities($this->row_plugin->render($row)));
  112. $key = md5($path . $field) . "::" . $path;
  113. if ($title) {
  114. $options[$title][$key] = $field;
  115. }
  116. else {
  117. $options[$key] = $field;
  118. }
  119. $paths[$path] = $key;
  120. $this->view->row_index++;
  121. }
  122. }
  123. unset($this->view->row_index);
  124. $default_value = '';
  125. if ($this->options['default_value']) {
  126. $lookup_options = array();
  127. // We need to check if the path is absolute
  128. // or else language is not taken in account.
  129. if ($this->view->display[$this->view->current_display]->display_options['fields'][$this->options['path']]['absolute']) {
  130. $lookup_options['absolute'] = TRUE;
  131. }
  132. $lookup_url = url($_GET['q'], $lookup_options);
  133. if (!empty($paths[$lookup_url])) {
  134. $default_value = $paths[$lookup_url];
  135. }
  136. }
  137. ctools_include('jump-menu');
  138. $settings = array(
  139. 'hide' => $this->options['hide'],
  140. 'button' => $this->options['text'],
  141. 'title' => $this->options['label'],
  142. 'choose' => $this->options['choose'],
  143. 'inline' => $this->options['inline'],
  144. 'default_value' => $default_value,
  145. );
  146. $form = drupal_get_form('ctools_jump_menu', $options, $settings);
  147. return $form;
  148. }
  149. function render_set($title, $records) {
  150. $options = array();
  151. $fields = $this->rendered_fields;
  152. }
  153. }