Projet

Général

Profil

Paste
Télécharger (9,08 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / plugins / views_plugin_display_attachment.inc @ 6f57d8c7

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains the attachment display plugin.
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
  function option_definition () {
19
    $options = parent::option_definition();
20

    
21
    $options['displays'] = array('default' => array());
22
    $options['attachment_position'] = array('default' => 'before');
23
    $options['inherit_arguments'] = array('default' => TRUE, 'bool' => TRUE);
24
    $options['inherit_exposed_filters'] = array('default' => FALSE, 'bool' => TRUE);
25
    $options['inherit_pager'] = array('default' => FALSE, 'bool' => TRUE);
26
    $options['render_pager'] = array('default' => FALSE, 'bool' => TRUE);
27

    
28
    return $options;
29
  }
30

    
31
  function execute() {
32
    return $this->view->render($this->display->id);
33
  }
34

    
35
  function attachment_positions($position = NULL) {
36
    $positions = array(
37
      'before' => t('Before'),
38
      'after' => t('After'),
39
      'both' => t('Both'),
40
    );
41

    
42
    if ($position) {
43
      return $positions[$position];
44
    }
45

    
46
    return $positions;
47
  }
48

    
49
  /**
50
   * Provide the summary for attachment options in the views UI.
51
   *
52
   * This output is returned as an array.
53
   */
54
  function options_summary(&$categories, &$options) {
55
    // It is very important to call the parent function here:
56
    parent::options_summary($categories, $options);
57

    
58
    $categories['attachment'] = array(
59
      'title' => t('Attachment settings'),
60
      'column' => 'second',
61
      'build' => array(
62
        '#weight' => -10,
63
      ),
64
    );
65

    
66
    $displays = array_filter($this->get_option('displays'));
67
    if (count($displays) > 1) {
68
      $attach_to = t('Multiple displays');
69
    }
70
    elseif (count($displays) == 1) {
71
      $display = array_shift($displays);
72
      if (!empty($this->view->display[$display])) {
73
        $attach_to = check_plain($this->view->display[$display]->display_title);
74
      }
75
    }
76

    
77
    if (!isset($attach_to)) {
78
      $attach_to = t('Not defined');
79
    }
80

    
81
    $options['displays'] = array(
82
      'category' => 'attachment',
83
      'title' => t('Attach to'),
84
      'value' => $attach_to,
85
    );
86

    
87
    $options['attachment_position'] = array(
88
      'category' => 'attachment',
89
      'title' => t('Attachment position'),
90
      'value' => $this->attachment_positions($this->get_option('attachment_position')),
91
    );
92

    
93
    $options['inherit_arguments'] = array(
94
      'category' => 'attachment',
95
      'title' => t('Inherit contextual filters'),
96
      'value' => $this->get_option('inherit_arguments') ? t('Yes') : t('No'),
97
    );
98

    
99
    $options['inherit_exposed_filters'] = array(
100
      'category' => 'attachment',
101
      'title' => t('Inherit exposed filters'),
102
      'value' => $this->get_option('inherit_exposed_filters') ? t('Yes') : t('No'),
103
    );
104

    
105
    $options['inherit_pager'] = array(
106
      'category' => 'pager',
107
      'title' => t('Inherit pager'),
108
      'value' => $this->get_option('inherit_pager') ? t('Yes') : t('No'),
109
    );
110

    
111
    $options['render_pager'] = array(
112
      'category' => 'pager',
113
      'title' => t('Render pager'),
114
      'value' => $this->get_option('render_pager') ? t('Yes') : t('No'),
115
    );
116

    
117
  }
118

    
119
  /**
120
   * Provide the default form for setting options.
121
   */
122
  function options_form(&$form, &$form_state) {
123
    // It is very important to call the parent function here:
124
    parent::options_form($form, $form_state);
125

    
126
    switch ($form_state['section']) {
127
      case 'inherit_arguments':
128
        $form['#title'] .= t('Inherit contextual filters');
129
        $form['inherit_arguments'] = array(
130
          '#type' => 'checkbox',
131
          '#title' => t('Inherit'),
132
          '#description' => t('Should this display inherit its contextual filter values from the parent display to which it is attached?'),
133
          '#default_value' => $this->get_option('inherit_arguments'),
134
        );
135
        break;
136
      case 'inherit_exposed_filters':
137
        $form['#title'] .= t('Inherit exposed filters');
138
        $form['inherit_exposed_filters'] = array(
139
          '#type' => 'checkbox',
140
          '#title' => t('Inherit'),
141
          '#description' => t('Should this display inherit its exposed filter values from the parent display to which it is attached?'),
142
          '#default_value' => $this->get_option('inherit_exposed_filters'),
143
        );
144
        break;
145
      case 'inherit_pager':
146
        $form['#title'] .= t('Inherit pager');
147
        $form['inherit_pager'] = array(
148
          '#type' => 'checkbox',
149
          '#title' => t('Inherit'),
150
          '#description' => t('Should this display inherit its paging values from the parent display to which it is attached?'),
151
          '#default_value' => $this->get_option('inherit_pager'),
152
        );
153
        break;
154
      case 'render_pager':
155
        $form['#title'] .= t('Render pager');
156
        $form['render_pager'] = array(
157
          '#type' => 'checkbox',
158
          '#title' => t('Render'),
159
          '#description' => t('Should this display render the pager values? This is only meaningful if inheriting a pager.'),
160
          '#default_value' => $this->get_option('render_pager'),
161
        );
162
        break;
163
      case 'attachment_position':
164
        $form['#title'] .= t('Position');
165
        $form['attachment_position'] = array(
166
          '#type' => 'radios',
167
          '#description' => t('Attach before or after the parent display?'),
168
          '#options' => $this->attachment_positions(),
169
          '#default_value' => $this->get_option('attachment_position'),
170
        );
171
        break;
172
      case 'displays':
173
        $form['#title'] .= t('Attach to');
174
        $displays = array();
175
        foreach ($this->view->display as $display_id => $display) {
176
          if (!empty($display->handler) && $display->handler->accept_attachments()) {
177
            $displays[$display_id] = $display->display_title;
178
          }
179
        }
180
        $form['displays'] = array(
181
          '#type' => 'checkboxes',
182
          '#description' => t('Select which display or displays this should attach to.'),
183
          '#options' => $displays,
184
          '#default_value' => $this->get_option('displays'),
185
        );
186
        break;
187
    }
188
  }
189

    
190
  /**
191
   * Perform any necessary changes to the form values prior to storage.
192
   * There is no need for this function to actually store the data.
193
   */
194
  function options_submit(&$form, &$form_state) {
195
    // It is very important to call the parent function here:
196
    parent::options_submit($form, $form_state);
197
    switch ($form_state['section']) {
198
      case 'inherit_arguments':
199
      case 'inherit_pager':
200
      case 'render_pager':
201
      case 'inherit_exposed_filters':
202
      case 'attachment_position':
203
      case 'displays':
204
        $this->set_option($form_state['section'], $form_state['values'][$form_state['section']]);
205
        break;
206
    }
207
  }
208

    
209
  /**
210
   * Attach to another view.
211
   */
212
  function attach_to($display_id) {
213
    $displays = $this->get_option('displays');
214

    
215
    if (empty($displays[$display_id])) {
216
      return;
217
    }
218

    
219
    if (!$this->access()) {
220
      return;
221
    }
222

    
223
    // Get a fresh view because our current one has a lot of stuff on it because it's
224
    // already been executed.
225
    $view = $this->view->clone_view();
226
    $view->original_args = $view->args;
227

    
228
    $args = $this->get_option('inherit_arguments') ? $this->view->args : array();
229
    $view->set_arguments($args);
230
    $exposed_input = $this->get_option('inherit_exposed_filters') ? $this->view->exposed_input : array();
231
    $view->set_exposed_input($exposed_input);
232
    $view->set_display($this->display->id);
233
    if ($this->get_option('inherit_pager')) {
234
      $view->display_handler->use_pager = $this->view->display[$display_id]->handler->use_pager();
235
      $view->display_handler->set_option('pager', $this->view->display[$display_id]->handler->get_option('pager'));
236
    }
237

    
238
    $attachment = $view->execute_display($this->display->id, $args);
239

    
240
    switch ($this->get_option('attachment_position')) {
241
      case 'before':
242
        $this->view->attachment_before .= $attachment;
243
        break;
244
      case 'after':
245
        $this->view->attachment_after .= $attachment;
246
        break;
247
      case 'both':
248
        $this->view->attachment_before .= $attachment;
249
        $this->view->attachment_after .= $attachment;
250
        break;
251
    }
252

    
253
    $view->destroy();
254
  }
255

    
256
  /**
257
   * Attachment displays only use exposed widgets if
258
   * they are set to inherit the exposed filter settings
259
   * of their parent display.
260
   */
261
  function uses_exposed() {
262
    if (!empty($this->options['inherit_exposed_filters']) && parent::uses_exposed()) {
263
      return TRUE;
264
    }
265
    return FALSE;
266
  }
267

    
268
  /**
269
   * If an attachment is set to inherit the exposed filter
270
   * settings from its parent display, then don't render and
271
   * display a second set of exposed filter widgets.
272
   */
273
  function displays_exposed() {
274
    return $this->options['inherit_exposed_filters'] ? FALSE : TRUE;
275
  }
276

    
277
  function use_pager() {
278
    return !empty($this->use_pager);
279
  }
280

    
281
  function render_pager() {
282
    return !empty($this->use_pager) && $this->get_option('render_pager');
283
  }
284
}