You are here

public function ComplexGrouping::renderGrouping in Views Complex Grouping 8

Group records as needed for rendering.

Overrides StylePluginBase::renderGrouping

File

src/Plugin/views/style/ComplexGrouping.php, line 133

Class

ComplexGrouping
Class ComplexGrouping.

Namespace

Drupal\views_complex_grouping\Plugin\views\style

Code

public function renderGrouping($records, $groupings = [], $group_rendered = NULL) {

  // This is for backward compatibility, when $groupings was a string
  // containing the ID of a single field.
  if (is_string($groupings)) {
    $rendered = $group_rendered === NULL ? TRUE : $group_rendered;
    $groupings = [
      [
        'field' => $groupings,
        'rendered' => $rendered,
      ],
    ];
  }

  // Make sure fields are rendered.
  $this
    ->renderFields($this->view->result);
  $sets = [];
  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 $level => $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])) {
          $group_content = $this
            ->getField($index, $field);
          if ($this->view->field[$field]->options['label']) {
            $group_content = $this->view->field[$field]->options['label'] . ': ' . $group_content;
          }
          if ($rendered) {
            $grouping = (string) $group_content;
            if ($rendered_strip) {
              $group_content = $grouping = strip_tags(htmlspecialchars_decode($group_content));
            }
          }
          else {
            $grouping = $this
              ->getFieldValue($index, $field);

            // Not all field handlers return a scalar value,
            // e.g. views_handler_field_field.
            if (!is_scalar($grouping)) {
              $grouping = hash('sha256', serialize($grouping));
            }
          }
        }

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

          // Add selected fields for this level.
          foreach ($this->options['grouping'][$level]['complex_grouping']['grouping_fields'] as $field) {
            $set[$grouping]['fields'][$field] = $this->rendered_fields[$index][$field];
          }
        }

        // 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 {

    // If this parameter isn't explicitly 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 = [];
      foreach ($sets as $group) {
        $old_style_sets[$group['group']] = $group['rows'];
      }
      $sets = $old_style_sets;
    }
  }

  // Apply the offset and limit.
  array_walk($sets, [
    $this,
    'complexGroupingRecursiveLimit',
  ]);
  return $sets;
}