comparison sites/all/modules/quicktabs/includes/quicktabs_style_plugin.inc @ 2:b74b41bb73f0

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