You are here

public function CumulativeField::getValue in Views Cumulative Field 8

Gets the value that's supposed to be rendered.

This api exists so that other modules can easy set the values of the field without having the need to change the render method as well.

Parameters

\Drupal\views\ResultRow $values: An object containing all retrieved values.

string $field: Optional name of the field where the value is stored.

Overrides FieldPluginBase::getValue

File

src/Plugin/views/field/CumulativeField.php, line 166
Defines Drupal\views_cumulative_field\Plugin\views\field\CumulativeField.

Class

CumulativeField
Field handler to flag the node type.

Namespace

Drupal\views_cumulative_field\Plugin\views\field

Code

public function getValue(ResultRow $values, $field = NULL) {
  parent::getValue($values, $field);

  // The field selected in the options form.
  $field = $this->options['data_field'];

  // Determine what type of field is being used.
  $field_type = $this
    ->getFieldType($field);

  // If the field is rewritten, get the rewritten text. Else, returns null.
  $rewritten = $this
    ->getRewriteStatus($field);

  // Start $data off at zero.
  $data = 0;

  // Process if not undefined.
  if ($field_type !== 'undefined') {

    // Get the value of a field that comes from a relationship.
    $relationship = $this
      ->getFieldRelationship($field);
    if ($relationship) {

      // Use the relationship's entity to fetch the field value.
      $entity = $this
        ->getRelationshipEntity($values, $field, $relationship);
    }
    else {

      // We know this is an entity-based View, so define the base entity.
      $entity = $values->_entity;
    }

    // The next two statements handle fields with or without a relationship.
    if (isset($entity) && $rewritten) {

      // If already numeric, there is no need for advancedRender().
      if (is_numeric($rewritten) == TRUE) {
        $data = $rewritten;
      }
      else {
        $data = current($this->view->field[$field]
          ->advancedRender($values));
      }
    }
    if (isset($entity) && !$rewritten) {
      $field_base = $this->displayHandler
        ->getHandler('field', $field)->field;
      if ($entity
        ->hasField($field_base)) {
        $data = $entity
          ->get($field_base)
          ->getValue()[0]['value'];
      }

      // For Commerce fields.
      if ($field_type === 'commerce_price_default' || $field_type === 'commerce_product_variation') {
        $commerce_field_id = $this->displayHandler
          ->getHandler('field', $field)->options['id'];
        if ($commerce_field_id === 'list_price__number') {
          $data = $entity
            ->get('list_price')
            ->getValue()[0]['number'];
        }
        if ($commerce_field_id === 'price__number') {
          $data = $entity
            ->get('price')
            ->getValue()[0]['number'];
        }
      }
    }
  }
  else {
    $data = $this->view->field[$field]
      ->getValue($values);
    if ($rewritten) {
      $data = current($this->displayHandler
        ->getHandler('field', $field)
        ->advancedRender($values));
    }
  }

  // Add the value of the current with the previous value.
  $this->additional_fields['cumulative_field_data'] = $data + $this->additional_fields['cumulative_field_data'];

  // The resulting value.
  $value = $this->additional_fields['cumulative_field_data'];

  // A control for modules that modify the row count, such as Charts.
  $this->counter++;
  if ($this->view
    ->getItemsPerPage() > 0) {
    if ($this->counter == $this->view
      ->getItemsPerPage()) {
      $this->additional_fields['cumulative_field_data'] = 0;
    }
  }
  else {
    if ($this->counter == $this->view->total_rows) {
      $this->additional_fields['cumulative_field_data'] = 0;
    }
  }
  return $value;
}