quicktabs_style_plugin.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. // Id:$
  3. /**
  4. * @file Add Quicktabs style plugins to Views.
  5. */
  6. /**
  7. * Style plugin to display Quicktabs.
  8. */
  9. class quicktabs_style_plugin extends views_plugin_style {
  10. // Allow some options for the style.
  11. function option_definition() {
  12. $options = parent::option_definition();
  13. $options['tab_style'] = array('default' => 'default');
  14. $options['tab_title_field'] = array('default' => NULL);
  15. return $options;
  16. }
  17. // Create the options form.
  18. function options_form(&$form, &$form_state) {
  19. parent::options_form($form, $form_state);
  20. $options = array();
  21. $styles = module_invoke_all('quicktabs_tabstyles');
  22. // The keys used for options must be valid html id-s.
  23. // Removing the css file path, because that can't be used.
  24. foreach ($styles as $style) {
  25. $options[$style] = $style;
  26. }
  27. ksort($options);
  28. $form['tab_style'] = array(
  29. '#type' => 'select',
  30. '#title' => t('Tab style'),
  31. '#options' => array('nostyle' => t('No style'), 'default' => t('Default style')) + $options,
  32. '#default_value' => $this->options['tab_style'],
  33. '#description' => t('The tab style that will be applied to this set of tabs. Note that this style may not show in the live preview.'),
  34. '#weight' => -5,
  35. );
  36. if (isset($form['grouping'])) {
  37. $options = array();
  38. foreach (element_children($form['grouping']) as $key => $value) {
  39. if (!empty($form['grouping'][$key]['field']['#options']) && is_array($form['grouping'][$key]['field']['#options'])) {
  40. $options = array_merge($options, $form['grouping'][$key]['field']['#options']);
  41. }
  42. }
  43. unset($options['']);
  44. $form['tab_title_field'] = array(
  45. '#type' => 'select',
  46. '#title' => t('Title field'),
  47. '#options' => $options,
  48. '#required' => TRUE,
  49. '#default_value' => $this->options['tab_title_field'],
  50. '#description' => t('Select the field that will be used as the tab title.'),
  51. '#weight' => -3,
  52. );
  53. }
  54. }
  55. // Ensure we have all the settings necessary to render into tabs.
  56. function validate() {
  57. $errors = parent::validate();
  58. // Ensure that we're using the field row style.
  59. if (!$this->row_plugin->uses_fields()) {
  60. $errors[] = t('Display "@display" uses the "@style" row style, but the Quicktabs display style requires use of the "Fields" row style.', array('@display' => $this->display->display_title, '@style' => $this->row_plugin->definition['title']));
  61. }
  62. // Ensure that a valid tab title field is selected.
  63. $fields = $this->display->handler->get_handlers('field');
  64. if (empty($this->options['tab_title_field']) || !isset($fields[$this->options['tab_title_field']])) {
  65. $errors[] = t('The Quicktabs display style requires that a field be configured to be used as the tab title.');
  66. }
  67. return $errors;
  68. }
  69. // Override the render functionality.
  70. function render() {
  71. if (empty($this->row_plugin)) {
  72. vpr('views_plugin_style_default: Missing row plugin');
  73. return;
  74. }
  75. $view = $this->view;
  76. $qt_name = 'view__' . $view->name .'__'. $view->current_display;
  77. // Group the rows according to the grouping field, if specified.
  78. $sets = $this->render_grouping($this->view->result, $this->options['grouping']);
  79. $tabs = array();
  80. foreach ($sets as $title => $records) {
  81. if ($this->uses_row_plugin()) {
  82. $rows = array();
  83. foreach ($records as $row_index => $row) {
  84. $this->view->row_index = $row_index;
  85. $rows[] = $this->row_plugin->render($row);
  86. }
  87. }
  88. else {
  89. $rows = $records;
  90. }
  91. // If grouped, we'll be using the group title for each tab.
  92. if ($this->options['grouping']) {
  93. // Remove labels from titles.
  94. foreach (element_children($this->options['grouping']) as $key => $value) {
  95. if (!empty($this->view->field[$this->options['grouping'][$key]['field']]->options['label'])) {
  96. $title = str_replace($this->view->field[$this->options['grouping'][$key]['field']]->options['label'] . ': ', '', $title);
  97. }
  98. }
  99. $contents = '';
  100. foreach ($rows as $row) {
  101. $contents .= '<div class="quicktabs-views-group">' . $row . '</div>';
  102. }
  103. $tabs[] = array(
  104. 'title' => $title,
  105. 'contents' => array('#markup' => $contents),
  106. );
  107. }
  108. // If not grouped, there's just one set of rows that we loop through.
  109. else {
  110. foreach ($rows as $index => $row) {
  111. $title = $this->get_field($index, $this->options['tab_title_field']);
  112. $tabs[] = array(
  113. 'title' => $title,
  114. 'contents' => array('#markup' => $row),
  115. );
  116. }
  117. }
  118. }
  119. $overrides = array('style' => $view->style_options['tab_style'], 'sorted' => TRUE);
  120. $quicktabs = quicktabs_build_quicktabs($qt_name, $overrides, $tabs);
  121. $output = drupal_render($quicktabs);
  122. // If doing a live preview, add the JavaScript directly to the output.
  123. if (isset($view->live_preview) && $view->live_preview) {
  124. $js = drupal_add_js();
  125. $qtsettings = array();
  126. foreach ($js['settings']['data'] as $settings) {
  127. if (isset($settings['quicktabs']['qt_'. $qt_name])) {
  128. $qtsettings = $settings['quicktabs']['qt_'. $qt_name];
  129. break;
  130. }
  131. }
  132. $output .= "<script type=\"text/javascript\">\n";
  133. $output .= "Drupal.settings.quicktabs = Drupal.settings.quicktabs || {};\n";
  134. $output .= "jQuery.extend(Drupal.settings.quicktabs, ". json_encode(array('qt_'. $qt_name => $qtsettings)) .");\n";
  135. $output .= "</script>\n";
  136. }
  137. unset($view->row_index);
  138. return $output;
  139. }
  140. }