StylePluginBase.php 22.9 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\views\Plugin\views\style\StylePluginBase.
merlinofchaos's avatar
merlinofchaos committed
6 7
 */

8
namespace Drupal\views\Plugin\views\style;
9

10
use Drupal\Core\Form\FormStateInterface;
11
use Drupal\views\Plugin\views\PluginBase;
12
use Drupal\views\Plugin\views\display\DisplayPluginBase;
13
use Drupal\views\Plugin\views\wizard\WizardInterface;
14
use Drupal\views\ViewExecutable;
15

merlinofchaos's avatar
merlinofchaos committed
16 17 18
/**
 * @defgroup views_style_plugins Views style plugins
 * @{
19
 * Plugins that control how the collection of results is rendered in a view.
merlinofchaos's avatar
merlinofchaos committed
20
 *
21
 * Style plugins control how a view is displayed. For the most part, they are
22 23 24 25 26 27 28 29 30 31
 * object wrappers around theme templates. Examples of styles include HTML
 * lists, tables, full or teaser content views, etc.
 *
 * Many (but not all) style plugins have an optional row plugin, which
 * displays a single record. Not all style plugins use row plugins, so it is
 * up to the style plugin to set this up and call the row plugin. See the
 * @link views_row_plugins Views row plugins topic @endlink for more
 * information.
 *
 * Style plugins extend \Drupal\views\Plugin\views\style\StylePluginBase. They
32
 * must be annotated with \Drupal\views\Annotation\ViewsStyle
33 34 35 36
 * annotation, and they must be in namespace directory Plugin\views\style.
 *
 * @ingroup views_plugins
 * @see plugin_api
merlinofchaos's avatar
merlinofchaos committed
37 38 39
 */

/**
40
 * Base class for views style plugins.
merlinofchaos's avatar
merlinofchaos committed
41
 */
42
abstract class StylePluginBase extends PluginBase {
43

44 45 46
  /**
   * Overrides Drupal\views\Plugin\Plugin::$usesOptions.
   */
47
  protected $usesOptions = TRUE;
48

merlinofchaos's avatar
merlinofchaos committed
49 50 51
  /**
   * Store all available tokens row rows.
   */
52
  protected $rowTokens = array();
merlinofchaos's avatar
merlinofchaos committed
53

54 55 56 57 58
  /**
   * Does the style plugin allows to use style plugins.
   *
   * @var bool
   */
59
  protected $usesRowPlugin = FALSE;
60 61 62 63 64 65

  /**
   * Does the style plugin support custom css class for the rows.
   *
   * @var bool
   */
66
  protected $usesRowClass = FALSE;
67

68 69 70 71 72 73 74
  /**
   * Does the style plugin support grouping of rows.
   *
   * @var bool
   */
  protected $usesGrouping = TRUE;

75 76 77 78 79 80 81 82
  /**
   * Does the style plugin for itself support to add fields to it's output.
   *
   * This option only makes sense on style plugins without row plugins, like
   * for example table.
   *
   * @var bool
   */
83
  protected $usesFields = FALSE;
84

85
  /**
86 87 88 89 90 91 92
   * Stores the rendered field values, keyed by the row index and field name.
   *
   * @see \Drupal\views\Plugin\views\style\StylePluginBase::renderFields()
   * @see \Drupal\views\Plugin\views\style\StylePluginBase::getField()
   *
   * @var array|null
   */
93 94
  protected $rendered_fields;

95 96 97 98 99 100 101 102
  /**
   * The theme function used to render the grouping set.
   *
   * Plugins may override this attribute if they wish to use some other theme
   * function to render the grouping set.
   *
   * @var string
   *
103
   * @see StylePluginBase::renderGroupingSets()
104 105 106
   */
  protected $groupingTheme = 'views_view_grouping';

107 108 109 110 111 112 113
  /**
   * Should field labels be enabled by default.
   *
   * @var bool
   */
  protected $defaultFieldLabels = FALSE;

merlinofchaos's avatar
merlinofchaos committed
114
  /**
115
   * Overrides \Drupal\views\Plugin\views\PluginBase::init().
merlinofchaos's avatar
merlinofchaos committed
116
   *
117 118
   * The style options might come externally as the style can be sourced from at
   * least two locations. If it's not included, look on the display.
merlinofchaos's avatar
merlinofchaos committed
119
   */
120 121
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);
merlinofchaos's avatar
merlinofchaos committed
122

123
    if ($this->usesRowPlugin() && $display->getOption('row')) {
124
      $this->view->rowPlugin = $display->getPlugin('row');
merlinofchaos's avatar
merlinofchaos committed
125 126 127 128 129 130 131 132
    }

    $this->options += array(
      'grouping' => array(),
    );

  }

133
  public function destroy() {
merlinofchaos's avatar
merlinofchaos committed
134 135
    parent::destroy();

136 137
    if (isset($this->view->rowPlugin)) {
      $this->view->rowPlugin->destroy();
merlinofchaos's avatar
merlinofchaos committed
138 139 140 141
    }
  }

  /**
142 143 144
   * Returns the usesRowPlugin property.
   *
   * @return bool
merlinofchaos's avatar
merlinofchaos committed
145
   */
146 147 148
  function usesRowPlugin() {
    return $this->usesRowPlugin;

merlinofchaos's avatar
merlinofchaos committed
149 150 151
  }

  /**
152 153 154
   * Returns the usesRowClass property.
   *
   * @return bool
merlinofchaos's avatar
merlinofchaos committed
155
   */
156 157
  function usesRowClass() {
    return $this->usesRowClass;
merlinofchaos's avatar
merlinofchaos committed
158 159
  }

160 161 162 163 164 165 166 167 168
  /**
   * Returns the usesGrouping property.
   *
   * @return bool
   */
  function usesGrouping() {
    return $this->usesGrouping;
  }

merlinofchaos's avatar
merlinofchaos committed
169 170 171 172 173
  /**
   * Return TRUE if this style also uses fields.
   *
   * @return bool
   */
174
  function usesFields() {
merlinofchaos's avatar
merlinofchaos committed
175 176 177
    // If we use a row plugin, ask the row plugin. Chances are, we don't
    // care, it does.
    $row_uses_fields = FALSE;
178 179
    if ($this->usesRowPlugin() && ($row_plugin = $this->displayHandler->getPlugin('row'))) {
      $row_uses_fields = $row_plugin->usesFields();
merlinofchaos's avatar
merlinofchaos committed
180 181
    }
    // Otherwise, check the definition or the option.
182
    return $row_uses_fields || $this->usesFields || !empty($this->options['uses_fields']);
merlinofchaos's avatar
merlinofchaos committed
183 184 185 186 187 188 189
  }

  /**
   * Return TRUE if this style uses tokens.
   *
   * Used to ensure we don't fetch tokens when not needed for performance.
   */
190
  public function usesTokens() {
191
    if ($this->usesRowClass()) {
merlinofchaos's avatar
merlinofchaos committed
192 193 194 195 196 197 198
      $class = $this->options['row_class'];
      if (strpos($class, '[') !== FALSE || strpos($class, '!') !== FALSE || strpos($class, '%') !== FALSE) {
        return TRUE;
      }
    }
  }

199 200 201 202 203 204 205 206 207
  /**
   * Return TRUE if this style enables field labels by default.
   *
   * @return bool
   */
  public function defaultFieldLabels() {
    return $this->defaultFieldLabels;
  }

merlinofchaos's avatar
merlinofchaos committed
208 209 210
  /**
   * Return the token replaced row class for the specified row.
   */
211
  public function getRowClass($row_index) {
212
    if ($this->usesRowClass()) {
merlinofchaos's avatar
merlinofchaos committed
213
      $class = $this->options['row_class'];
214
      if ($this->usesFields() && $this->view->field) {
215
        $class = strip_tags($this->tokenizeValue($class, $row_index));
merlinofchaos's avatar
merlinofchaos committed
216 217 218 219 220 221 222 223 224 225 226 227 228
      }

      $classes = explode(' ', $class);
      foreach ($classes as &$class) {
        $class = drupal_clean_css_identifier($class);
      }
      return implode(' ', $classes);
    }
  }

  /**
   * Take a value and apply token replacement logic to it.
   */
229
  public function tokenizeValue($value, $row_index) {
merlinofchaos's avatar
merlinofchaos committed
230 231
    if (strpos($value, '[') !== FALSE || strpos($value, '!') !== FALSE || strpos($value, '%') !== FALSE) {
      // Row tokens might be empty, for example for node row style.
232
      $tokens = isset($this->rowTokens[$row_index]) ? $this->rowTokens[$row_index] : array();
merlinofchaos's avatar
merlinofchaos committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
      if (!empty($this->view->build_info['substitutions'])) {
        $tokens += $this->view->build_info['substitutions'];
      }

      if ($tokens) {
        $value = strtr($value, $tokens);
      }
    }

    return $value;
  }

  /**
   * Should the output of the style plugin be rendered even if it's a empty view.
   */
248
  public function evenEmpty() {
merlinofchaos's avatar
merlinofchaos committed
249 250 251
    return !empty($this->definition['even empty']);
  }

252 253
  protected function defineOptions() {
    $options = parent::defineOptions();
merlinofchaos's avatar
merlinofchaos committed
254
    $options['grouping'] = array('default' => array());
255
    if ($this->usesRowClass()) {
merlinofchaos's avatar
merlinofchaos committed
256 257 258 259 260 261 262 263
      $options['row_class'] = array('default' => '');
      $options['default_row_class'] = array('default' => TRUE, 'bool' => TRUE);
    }
    $options['uses_fields'] = array('default' => FALSE, 'bool' => TRUE);

    return $options;
  }

264
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
265
    parent::buildOptionsForm($form, $form_state);
merlinofchaos's avatar
merlinofchaos committed
266
    // Only fields-based views can handle grouping.  Style plugins can also exclude
267 268 269 270
    // themselves from being groupable by setting their "usesGrouping" property
    // to FALSE.
    // @TODO: Document "usesGrouping" in docs.php when docs.php is written.
    if ($this->usesFields() && $this->usesGrouping()) {
merlinofchaos's avatar
merlinofchaos committed
271
      $options = array('' => t('- None -'));
272
      $field_labels = $this->displayHandler->getFieldLabels(TRUE);
merlinofchaos's avatar
merlinofchaos committed
273 274 275
      $options += $field_labels;
      // If there are no fields, we can't group on them.
      if (count($options) > 1) {
276 277
        // This is for backward compatibility, when there was just a single
        // select form.
merlinofchaos's avatar
merlinofchaos committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
        if (is_string($this->options['grouping'])) {
          $grouping = $this->options['grouping'];
          $this->options['grouping'] = array();
          $this->options['grouping'][0]['field'] = $grouping;
        }
        if (isset($this->options['group_rendered']) && is_string($this->options['group_rendered'])) {
          $this->options['grouping'][0]['rendered'] = $this->options['group_rendered'];
          unset($this->options['group_rendered']);
        }

        $c = count($this->options['grouping']);
        // Add a form for every grouping, plus one.
        for ($i = 0; $i <= $c; $i++) {
          $grouping = !empty($this->options['grouping'][$i]) ? $this->options['grouping'][$i] : array();
          $grouping += array('field' => '', 'rendered' => TRUE, 'rendered_strip' => FALSE);
          $form['grouping'][$i]['field'] = array(
            '#type' => 'select',
            '#title' => t('Grouping field Nr.@number', array('@number' => $i + 1)),
            '#options' => $options,
            '#default_value' => $grouping['field'],
            '#description' => t('You may optionally specify a field by which to group the records. Leave blank to not group.'),
          );
          $form['grouping'][$i]['rendered'] = array(
            '#type' => 'checkbox',
            '#title' => t('Use rendered output to group rows'),
            '#default_value' => $grouping['rendered'],
            '#description' => t('If enabled the rendered output of the grouping field is used to group the rows.'),
305 306 307 308 309
            '#states' => array(
              'invisible' => array(
                ':input[name="style_options[grouping][' . $i . '][field]"]' => array('value' => ''),
              ),
            ),
merlinofchaos's avatar
merlinofchaos committed
310 311 312 313 314
          );
          $form['grouping'][$i]['rendered_strip'] = array(
            '#type' => 'checkbox',
            '#title' => t('Remove tags from rendered output'),
            '#default_value' => $grouping['rendered_strip'],
315 316 317 318 319
            '#states' => array(
              'invisible' => array(
                ':input[name="style_options[grouping][' . $i . '][field]"]' => array('value' => ''),
              ),
            ),
merlinofchaos's avatar
merlinofchaos committed
320 321 322 323 324
          );
        }
      }
    }

325
    if ($this->usesRowClass()) {
merlinofchaos's avatar
merlinofchaos committed
326 327 328 329 330 331 332
      $form['row_class'] = array(
        '#title' => t('Row class'),
        '#description' => t('The class to provide on each row.'),
        '#type' => 'textfield',
        '#default_value' => $this->options['row_class'],
      );

333
      if ($this->usesFields()) {
merlinofchaos's avatar
merlinofchaos committed
334 335 336 337 338 339 340 341 342 343 344
        $form['row_class']['#description'] .= ' ' . t('You may use field tokens from as per the "Replacement patterns" used in "Rewrite the output of this field" for all fields.');
      }

      $form['default_row_class'] = array(
        '#title' => t('Add views row classes'),
        '#description' => t('Add the default row classes like views-row-1 to the output. You can use this to quickly reduce the amount of markup the view provides by default, at the cost of making it more difficult to apply CSS.'),
        '#type' => 'checkbox',
        '#default_value' => $this->options['default_row_class'],
      );
    }

345
    if (!$this->usesFields() || !empty($this->options['uses_fields'])) {
merlinofchaos's avatar
merlinofchaos committed
346 347 348 349 350 351 352 353 354
      $form['uses_fields'] = array(
        '#type' => 'checkbox',
        '#title' => t('Force using fields'),
        '#description' => t('If neither the row nor the style plugin supports fields, this field allows to enable them, so you can for example use groupby.'),
        '#default_value' => $this->options['uses_fields'],
      );
    }
  }

355
  public function validateOptionsForm(&$form, FormStateInterface $form_state) {
merlinofchaos's avatar
merlinofchaos committed
356
    // Don't run validation on style plugins without the grouping setting.
357
    if ($form_state->hasValue(array('style_options', 'grouping'))) {
merlinofchaos's avatar
merlinofchaos committed
358
      // Don't save grouping if no field is specified.
359 360
      $groupings = $form_state->getValue(array('style_options', 'grouping'));
      foreach ($groupings as $index => $grouping) {
merlinofchaos's avatar
merlinofchaos committed
361
        if (empty($grouping['field'])) {
362
          $form_state->unsetValue(array('style_options', 'grouping', $index));
merlinofchaos's avatar
merlinofchaos committed
363 364 365 366 367
        }
      }
    }
  }

368 369 370 371 372
  /**
   * Provide a form in the views wizard if this style is selected.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
373 374
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
375 376 377
   * @param string $type
   *    The display type, either block or page.
   */
378
  public function wizardForm(&$form, FormStateInterface $form_state, $type) {
379 380 381 382 383 384 385
  }

  /**
   * Alter the options of a display before they are added to the view.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
386 387
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
388
   * @param \Drupal\views\Plugin\views\wizard\WizardInterface $wizard
389 390 391 392 393 394 395
   *   The current used wizard.
   * @param array $display_options
   *   The options which will be used on the view. The style plugin should
   *   alter this to its own needs.
   * @param string $display_type
   *   The display type, either block or page.
   */
396
  public function wizardSubmit(&$form, FormStateInterface $form_state, WizardInterface $wizard, &$display_options, $display_type) {
397 398
  }

merlinofchaos's avatar
merlinofchaos committed
399 400 401 402 403
  /**
   * Called by the view builder to see if this style handler wants to
   * interfere with the sorts. If so it should build; if it returns
   * any non-TRUE value, normal sorting will NOT be added to the query.
   */
404
  public function buildSort() { return TRUE; }
merlinofchaos's avatar
merlinofchaos committed
405 406 407 408 409

  /**
   * Called by the view builder to let the style build a second set of
   * sorts that will come after any other sorts in the view.
   */
410
  public function buildSortPost() { }
merlinofchaos's avatar
merlinofchaos committed
411 412 413 414 415 416 417

  /**
   * Allow the style to do stuff before each row is rendered.
   *
   * @param $result
   *   The full array of results from the query.
   */
418
  public function preRender($result) {
419
    if (!empty($this->view->rowPlugin)) {
420
      $this->view->rowPlugin->preRender($result);
merlinofchaos's avatar
merlinofchaos committed
421 422 423
    }
  }

424
  /**
425 426 427 428
   * Renders a group of rows of the grouped view.
   *
   * @param array $rows
   *   The result rows rendered in this group.
429 430
   *
   * @return array
431
   *   The render array containing the single group theme output.
432
   */
433
  protected function renderRowGroup(array $rows = array()) {
434 435 436 437 438 439 440
    return array(
      '#theme' => $this->themeFunctions(),
      '#view' => $this->view,
      '#rows' => $rows,
    );
  }

merlinofchaos's avatar
merlinofchaos committed
441 442 443
  /**
   * Render the display in this style.
   */
444
  public function render() {
445
    if ($this->usesRowPlugin() && empty($this->view->rowPlugin)) {
446
      debug('Drupal\views\Plugin\views\style\StylePluginBase: Missing row plugin');
merlinofchaos's avatar
merlinofchaos committed
447 448 449 450
      return;
    }

    // Group the rows according to the grouping instructions, if specified.
451
    $sets = $this->renderGrouping(
merlinofchaos's avatar
merlinofchaos committed
452 453 454 455 456
      $this->view->result,
      $this->options['grouping'],
      TRUE
    );

457
    return $this->renderGroupingSets($sets);
merlinofchaos's avatar
merlinofchaos committed
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
  }

  /**
   * Render the grouping sets.
   *
   * Plugins may override this method if they wish some other way of handling
   * grouping.
   *
   * @param $sets
   *   Array containing the grouping sets to render.
   * @param $level
   *   Integer indicating the hierarchical level of the grouping.
   *
   * @return string
   *   Rendered output of given grouping sets.
   */
474
  public function renderGroupingSets($sets, $level = 0) {
475
    $output = array();
476
    $theme_functions = $this->view->buildThemeFunctions($this->groupingTheme);
merlinofchaos's avatar
merlinofchaos committed
477 478 479 480
    foreach ($sets as $set) {
      $row = reset($set['rows']);
      // Render as a grouping set.
      if (is_array($row) && isset($row['group'])) {
481 482 483 484 485 486 487
        $output[] = array(
          '#theme' => $theme_functions,
          '#view' => $this->view,
          '#grouping' => $this->options['grouping'][$level],
          '#grouping_level' => $level,
          '#rows' => $set['rows'],
          '#title' => $set['group'],
merlinofchaos's avatar
merlinofchaos committed
488 489 490 491
        );
      }
      // Render as a record set.
      else {
492
        if ($this->usesRowPlugin()) {
merlinofchaos's avatar
merlinofchaos committed
493 494
          foreach ($set['rows'] as $index => $row) {
            $this->view->row_index = $index;
495
            $set['rows'][$index] = $this->view->rowPlugin->render($row);
merlinofchaos's avatar
merlinofchaos committed
496 497 498
          }
        }

499
        $single_output = $this->renderRowGroup($set['rows']);
500 501 502
        $single_output['#grouping_level'] = $level;
        $single_output['#title'] = $set['group'];
        $output[] = $single_output;
merlinofchaos's avatar
merlinofchaos committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
      }
    }
    unset($this->view->row_index);
    return $output;
  }

  /**
   * Group records as needed for rendering.
   *
   * @param $records
   *   An array of records from the view to group.
   * @param $groupings
   *   An array of grouping instructions on which fields to group. If empty, the
   *   result set will be given a single group with an empty string as a label.
   * @param $group_rendered
   *   Boolean value whether to use the rendered or the raw field value for
   *   grouping. If set to NULL the return is structured as before
   *   Views 7.x-3.0-rc2. After Views 7.x-3.0 this boolean is only used if
   *   $groupings is an old-style string or if the rendered option is missing
   *   for a grouping instruction.
   * @return
   *   The grouped record set.
   *   A nested set structure is generated if multiple grouping fields are used.
   *
   *   @code
   *   array(
   *     'grouping_field_1:grouping_1' => array(
   *       'group' => 'grouping_field_1:content_1',
   *       'rows' => array(
   *         'grouping_field_2:grouping_a' => array(
   *           'group' => 'grouping_field_2:content_a',
   *           'rows' => array(
   *             $row_index_1 => $row_1,
   *             $row_index_2 => $row_2,
   *             // ...
   *           )
   *         ),
   *       ),
   *     ),
   *     'grouping_field_1:grouping_2' => array(
   *       // ...
   *     ),
   *   )
   *   @endcode
   */
548
  public function renderGrouping($records, $groupings = array(), $group_rendered = NULL) {
549 550
    // This is for backward compatibility, when $groupings was a string
    // containing the ID of a single field.
merlinofchaos's avatar
merlinofchaos committed
551 552 553 554 555 556
    if (is_string($groupings)) {
      $rendered = $group_rendered === NULL ? TRUE : $group_rendered;
      $groupings = array(array('field' => $groupings, 'rendered' => $rendered));
    }

    // Make sure fields are rendered
557
    $this->renderFields($this->view->result);
merlinofchaos's avatar
merlinofchaos committed
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
    $sets = array();
    if ($groupings) {
      foreach ($records as $index => $row) {
        // Iterate through configured grouping fields to determine the
        // hierarchically positioned set where the current row belongs to.
        // While iterating, parent groups, that do not exist yet, are added.
        $set = &$sets;
        foreach ($groupings as $info) {
          $field = $info['field'];
          $rendered = isset($info['rendered']) ? $info['rendered'] : $group_rendered;
          $rendered_strip = isset($info['rendered_strip']) ? $info['rendered_strip'] : FALSE;
          $grouping = '';
          $group_content = '';
          // Group on the rendered version of the field, not the raw.  That way,
          // we can control any special formatting of the grouping field through
          // the admin or theme layer or anywhere else we'd like.
          if (isset($this->view->field[$field])) {
575
            $group_content = $this->getField($index, $field);
merlinofchaos's avatar
merlinofchaos committed
576 577 578 579 580 581 582 583 584 585
            if ($this->view->field[$field]->options['label']) {
              $group_content = $this->view->field[$field]->options['label'] . ': ' . $group_content;
            }
            if ($rendered) {
              $grouping = $group_content;
              if ($rendered_strip) {
                $group_content = $grouping = strip_tags(htmlspecialchars_decode($group_content));
              }
            }
            else {
586
              $grouping = $this->getFieldValue($index, $field);
merlinofchaos's avatar
merlinofchaos committed
587 588 589
              // Not all field handlers return a scalar value,
              // e.g. views_handler_field_field.
              if (!is_scalar($grouping)) {
590
                $grouping = hash('sha256', serialize($grouping));
merlinofchaos's avatar
merlinofchaos committed
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
              }
            }
          }

          // Create the group if it does not exist yet.
          if (empty($set[$grouping])) {
            $set[$grouping]['group'] = $group_content;
            $set[$grouping]['rows'] = array();
          }

          // Move the set reference into the row set of the group we just determined.
          $set = &$set[$grouping]['rows'];
        }
        // Add the row to the hierarchically positioned row set we just determined.
        $set[$index] = $row;
      }
    }
    else {
      // Create a single group with an empty grouping field.
      $sets[''] = array(
        'group' => '',
        'rows' => $records,
      );
    }

    // If this parameter isn't explicitely set modify the output to be fully
    // backward compatible to code before Views 7.x-3.0-rc2.
    // @TODO Remove this as soon as possible e.g. October 2020
    if ($group_rendered === NULL) {
      $old_style_sets = array();
      foreach ($sets as $group) {
        $old_style_sets[$group['group']] = $group['rows'];
      }
      $sets = $old_style_sets;
    }

    return $sets;
  }

  /**
631
   * Renders all of the fields for a given style and store them on the object.
merlinofchaos's avatar
merlinofchaos committed
632
   *
633
   * @param array $result
merlinofchaos's avatar
merlinofchaos committed
634 635
   *   The result array from $view->result
   */
636
  protected function renderFields(array $result) {
637
    if (!$this->usesFields()) {
merlinofchaos's avatar
merlinofchaos committed
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
      return;
    }

    if (!isset($this->rendered_fields)) {
      $this->rendered_fields = array();
      $this->view->row_index = 0;
      $keys = array_keys($this->view->field);

      // If all fields have a field::access FALSE there might be no fields, so
      // there is no reason to execute this code.
      if (!empty($keys)) {
        foreach ($result as $count => $row) {
          $this->view->row_index = $count;
          foreach ($keys as $id) {
            $this->rendered_fields[$count][$id] = $this->view->field[$id]->theme($row);
          }

655
          $this->rowTokens[$count] = $this->view->field[$id]->getRenderTokens(array());
merlinofchaos's avatar
merlinofchaos committed
656 657 658 659 660 661 662
        }
      }
      unset($this->view->row_index);
    }
  }

  /**
663
   * Gets a rendered field.
merlinofchaos's avatar
merlinofchaos committed
664
   *
665
   * @param int $index
merlinofchaos's avatar
merlinofchaos committed
666
   *   The index count of the row.
667 668 669 670 671
   * @param string $field
   *   The ID of the field.
   *
   * @return string|null
   *   The output of the field, or NULL if it was empty.
merlinofchaos's avatar
merlinofchaos committed
672
   */
673
  public function getField($index, $field) {
merlinofchaos's avatar
merlinofchaos committed
674
    if (!isset($this->rendered_fields)) {
675
      $this->renderFields($this->view->result);
merlinofchaos's avatar
merlinofchaos committed
676 677 678 679 680 681 682 683
    }

    if (isset($this->rendered_fields[$index][$field])) {
      return $this->rendered_fields[$index][$field];
    }
  }

  /**
684 685 686 687 688 689 690
   * Get the raw field value.
   *
   * @param $index
   *   The index count of the row.
   * @param $field
   *    The id of the field.
   */
691
  protected function getFieldValue($index, $field) {
merlinofchaos's avatar
merlinofchaos committed
692
    $this->view->row_index = $index;
693
    $value = $this->view->field[$field]->getValue($this->view->result[$index]);
merlinofchaos's avatar
merlinofchaos committed
694 695 696 697
    unset($this->view->row_index);
    return $value;
  }

698
  public function validate() {
merlinofchaos's avatar
merlinofchaos committed
699 700
    $errors = parent::validate();

701
    if ($this->usesRowPlugin()) {
702
      $plugin = $this->displayHandler->getPlugin('row');
merlinofchaos's avatar
merlinofchaos committed
703 704 705 706 707 708 709 710 711 712 713 714 715
      if (empty($plugin)) {
        $errors[] = t('Style @style requires a row style but the row plugin is invalid.', array('@style' => $this->definition['title']));
      }
      else {
        $result = $plugin->validate();
        if (!empty($result) && is_array($result)) {
          $errors = array_merge($errors, $result);
        }
      }
    }
    return $errors;
  }

716
  public function query() {
merlinofchaos's avatar
merlinofchaos committed
717
    parent::query();
718 719
    if (isset($this->view->rowPlugin)) {
      $this->view->rowPlugin->query();
merlinofchaos's avatar
merlinofchaos committed
720 721
    }
  }
722

merlinofchaos's avatar
merlinofchaos committed
723 724 725 726 727
}

/**
 * @}
 */