1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Definition of views_plugin_display_attachment.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* The plugin that handles an attachment display.
|
10
|
*
|
11
|
* Attachment displays are secondary displays that are 'attached' to a primary
|
12
|
* display. Effectively they are a simple way to get multiple views within
|
13
|
* the same view. They can share some information.
|
14
|
*
|
15
|
* @ingroup views_display_plugins
|
16
|
*/
|
17
|
class views_plugin_display_attachment extends views_plugin_display {
|
18
|
|
19
|
/**
|
20
|
* {@inheritdoc}
|
21
|
*/
|
22
|
public function option_definition () {
|
23
|
$options = parent::option_definition();
|
24
|
|
25
|
$options['displays'] = array('default' => array());
|
26
|
$options['attachment_position'] = array('default' => 'before');
|
27
|
$options['inherit_arguments'] = array('default' => TRUE, 'bool' => TRUE);
|
28
|
$options['inherit_exposed_filters'] = array('default' => FALSE, 'bool' => TRUE);
|
29
|
$options['inherit_pager'] = array('default' => FALSE, 'bool' => TRUE);
|
30
|
$options['render_pager'] = array('default' => FALSE, 'bool' => TRUE);
|
31
|
|
32
|
return $options;
|
33
|
}
|
34
|
|
35
|
/**
|
36
|
* {@inheritdoc}
|
37
|
*/
|
38
|
public function execute() {
|
39
|
return $this->view->render($this->display->id);
|
40
|
}
|
41
|
|
42
|
/**
|
43
|
* {@inheritdoc}
|
44
|
*/
|
45
|
public function attachment_positions($position = NULL) {
|
46
|
$positions = array(
|
47
|
'before' => t('Before'),
|
48
|
'after' => t('After'),
|
49
|
'both' => t('Both'),
|
50
|
);
|
51
|
|
52
|
if ($position) {
|
53
|
return $positions[$position];
|
54
|
}
|
55
|
|
56
|
return $positions;
|
57
|
}
|
58
|
|
59
|
/**
|
60
|
* Provide the summary for attachment options in the views UI.
|
61
|
*
|
62
|
* This output is returned as an array.
|
63
|
*/
|
64
|
public function options_summary(&$categories, &$options) {
|
65
|
// It is very important to call the parent function here.
|
66
|
parent::options_summary($categories, $options);
|
67
|
|
68
|
$categories['attachment'] = array(
|
69
|
'title' => t('Attachment settings'),
|
70
|
'column' => 'second',
|
71
|
'build' => array(
|
72
|
'#weight' => -10,
|
73
|
),
|
74
|
);
|
75
|
|
76
|
$displays = array_filter($this->get_option('displays'));
|
77
|
if (count($displays) > 1) {
|
78
|
$attach_to = t('Multiple displays');
|
79
|
}
|
80
|
elseif (count($displays) == 1) {
|
81
|
$display = array_shift($displays);
|
82
|
if (!empty($this->view->display[$display])) {
|
83
|
$attach_to = check_plain($this->view->display[$display]->display_title);
|
84
|
}
|
85
|
}
|
86
|
|
87
|
if (!isset($attach_to)) {
|
88
|
$attach_to = t('Not defined');
|
89
|
}
|
90
|
|
91
|
$options['displays'] = array(
|
92
|
'category' => 'attachment',
|
93
|
'title' => t('Attach to'),
|
94
|
'value' => $attach_to,
|
95
|
);
|
96
|
|
97
|
$options['attachment_position'] = array(
|
98
|
'category' => 'attachment',
|
99
|
'title' => t('Attachment position'),
|
100
|
'value' => $this->attachment_positions($this->get_option('attachment_position')),
|
101
|
);
|
102
|
|
103
|
$options['inherit_arguments'] = array(
|
104
|
'category' => 'attachment',
|
105
|
'title' => t('Inherit contextual filters'),
|
106
|
'value' => $this->get_option('inherit_arguments') ? t('Yes') : t('No'),
|
107
|
);
|
108
|
|
109
|
$options['inherit_exposed_filters'] = array(
|
110
|
'category' => 'attachment',
|
111
|
'title' => t('Inherit exposed filters'),
|
112
|
'value' => $this->get_option('inherit_exposed_filters') ? t('Yes') : t('No'),
|
113
|
);
|
114
|
|
115
|
$options['inherit_pager'] = array(
|
116
|
'category' => 'pager',
|
117
|
'title' => t('Inherit pager'),
|
118
|
'value' => $this->get_option('inherit_pager') ? t('Yes') : t('No'),
|
119
|
);
|
120
|
|
121
|
$options['render_pager'] = array(
|
122
|
'category' => 'pager',
|
123
|
'title' => t('Render pager'),
|
124
|
'value' => $this->get_option('render_pager') ? t('Yes') : t('No'),
|
125
|
);
|
126
|
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* Provide the default form for setting options.
|
131
|
*/
|
132
|
public function options_form(&$form, &$form_state) {
|
133
|
// It is very important to call the parent function here.
|
134
|
parent::options_form($form, $form_state);
|
135
|
|
136
|
switch ($form_state['section']) {
|
137
|
case 'inherit_arguments':
|
138
|
$form['#title'] .= t('Inherit contextual filters');
|
139
|
$form['inherit_arguments'] = array(
|
140
|
'#type' => 'checkbox',
|
141
|
'#title' => t('Inherit'),
|
142
|
'#description' => t('Should this display inherit its contextual filter values from the parent display to which it is attached?'),
|
143
|
'#default_value' => $this->get_option('inherit_arguments'),
|
144
|
);
|
145
|
break;
|
146
|
|
147
|
case 'inherit_exposed_filters':
|
148
|
$form['#title'] .= t('Inherit exposed filters');
|
149
|
$form['inherit_exposed_filters'] = array(
|
150
|
'#type' => 'checkbox',
|
151
|
'#title' => t('Inherit'),
|
152
|
'#description' => t('Should this display inherit its exposed filter values from the parent display to which it is attached?'),
|
153
|
'#default_value' => $this->get_option('inherit_exposed_filters'),
|
154
|
);
|
155
|
break;
|
156
|
|
157
|
case 'inherit_pager':
|
158
|
$form['#title'] .= t('Inherit pager');
|
159
|
$form['inherit_pager'] = array(
|
160
|
'#type' => 'checkbox',
|
161
|
'#title' => t('Inherit'),
|
162
|
'#description' => t('Should this display inherit its paging values from the parent display to which it is attached?'),
|
163
|
'#default_value' => $this->get_option('inherit_pager'),
|
164
|
);
|
165
|
break;
|
166
|
|
167
|
case 'render_pager':
|
168
|
$form['#title'] .= t('Render pager');
|
169
|
$form['render_pager'] = array(
|
170
|
'#type' => 'checkbox',
|
171
|
'#title' => t('Render'),
|
172
|
'#description' => t('Should this display render the pager values? This is only meaningful if inheriting a pager.'),
|
173
|
'#default_value' => $this->get_option('render_pager'),
|
174
|
);
|
175
|
break;
|
176
|
|
177
|
case 'attachment_position':
|
178
|
$form['#title'] .= t('Position');
|
179
|
$form['attachment_position'] = array(
|
180
|
'#type' => 'radios',
|
181
|
'#description' => t('Attach before or after the parent display?'),
|
182
|
'#options' => $this->attachment_positions(),
|
183
|
'#default_value' => $this->get_option('attachment_position'),
|
184
|
);
|
185
|
break;
|
186
|
|
187
|
case 'displays':
|
188
|
$form['#title'] .= t('Attach to');
|
189
|
$displays = array();
|
190
|
foreach ($this->view->display as $display_id => $display) {
|
191
|
if (!empty($display->handler) && $display->handler->accept_attachments()) {
|
192
|
$displays[$display_id] = $display->display_title;
|
193
|
}
|
194
|
}
|
195
|
$form['displays'] = array(
|
196
|
'#type' => 'checkboxes',
|
197
|
'#description' => t('Select which display or displays this should attach to.'),
|
198
|
'#options' => $displays,
|
199
|
'#default_value' => $this->get_option('displays'),
|
200
|
);
|
201
|
break;
|
202
|
}
|
203
|
}
|
204
|
|
205
|
/**
|
206
|
* Perform any necessary changes to the form values prior to storage.
|
207
|
* There is no need for this function to actually store the data.
|
208
|
*/
|
209
|
public function options_submit(&$form, &$form_state) {
|
210
|
// It is very important to call the parent function here.
|
211
|
parent::options_submit($form, $form_state);
|
212
|
switch ($form_state['section']) {
|
213
|
case 'inherit_arguments':
|
214
|
case 'inherit_pager':
|
215
|
case 'render_pager':
|
216
|
case 'inherit_exposed_filters':
|
217
|
case 'attachment_position':
|
218
|
case 'displays':
|
219
|
$this->set_option($form_state['section'], $form_state['values'][$form_state['section']]);
|
220
|
break;
|
221
|
}
|
222
|
}
|
223
|
|
224
|
/**
|
225
|
* Attach to another view.
|
226
|
*/
|
227
|
public function attach_to($display_id) {
|
228
|
$displays = $this->get_option('displays');
|
229
|
|
230
|
if (empty($displays[$display_id])) {
|
231
|
return;
|
232
|
}
|
233
|
|
234
|
if (!$this->access()) {
|
235
|
return;
|
236
|
}
|
237
|
|
238
|
// Get a fresh view because our current one has a lot of stuff on it
|
239
|
// because it's already been executed.
|
240
|
$view = $this->view->clone_view();
|
241
|
$view->original_args = $view->args;
|
242
|
|
243
|
$args = $this->get_option('inherit_arguments') ? $this->view->args : array();
|
244
|
$view->set_arguments($args);
|
245
|
$exposed_input = $this->get_option('inherit_exposed_filters') ? $this->view->exposed_input : array();
|
246
|
$view->set_exposed_input($exposed_input);
|
247
|
$view->set_display($this->display->id);
|
248
|
if ($this->get_option('inherit_pager')) {
|
249
|
$view->display_handler->use_pager = $this->view->display[$display_id]->handler->use_pager();
|
250
|
$view->display_handler->set_option('pager', $this->view->display[$display_id]->handler->get_option('pager'));
|
251
|
}
|
252
|
|
253
|
$attachment = $view->execute_display($this->display->id, $args);
|
254
|
|
255
|
switch ($this->get_option('attachment_position')) {
|
256
|
case 'before':
|
257
|
$this->view->attachment_before .= $attachment;
|
258
|
break;
|
259
|
|
260
|
case 'after':
|
261
|
$this->view->attachment_after .= $attachment;
|
262
|
break;
|
263
|
|
264
|
case 'both':
|
265
|
$this->view->attachment_before .= $attachment;
|
266
|
$this->view->attachment_after .= $attachment;
|
267
|
break;
|
268
|
}
|
269
|
|
270
|
$view->destroy();
|
271
|
}
|
272
|
|
273
|
/**
|
274
|
* Attachment displays only use exposed widgets if they are set to inherit
|
275
|
* the exposed filter settings of their parent display.
|
276
|
*/
|
277
|
public function uses_exposed() {
|
278
|
if (!empty($this->options['inherit_exposed_filters']) && parent::uses_exposed()) {
|
279
|
return TRUE;
|
280
|
}
|
281
|
return FALSE;
|
282
|
}
|
283
|
|
284
|
/**
|
285
|
* If an attachment is set to inherit the exposed filter settings from its
|
286
|
* parent display, then don't render and display a second set of exposed
|
287
|
* filter widgets.
|
288
|
*/
|
289
|
public function displays_exposed() {
|
290
|
return $this->options['inherit_exposed_filters'] ? FALSE : TRUE;
|
291
|
}
|
292
|
|
293
|
/**
|
294
|
* {@inheritdoc}
|
295
|
*/
|
296
|
public function use_pager() {
|
297
|
return !empty($this->use_pager);
|
298
|
}
|
299
|
|
300
|
/**
|
301
|
* {@inheritdoc}
|
302
|
*/
|
303
|
public function render_pager() {
|
304
|
return !empty($this->use_pager) && $this->get_option('render_pager');
|
305
|
}
|
306
|
|
307
|
}
|