You are here

protected function Table::renderNewValue in Views Aggregator Plus 8

Returns the rendered value for a new (raw) value of a table cell.

Parameters

object $field_handler: The handler associated with the field/table-column being requested.

int $row_num: The result row number.

mixed $new_values: The raw value or array of raw values to render.

string $separator: Separator to use between rendered values, when $new_values is an array.

Return value

mixed The rendered new value or FALSE if the value could not be rendered.

2 calls to Table::renderNewValue()
Table::setCell in src/Plugin/views/style/Table.php
Render and set a raw value on the table cell in specified column and row.
Table::setTotalsRow in src/Plugin/views/style/Table.php
Write the aggregated results back into the View results totals (footer).

File

src/Plugin/views/style/Table.php, line 808

Class

Table
Style plugin to render each item as a row in a table.

Namespace

Drupal\views_aggregator\Plugin\views\style

Code

protected function renderNewValue($field_handler, $row_num, $new_values, $separator) {
  $new_values = is_array($new_values) ? $new_values : [
    $new_values,
  ];

  // If the field_handler belongs to an entity Field (as in the field module),
  // then we call renderFromRaw(), which uses the _relationship_entities array
  // to find, format and replace the supplied value.
  if (isset($field_handler->options['entity_field'])) {
    $field_name = $field_handler->options['entity_field'];
  }
  else {
    $field_name = $field_handler->options['id'];
  }
  $rendered_values = [];
  foreach ($new_values as $new_value) {
    if ($this
      ->isCustomTextField($field_handler)) {
      $custom_formula = $field_handler->options['alter']['text'];

      // Use only the "number_format" at the end of the
      // expression to format the result only.
      if (strrpos($custom_formula, '|number_format(')) {
        $start_pos = strrpos($custom_formula, '|number_format(') - 1;
        $custom_format = preg_match('/number_format\\((.*)\\)/', substr($custom_formula, $start_pos), $matches, PREG_OFFSET_CAPTURE);
        $custom_delimiters = str_getcsv($matches[1][0], ',', "'");

        // Check if arguments are set, otherwise use dot for default decimal.
        $rendered_values[] = number_format($new_value, isset($custom_delimiters[0]) ? $custom_delimiters[0] : 0, isset($custom_delimiters[1]) ? $custom_delimiters[1] : '.', isset($custom_delimiters[2]) ? $custom_delimiters[2] : '');
      }
      else {
        $rendered_values[] = $new_value;
      }
    }
    elseif ($this
      ->isCommerceField($field_handler)) {

      // Check which function is set on the field for group aggregation.
      $aggr_operation = substr(reset($this->options['info'][$field_handler->options['id']]['aggr']), 17);
      $aggr = $this->options['info'][$field_handler->options['id']]['has_aggr'];
      $col_aggr = $this->options['info'][$field_handler->options['id']]['has_aggr_column'];
      $col_operation = substr($this->options['info'][$field_handler->options['id']]['aggr_column'], 17);
      $operations = [
        'sum',
        'average',
        'median',
        'maximum',
        'minimum',
        'range',
      ];

      // The value of attributes is stored differently.
      if (substr($field_name, 0, 10) === 'attribute_') {
        $rendered_values[] = $new_value;
      }
      elseif (in_array($aggr_operation, $operations) && $aggr == 1) {
        $rendered_values[] = $this
          ->renderFromRaw($field_handler, $row_num, $new_value);
      }
      elseif (in_array($col_operation, $operations) && $col_aggr == 1) {
        $rendered_values[] = $this
          ->renderFromRaw($field_handler, $row_num, $new_value);
      }
      else {
        $rendered_values[] = $new_value;
      }
    }
    elseif ($this
      ->isStandardField($field_handler)) {
      $rendered_values[] = $this
        ->renderFromRaw($field_handler, $row_num, $new_value);
    }
    elseif ($this
      ->isViewsFieldView($field_handler)) {
      $rendered_values[] = $new_value;
    }
    elseif ($this
      ->isWebformNumeric($field_handler)) {
      $separator = $this->options['info'][$field_name]['separator'];
      $rendered_values[] = $this
        ->renderNewWebformValue($field_handler, $row_num, $new_value, $separator);
    }
    else {
      $rendered_values[] = $new_value;
    }
  }
  $rendered_value = implode(empty($separator) ? ' - ' : $separator, $rendered_values);
  return is_array($rendered_value) ? $this
    ->getRenderer()
    ->render($rendered_value) : $rendered_value;
}