Révision 5d12d676
Ajouté par Assos Assos il y a environ 6 ans
drupal7/sites/all/modules/views/plugins/views_plugin_style.inc | ||
---|---|---|
8 | 8 |
/** |
9 | 9 |
* @defgroup views_style_plugins Views style plugins |
10 | 10 |
* @{ |
11 |
* Style plugins control how a view is rendered. For example, they |
|
12 |
* can choose to display a collection of fields, node_view() output,
|
|
13 |
* table output, or any kind of crazy output they want.
|
|
11 |
* Style plugins control how a view is rendered. For example, they can choose to
|
|
12 |
* display a collection of fields, node_view() output, table output, or any kind
|
|
13 |
* of crazy output they want. |
|
14 | 14 |
* |
15 |
* Many style plugins can have an optional 'row' plugin, that displays |
|
16 |
* a single record. Not all style plugins can utilize this, so it is
|
|
17 |
* up to the plugin to set this up and call through to the row plugin.
|
|
15 |
* Many style plugins can have an optional 'row' plugin, that displays a single
|
|
16 |
* record. Not all style plugins can utilize this, so it is up to the plugin to
|
|
17 |
* set this up and call through to the row plugin. |
|
18 | 18 |
* |
19 | 19 |
* @see hook_views_plugins() |
20 | 20 |
*/ |
... | ... | |
23 | 23 |
* Base class to define a style plugin handler. |
24 | 24 |
*/ |
25 | 25 |
class views_plugin_style extends views_plugin { |
26 |
|
|
26 | 27 |
/** |
27 | 28 |
* Store all available tokens row rows. |
28 | 29 |
*/ |
29 |
var $row_tokens = array();
|
|
30 |
public $row_tokens = array();
|
|
30 | 31 |
|
31 | 32 |
/** |
32 |
* Contains the row plugin, if it's initialized |
|
33 |
* and the style itself supports it. |
|
33 |
* The row plugin, if it's initialized and the style itself supports it. |
|
34 | 34 |
* |
35 | 35 |
* @var views_plugin_row |
36 | 36 |
*/ |
37 |
var $row_plugin;
|
|
37 |
public $row_plugin;
|
|
38 | 38 |
|
39 | 39 |
/** |
40 | 40 |
* Initialize a style plugin. |
41 | 41 |
* |
42 |
* @param $view |
|
43 |
* @param $display |
|
44 |
* @param $options |
|
42 |
* @param view $view
|
|
43 |
* @param object $display
|
|
44 |
* @param array $options
|
|
45 | 45 |
* The style options might come externally as the style can be sourced |
46 | 46 |
* from at least two locations. If it's not included, look on the display. |
47 | 47 |
*/ |
48 |
function init(&$view, &$display, $options = NULL) { |
|
48 |
public function init(&$view, &$display, $options = NULL) {
|
|
49 | 49 |
$this->view = &$view; |
50 | 50 |
$this->display = &$display; |
51 | 51 |
|
... | ... | |
65 | 65 |
); |
66 | 66 |
} |
67 | 67 |
|
68 |
function destroy() { |
|
68 |
/** |
|
69 |
* |
|
70 |
*/ |
|
71 |
public function destroy() { |
|
69 | 72 |
parent::destroy(); |
70 | 73 |
|
71 | 74 |
if (isset($this->row_plugin)) { |
... | ... | |
76 | 79 |
/** |
77 | 80 |
* Return TRUE if this style also uses a row plugin. |
78 | 81 |
*/ |
79 |
function uses_row_plugin() { |
|
82 |
public function uses_row_plugin() {
|
|
80 | 83 |
return !empty($this->definition['uses row plugin']); |
81 | 84 |
} |
82 | 85 |
|
83 | 86 |
/** |
84 | 87 |
* Return TRUE if this style also uses a row plugin. |
85 | 88 |
*/ |
86 |
function uses_row_class() { |
|
89 |
public function uses_row_class() {
|
|
87 | 90 |
return !empty($this->definition['uses row class']); |
88 | 91 |
} |
89 | 92 |
|
... | ... | |
92 | 95 |
* |
93 | 96 |
* @return bool |
94 | 97 |
*/ |
95 |
function uses_fields() { |
|
98 |
public function uses_fields() {
|
|
96 | 99 |
// If we use a row plugin, ask the row plugin. Chances are, we don't |
97 | 100 |
// care, it does. |
98 | 101 |
$row_uses_fields = FALSE; |
... | ... | |
108 | 111 |
* |
109 | 112 |
* Used to ensure we don't fetch tokens when not needed for performance. |
110 | 113 |
*/ |
111 |
function uses_tokens() { |
|
114 |
public function uses_tokens() {
|
|
112 | 115 |
if ($this->uses_row_class()) { |
113 | 116 |
$class = $this->options['row_class']; |
114 | 117 |
if (strpos($class, '[') !== FALSE || strpos($class, '!') !== FALSE || strpos($class, '%') !== FALSE) { |
... | ... | |
120 | 123 |
/** |
121 | 124 |
* Return the token replaced row class for the specified row. |
122 | 125 |
*/ |
123 |
function get_row_class($row_index) { |
|
126 |
public function get_row_class($row_index) {
|
|
124 | 127 |
if ($this->uses_row_class()) { |
125 | 128 |
$class = $this->options['row_class']; |
126 | 129 |
|
... | ... | |
129 | 132 |
|
130 | 133 |
// Explode the value by whitespace, this allows the function to handle |
131 | 134 |
// a single class name and multiple class names that are then tokenized. |
132 |
foreach(explode(' ', $class) as $token_class) { |
|
135 |
foreach (explode(' ', $class) as $token_class) {
|
|
133 | 136 |
$classes = array_merge($classes, explode(' ', strip_tags($this->tokenize_value($token_class, $row_index)))); |
134 | 137 |
} |
135 | 138 |
} |
... | ... | |
148 | 151 |
/** |
149 | 152 |
* Take a value and apply token replacement logic to it. |
150 | 153 |
*/ |
151 |
function tokenize_value($value, $row_index) { |
|
154 |
public function tokenize_value($value, $row_index) {
|
|
152 | 155 |
if (strpos($value, '[') !== FALSE || strpos($value, '!') !== FALSE || strpos($value, '%') !== FALSE) { |
153 | 156 |
$fake_item = array( |
154 | 157 |
'alter_text' => TRUE, |
... | ... | |
170 | 173 |
} |
171 | 174 |
|
172 | 175 |
/** |
173 |
* Should the output of the style plugin be rendered even if it's a empty view.
|
|
176 |
* Should the output of the style plugin be rendered even if it's empty.
|
|
174 | 177 |
*/ |
175 |
function even_empty() { |
|
178 |
public function even_empty() {
|
|
176 | 179 |
return !empty($this->definition['even empty']); |
177 | 180 |
} |
178 | 181 |
|
179 |
function option_definition() { |
|
182 |
/** |
|
183 |
* {@inheritdoc} |
|
184 |
*/ |
|
185 |
public function option_definition() { |
|
180 | 186 |
$options = parent::option_definition(); |
181 | 187 |
$options['grouping'] = array('default' => array()); |
182 | 188 |
if ($this->uses_row_class()) { |
... | ... | |
189 | 195 |
return $options; |
190 | 196 |
} |
191 | 197 |
|
192 |
function options_form(&$form, &$form_state) { |
|
198 |
/** |
|
199 |
* {@inheritdoc} |
|
200 |
*/ |
|
201 |
public function options_form(&$form, &$form_state) { |
|
193 | 202 |
parent::options_form($form, $form_state); |
194 |
// Only fields-based views can handle grouping. Style plugins can also exclude
|
|
195 |
// themselves from being groupable by setting their "use grouping" definition
|
|
196 |
// key to FALSE. |
|
197 |
// @TODO: Document "uses grouping" in docs.php when docs.php is written.
|
|
203 |
// Only fields-based views can handle grouping. Style plugins can also
|
|
204 |
// exclude themselves from being groupable by setting their "use grouping"
|
|
205 |
// definition key to FALSE.
|
|
206 |
// @todo Document "uses grouping" in docs.php when docs.php is written.
|
|
198 | 207 |
if ($this->uses_fields() && $this->definition['uses grouping']) { |
199 | 208 |
$options = array('' => t('- None -')); |
200 | 209 |
$field_labels = $this->display->handler->get_field_labels(TRUE); |
201 | 210 |
$options += $field_labels; |
202 | 211 |
// If there are no fields, we can't group on them. |
203 | 212 |
if (count($options) > 1) { |
204 |
// This is for backward compatibility, when there was just a single select form. |
|
213 |
// This is for backward compatibility, when there was just a single |
|
214 |
// select form. |
|
205 | 215 |
if (is_string($this->options['grouping'])) { |
206 | 216 |
$grouping = $this->options['grouping']; |
207 | 217 |
$this->options['grouping'] = array(); |
... | ... | |
282 | 292 |
} |
283 | 293 |
} |
284 | 294 |
|
285 |
function options_validate(&$form, &$form_state) { |
|
295 |
/** |
|
296 |
* {@inheritdoc} |
|
297 |
*/ |
|
298 |
public function options_validate(&$form, &$form_state) { |
|
286 | 299 |
// Don't run validation on style plugins without the grouping setting. |
287 | 300 |
if (isset($form_state['values']['style_options']['grouping'])) { |
288 | 301 |
// Don't save grouping if no field is specified. |
... | ... | |
299 | 312 |
* interfere with the sorts. If so it should build; if it returns |
300 | 313 |
* any non-TRUE value, normal sorting will NOT be added to the query. |
301 | 314 |
*/ |
302 |
function build_sort() { return TRUE; } |
|
315 |
public function build_sort() { |
|
316 |
return TRUE; |
|
317 |
} |
|
303 | 318 |
|
304 | 319 |
/** |
305 | 320 |
* Called by the view builder to let the style build a second set of |
306 | 321 |
* sorts that will come after any other sorts in the view. |
307 | 322 |
*/ |
308 |
function build_sort_post() { } |
|
323 |
public function build_sort_post() { |
|
324 |
} |
|
309 | 325 |
|
310 | 326 |
/** |
311 | 327 |
* Allow the style to do stuff before each row is rendered. |
312 | 328 |
* |
313 |
* @param $result |
|
329 |
* @param array $result
|
|
314 | 330 |
* The full array of results from the query. |
315 | 331 |
*/ |
316 |
function pre_render($result) { |
|
332 |
public function pre_render($result) {
|
|
317 | 333 |
if (!empty($this->row_plugin)) { |
318 | 334 |
$this->row_plugin->pre_render($result); |
319 | 335 |
} |
... | ... | |
322 | 338 |
/** |
323 | 339 |
* Render the display in this style. |
324 | 340 |
*/ |
325 |
function render() { |
|
341 |
public function render() {
|
|
326 | 342 |
if ($this->uses_row_plugin() && empty($this->row_plugin)) { |
327 | 343 |
debug('views_plugin_style_default: Missing row plugin'); |
328 | 344 |
return; |
... | ... | |
344 | 360 |
* Plugins may override this method if they wish some other way of handling |
345 | 361 |
* grouping. |
346 | 362 |
* |
347 |
* @param $sets |
|
363 |
* @param array $sets
|
|
348 | 364 |
* Array containing the grouping sets to render. |
349 |
* @param $level |
|
365 |
* @param int $level
|
|
350 | 366 |
* Integer indicating the hierarchical level of the grouping. |
351 | 367 |
* |
352 | 368 |
* @return string |
353 | 369 |
* Rendered output of given grouping sets. |
354 | 370 |
*/ |
355 |
function render_grouping_sets($sets, $level = 0) { |
|
371 |
public function render_grouping_sets($sets, $level = 0) {
|
|
356 | 372 |
$output = ''; |
357 | 373 |
foreach ($sets as $set) { |
358 | 374 |
$row = reset($set['rows']); |
... | ... | |
393 | 409 |
/** |
394 | 410 |
* Group records as needed for rendering. |
395 | 411 |
* |
396 |
* @param $records |
|
412 |
* @param array $records
|
|
397 | 413 |
* An array of records from the view to group. |
398 |
* @param $groupings |
|
414 |
* @param array $groupings
|
|
399 | 415 |
* An array of grouping instructions on which fields to group. If empty, the |
400 | 416 |
* result set will be given a single group with an empty string as a label. |
401 |
* @param $group_rendered |
|
417 |
* @param bool $group_rendered
|
|
402 | 418 |
* Boolean value whether to use the rendered or the raw field value for |
403 | 419 |
* grouping. If set to NULL the return is structured as before |
404 | 420 |
* Views 7.x-3.0-rc2. After Views 7.x-3.0 this boolean is only used if |
405 | 421 |
* $groupings is an old-style string or if the rendered option is missing |
406 | 422 |
* for a grouping instruction. |
407 |
* @return |
|
423 |
* |
|
424 |
* @return array |
|
408 | 425 |
* The grouped record set. |
409 | 426 |
* A nested set structure is generated if multiple grouping fields are used. |
410 | 427 |
* |
... | ... | |
429 | 446 |
* ) |
430 | 447 |
* @endcode |
431 | 448 |
*/ |
432 |
function render_grouping($records, $groupings = array(), $group_rendered = NULL) { |
|
433 |
// This is for backward compatibility, when $groupings was a string containing
|
|
434 |
// the ID of a single field. |
|
449 |
public function render_grouping($records, $groupings = array(), $group_rendered = NULL) {
|
|
450 |
// This is for backward compatibility, when $groupings was a string |
|
451 |
// containing the ID of a single field.
|
|
435 | 452 |
if (is_string($groupings)) { |
436 | 453 |
$rendered = $group_rendered === NULL ? TRUE : $group_rendered; |
437 | 454 |
$groupings = array(array('field' => $groupings, 'rendered' => $rendered)); |
... | ... | |
482 | 499 |
$set[$grouping]['rows'] = array(); |
483 | 500 |
} |
484 | 501 |
|
485 |
// Move the set reference into the row set of the group we just determined. |
|
502 |
// Move the set reference into the row set of the group we just |
|
503 |
// determined. |
|
486 | 504 |
$set = &$set[$grouping]['rows']; |
487 | 505 |
} |
488 |
// Add the row to the hierarchically positioned row set we just determined. |
|
506 |
// Add the row to the hierarchically positioned row set we just |
|
507 |
// determined. |
|
489 | 508 |
$set[$index] = $row; |
490 | 509 |
} |
491 | 510 |
} |
... | ... | |
499 | 518 |
|
500 | 519 |
// If this parameter isn't explicitly set modify the output to be fully |
501 | 520 |
// backward compatible to code before Views 7.x-3.0-rc2. |
502 |
// @TODO Remove this as soon as possible e.g. October 2020
|
|
521 |
// @todo Remove this as soon as possible e.g. October 2020
|
|
503 | 522 |
if ($group_rendered === NULL) { |
504 | 523 |
$old_style_sets = array(); |
505 | 524 |
foreach ($sets as $group) { |
... | ... | |
514 | 533 |
/** |
515 | 534 |
* Render all of the fields for a given style and store them on the object. |
516 | 535 |
* |
517 |
* @param $result |
|
536 |
* @param array $result
|
|
518 | 537 |
* The result array from $view->result |
519 | 538 |
*/ |
520 |
function render_fields($result) { |
|
539 |
public function render_fields($result) {
|
|
521 | 540 |
if (!$this->uses_fields()) { |
522 | 541 |
return; |
523 | 542 |
} |
... | ... | |
548 | 567 |
/** |
549 | 568 |
* Get a rendered field. |
550 | 569 |
* |
551 |
* @param $index |
|
570 |
* @param int $index
|
|
552 | 571 |
* The index count of the row. |
553 |
* @param $field |
|
572 |
* @param string $field
|
|
554 | 573 |
* The id of the field. |
555 | 574 |
*/ |
556 |
function get_field($index, $field) { |
|
575 |
public function get_field($index, $field) {
|
|
557 | 576 |
if (!isset($this->rendered_fields)) { |
558 | 577 |
$this->render_fields($this->view->result); |
559 | 578 |
} |
... | ... | |
566 | 585 |
/** |
567 | 586 |
* Get the raw field value. |
568 | 587 |
* |
569 |
* @param $index |
|
588 |
* @param int $index
|
|
570 | 589 |
* The index count of the row. |
571 |
* @param $field |
|
590 |
* @param string $field
|
|
572 | 591 |
* The id of the field. |
573 | 592 |
*/ |
574 |
function get_field_value($index, $field) { |
|
593 |
public function get_field_value($index, $field) {
|
|
575 | 594 |
$this->view->row_index = $index; |
576 | 595 |
$value = $this->view->field[$field]->get_value($this->view->result[$index]); |
577 | 596 |
unset($this->view->row_index); |
578 | 597 |
return $value; |
579 | 598 |
} |
580 | 599 |
|
581 |
function validate() { |
|
600 |
/** |
|
601 |
* {@inheritdoc} |
|
602 |
*/ |
|
603 |
public function validate() { |
|
582 | 604 |
$errors = parent::validate(); |
583 | 605 |
|
584 | 606 |
if ($this->uses_row_plugin()) { |
... | ... | |
596 | 618 |
return $errors; |
597 | 619 |
} |
598 | 620 |
|
599 |
function query() { |
|
621 |
/** |
|
622 |
* {@inheritdoc} |
|
623 |
*/ |
|
624 |
public function query() { |
|
600 | 625 |
parent::query(); |
601 | 626 |
if (isset($this->row_plugin)) { |
602 | 627 |
$this->row_plugin->query(); |
603 | 628 |
} |
604 | 629 |
} |
630 |
|
|
605 | 631 |
} |
606 | 632 |
|
607 | 633 |
/** |
Formats disponibles : Unified diff
Weekly update of contrib modules