Score:0

How to sum views field value

pe flag

A have Drupal 8 and a custom module, which adds a custom field to a view. I want to sum two fields in each views row.

In my module I sum two field values:

  public function render(ResultRow $values) {

  $value = $this->view->field['field_one']->original_value;
  $value_two = $this->view->field['field_two']->original_value;
  $sum = $value + $value_two;
  return $sum;

}

but I get a number 2 as a result for all rows instead of real sum. Looks like a count, not a sum.

Why so that?

leymannx avatar
ne flag
What type of data is `$value` and `$value_two`?
beltouche avatar
cn flag
Your function takes in a ResultRow parameter, but doesn't use it. You're getting the same value in every case because you're always calculating the sum from the same two values: `$this->view->field['field_one']->original_value` and `$this->view->field['field_two']->original_value`
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.