When a column contains decimal numbers and a suffix, the SUM is calculated as of integers, omitting decimal points

Created on 14 May 2022, over 2 years ago
Updated 5 September 2024, 2 months ago

Add a field containing decimal values, format it to add a suffix

🐛 Bug report
Status

Needs work

Version

2.0

Component

Code

Created by

🇺🇦Ukraine Busybee

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States tr Cascadia

    @sepa_cleversoft Please provide the additional details requested in #3. Specifically, we need detailed steps to reproduce the error.

  • 🇮🇹Italy sepa_cleversoft

    I have a view custom field, defined with this code in modules/custom/my_module/src/Plugin/views/field

    <?php
     
    /**
     * @file
     * Definition of Drupal\budget_giorni\Plugin\views\field\GiornateEseguite
     */
     
    namespace Drupal\budget_giorni\Plugin\views\field;
     
    use Drupal\views\Plugin\views\field\NumericField ;
    use Drupal\views\ResultRow;
    use Drupal\node\Entity\Node;
    
     
    /**
     * Field handler to flag the node type.
     *
     * @ingroup views_field_handlers
     *
     * @ViewsField("giornate_eseguite")
     */
    class GiornateEseguite extends NumericField {
     
      /**
       * @{inheritdoc}
       */
      public function query() {
        // Leave empty to avoid a query on this field.
      }
     
      /**
       * @{inheritdoc}
       */
      public function render(ResultRow $values) {
        $counter = 0;
        $node = $values->_entity;
        //dpm($node);
        $interventi_array = $node->field_interventi->getValue();
        //dpm($interventi_array);
    
      if (!empty($interventi_array)) {    
          foreach ($interventi_array as $intervento) {
            $intervento_node = Node::load($intervento["target_id"]);
            //dpm($intervento_node);
            if (isset($intervento_node->field_stato_intervento)) {
              $stato_intervento_tid = $intervento_node->field_stato_intervento->getString();
              //dpm($stato_intervento_tid);
              if ($stato_intervento_tid == "99") {
                if (isset($node->field_data_per_calcoli) && $node->field_data_per_calcoli->value) {
                  //dpm($node->field_data_per_calcoli->value);
                  if ($intervento_node->field_data_orario->value >= $node->field_data_per_calcoli->value) {
                    $durata = (float) $intervento_node->field_durata->getString();
                    $counter += $durata;
                  }
                } else {
                  $durata = (float) $intervento_node->field_durata->getString();
                  $counter += $durata;
                }
              }
            }
          }
      }    
        return $counter;
    
      }
    }
    

    The field is shown correctly (or at lest it seems) in view. The problem is that the calculation is done without comma or dot for decimals. So 0,5 becomes 05, and the calculation fails.

  • Status changed to Active almost 2 years ago
  • 🇺🇸United States tr Cascadia
  • 🇮🇹Italy sepa_cleversoft

    Do you need more info to replicate the error?

    I suspect that I don't render the field in the correct way, but I also tried everything and it always ignore the decimal separator, giving wrong result.

    I don't know if it's a general issue, that affects many other cases, or just something I'm doing wrong.

    Let me know if you need something.

  • 🇮🇪Ireland marksmith

    Do you have a rewrite in the field to be summed by VAP? For me the SUM column aggregation function works as expected with regular decimal field with suffix. It does not work, however, if this operation is performed on a twig rewritten custom field, with math calculation that also contains non-numeric values. See both instances in the attached screenshot (D 9.5.3).

  • 🇮🇹Italy sepa_cleversoft

    I have no twig rewrite or other some of rewriting. The field is just attached to a view. The rows are rendered well, the sum is with trimmed values.

  • 🇮🇹Italy sepa_cleversoft

    I fixed the problem disabling the theme debug. Very strange, but I hope this can help somebody else.

  • 🇦🇹Austria jordik

    Did you have a look at 🐛 views_aggregator\Plugin\views\style\Table->getCell assumes any field not provided by views or webform_views should be rendered Needs work ?
    Seems to tackle the same problem with theme debug.
    Please apply the patch and provide feedback whether it solved the problem.

  • Status changed to Needs work 2 months ago
  • 🇩🇪Germany Anybody Porta Westfalica
  • 🇫🇷France johaziel

    I steel have the same probleme on drupal 10.3 with a view_simple_math_field..
    Sum was calculated exactly like the picture...

    For me, it's because $field_handler->view->style_plugin->getCell(..) in views_aggregator_get_cell return also the preffix and suffix of the field but vap_num() need only the value !

    So I changed views_aggregator_get_cell like this

    function views_aggregator_get_cell(FieldHandlerInterface $field_handler, int $row_num, bool $rendered = FALSE): string {
      if(!$rendered){
        if (isset($field_handler->view->result[$row_num])) {
          $row = $field_handler->view->result[$row_num];
          return $field_handler->getValue($row);
        }
      }
      if (isset($field_handler->view->style_plugin)) {
        return $field_handler->view->style_plugin->getCell($field_handler, $row_num, $rendered);
      }
      return '';
    }
    

    And know my sum is good !

Production build 0.71.5 2024