View source
<?php
namespace Drupal\views_cumulative_field\Plugin\views\field;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\NumericField;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CumulativeField extends NumericField {
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManager $entityTypeManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entityTypeManager;
}
private $counter = 0;
public function query() {
$this->additional_fields['cumulative_field_data'] = 0;
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['data_field'] = [
'default' => NULL,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$field_options = $this->displayHandler
->getFieldLabels();
unset($field_options['field_cumulative_field']);
$form['data_field'] = [
'#type' => 'radios',
'#title' => $this
->t('Data Field'),
'#options' => $field_options,
'#default_value' => $this->options['data_field'],
'#description' => $this
->t('Select the field for which to calculate the cumulative value.'),
'#weight' => -10,
];
return $form;
}
protected function getFieldType($field) {
$field_handler = $this->displayHandler
->getHandler('field', $field)->options;
if (!empty($field_handler['type'])) {
$field_type = $field_handler['type'];
}
else {
$field_type = 'undefined';
}
return $field_type;
}
protected function getFieldRelationship($field) {
$field_handler = $this->displayHandler
->getHandler('field', $field)->options;
if (!empty($field_handler['relationship']) && $field_handler['relationship'] !== 'none') {
$relationship = $field_handler['relationship'];
}
else {
$relationship = NULL;
}
return $relationship;
}
protected function getRewriteStatus($field) {
$field_handler = $this->displayHandler
->getHandler('field', $field)->options;
if ($field_handler['alter']['alter_text'] && !empty($field_handler['alter']['text'])) {
$alter = $field_handler['alter']['text'];
}
else {
$alter = NULL;
}
return $alter;
}
protected function getRelationshipEntity($values, $field, $relationship) {
$relationship_entity = NULL;
$relationship_entity_type = $this->displayHandler
->getHandler('field', $field)
->getEntityType();
$relationship_entities = $values->_relationship_entities;
if (isset($relationship_entities[$relationship])) {
$entity_id = $relationship_entities[$relationship]
->id();
$relationship_entity = $this->entityTypeManager
->getStorage($relationship_entity_type)
->load($entity_id);
}
return $relationship_entity;
}
public function getValue(ResultRow $values, $field = NULL) {
parent::getValue($values, $field);
$field = $this->options['data_field'];
$field_type = $this
->getFieldType($field);
$rewritten = $this
->getRewriteStatus($field);
$data = 0;
if ($field_type !== 'undefined') {
$relationship = $this
->getFieldRelationship($field);
if ($relationship) {
$entity = $this
->getRelationshipEntity($values, $field, $relationship);
}
else {
$entity = $values->_entity;
}
if (isset($entity) && $rewritten) {
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'];
}
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));
}
}
$this->additional_fields['cumulative_field_data'] = $data + $this->additional_fields['cumulative_field_data'];
$value = $this->additional_fields['cumulative_field_data'];
$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;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'));
}
}