Projet

Général

Profil

Paste
Télécharger (7,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / views_content / plugins / content_types / views_row.inc @ 219d19c4

1
<?php
2

    
3
/**
4
 * @file
5
 * Allow a view context to display individual rows.
6
 */
7

    
8
$plugin = array(
9
  'title' => t('View row'),
10
  'category' => t('View context'),
11
  'icon' => 'icon_views_page.png',
12
  'description' => t('Display all or a specific amount of rows from a loaded view context.'),
13
  'required context' => new ctools_context_required(t('View'), 'view'),
14
  'defaults' => array(
15
    'rows' => array(),
16
    'use_fields' => array(),
17
    'fields' => array(),
18
  ),
19
  'add form' => array(
20
    'views_content_views_row_content_type_edit_form' => t('Select context'),
21
    'views_content_views_row_edit' => t('Configure rows'),
22
  ),
23
  'edit form' => array(
24
    'views_content_views_row_content_type_edit_form' => t('Select context'),
25
    'views_content_views_row_edit' => t('Configure rows'),
26
  ),
27
);
28

    
29
/**
30
 * Render the node_terms content type.
31
 */
32
function views_content_views_row_content_type_render($subtype, $conf, $panel_args, $context) {
33
  if (empty($context) || empty($context->data)) {
34
    return;
35
  }
36

    
37
  // Build the content type block.
38
  $block          = new stdClass();
39
  $block->module  = 'views_row';
40
  $block->delta   = $context->argument;
41
  $block->title   = '';
42
  $block->content = '';
43

    
44
  // This guarantees the view is rendered normally which must happen.
45
  $view = views_content_context_get_view($context);
46
  $output = views_content_context_get_output($context);
47

    
48
  $sets = array();
49
  $plugin = $view->style_plugin;
50

    
51
  // If all rows have to be displayed then simply get the key of all rows.
52
  $row_indexes = array();
53
  if (empty($conf['rows'])) {
54
    if (is_array($output['rows'])) {
55
      $row_indexes = array_keys($output['rows']);
56
    }
57
  }
58
  else {
59
    // If a subset of rows is requested collect the list of row keys.
60
    foreach ($conf['rows'] as $index) {
61
      $row_indexes[] = $index - 1;
62
    }
63
  }
64

    
65
  if (empty($conf['use_fields']) || empty($plugin->row_plugin)) {
66
    foreach ($row_indexes as $row_index) {
67
      if (isset($output['rows'][$row_index])) {
68
        $sets[$plugin->groups[$row_index]][$row_index] = $output['rows'][$row_index];
69
      }
70
    }
71
  }
72
  else {
73
    // If we're using specific fields, go through and poke the 'exclude' flag.
74
    foreach ($view->field as $id => $field) {
75
      $view->field[$id]->options['exclude'] = empty($conf['fields'][$id]);
76
    }
77

    
78
    // Rerender just the rows we need.
79
    foreach ($row_indexes as $row_index) {
80
      $view->row_index = $row_index;
81
      if (!empty($view->result[$view->row_index])) {
82
        $sets[$plugin->groups[$view->row_index]][$view->row_index] = $plugin->row_plugin->render($view->result[$view->row_index]);
83
      }
84
      unset($view->row_index);
85
    }
86
  }
87

    
88
  foreach ($sets as $title => $rows) {
89
    $block->content .= theme($plugin->theme_functions(),
90
      array(
91
        'view' => $view,
92
        'options' => $plugin->options,
93
        'rows' => $rows,
94
        'title' => $title,
95
      )
96
    );
97
  }
98

    
99
  return $block;
100
}
101

    
102
function views_content_views_row_content_type_edit_form($form, &$form_state) {
103
  // This form does nothing; it exists to let the main form select the view context.
104
  return $form;
105
}
106

    
107
function views_content_views_row_content_type_edit_form_submit($form, &$form_state) {
108
}
109

    
110
function views_content_views_row_edit($form, &$form_state) {
111
  $conf = $form_state['conf'];
112
  $contexts = $form_state['contexts'];
113

    
114
  if (empty($contexts[$conf['context']])) {
115
    $form['markup'] = array('#markup' => '<p>' . t('Invalid context selected.') . '</p>');
116
    return $form;
117
  }
118

    
119
  if (!isset($contexts[$conf['context']]->argument)) {
120
    $name = $contexts[$conf['context']]->placeholder['conf']['name'];
121
    list($plugin, $view_data) = explode(':', $name);
122
    list($view_name, $display_id) = explode('-', $view_data);
123
  }
124
  else {
125
    $view_data = $contexts[$conf['context']]->argument;
126
    list($view_name, $display_id) = explode(':', $view_data);
127
  }
128
  $contexts[$conf['context']]->data['name'] = $view_name;
129
  $contexts[$conf['context']]->data['display'] = $display_id;
130
  $view = views_content_context_get_view($contexts[$conf['context']]);
131
  if (empty($view)) {
132
    $form['markup'] = array('#markup' => '<p>' . t('Context contains an invalid view.') . '</p>');
133
    return $form;
134
  }
135

    
136
  ctools_include('dependent');
137
  $form['limit_rows'] = array(
138
    '#type' => 'checkbox',
139
    '#title' => t('Limit rows'),
140
    '#default_value' => (int) !empty($conf['rows']),
141
  );
142

    
143
  $view->init_pager();
144
  $rows = $view->get_items_per_page();
145

    
146
  if (!empty($rows)) {
147
    foreach (range(1, $rows) as $row) {
148
      $options[$row] = t('Row @number', array('@number' => $row));
149
    }
150
    $form['rows'] = array(
151
      '#type' => 'checkboxes',
152
      '#title' => t('Select rows'),
153
      '#options' => $options,
154
      '#default_value' => $conf['rows'],
155
      '#dependency' => array('edit-limit-rows' => array(TRUE)),
156
    );
157
  }
158
  else {
159
    $form['rows'] = array('#markup' => '<p>' . t('The view must have a maximum number of items set to use this setting.') . '</p>');
160
    return $form;
161
  }
162

    
163
  if ($view->display_handler->uses_fields()) {
164
    $form['use_fields'] = array(
165
      '#type' => 'checkbox',
166
      '#title' => t('Display specific fields'),
167
      '#default_value' => $conf['use_fields'],
168
    );
169

    
170
    $form['fields'] = array(
171
      '#type' => 'checkboxes',
172
      '#options' => $view->display_handler->get_field_labels(),
173
      '#default_value' => $conf['fields'],
174
      '#prefix' => '<div id="edit-fields-wrapper"><div id="edit-fields">',
175
      '#suffix' => '</div></div>',
176
      '#dependency' => array('edit-use-fields' => array(TRUE)),
177
    );
178
  }
179
  return $form;
180
}
181

    
182
function views_content_views_row_edit_validate(&$form, &$form_state) {
183
}
184

    
185
function views_content_views_row_edit_submit(&$form, &$form_state) {
186
  $form_state['conf']['rows'] = array_filter($form_state['values']['rows']);
187
  $form_state['conf']['use_fields'] = $form_state['values']['use_fields'];
188
  $form_state['conf']['fields'] = array_filter($form_state['values']['fields']);
189
}
190

    
191
function views_content_views_row_content_type_admin_info($subtype, $conf, $contexts) {
192
  // Go through this route to make sure we catch changes in configuration
193
  // that can happen.
194
  $plugin = ctools_get_content_type('views_row');
195
  $context = ctools_content_select_context($plugin, $subtype, $conf, $contexts);
196

    
197
  $block = new stdClass();
198
  $block->title = t('Row information');
199

    
200
  if (!empty($conf['use_fields'])) {
201
    $display_fields = array();
202
    $view = views_content_context_get_view($context);
203
    if (empty($view)) {
204
      $block->title = t('Broken view');
205
      return $block;
206
    }
207
    $fields = $view->display_handler->get_field_labels();
208

    
209
    foreach ($conf['fields'] as $field) {
210
      if (!empty($fields[$field])) {
211
        $display_fields[$field] = '"<em>' . check_plain($fields[$field]) . '</em>"';
212
      }
213
    }
214

    
215
    if ($display_fields) {
216
      $block->content = t('Displaying: !fields', array('!fields' => implode(', ', $display_fields)));
217
    }
218
    else {
219
      $block->content = t('Displaying no fields due to misconfiguration.');
220
    }
221
  }
222
  else {
223
    $block->content = t('Displaying the configured row.');
224
  }
225

    
226
  return $block;
227
}
228

    
229
function views_content_views_row_content_type_admin_title($subtype, $conf, $context) {
230
  $rows = array_filter($conf['rows']);
231
  $row_count = count($rows);
232
  $rows = empty($rows) ? t('Show all') : implode(', ', $rows);
233
  return format_plural($row_count,
234
    '"@context" row @rows',
235
    '"@context" rows @rows',
236
    array('@context' => $context->identifier, '@rows' => $rows)
237
  );
238
}