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

CommentFileSizeAuthor
#10 VAP.JPG26.28 KBmarksmith

Comments

Busybee created an issue. See original summary.

Busybee’s picture

Issue summary: View changes
jordik’s picture

More details would be helpful - e.g. fields snd functions used, settings for the suffix etc.

jordik’s picture

Status: Active » Postponed (maintainer needs more info)
sepa_cleversoft’s picture

I also have the same problem, without suffix in a custom field generated with a views field plugin.

tr’s picture

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

sepa_cleversoft’s picture

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.

tr’s picture

Status: Postponed (maintainer needs more info) » Active
sepa_cleversoft’s picture

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.

marksmith’s picture

StatusFileSize
new26.28 KB

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).

sepa_cleversoft’s picture

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.

But now I'm sure to not be the only one to notice this annoying error. :)

sepa_cleversoft’s picture

I fixed the problem disabling the theme debug. I suspect the problem was on views_aggregator_get_cell or vap_num functions.

views_aggregator_get_cell($field_handler, $num, FALSE) returns not only the string with the value, but also the comment for the debug function.

Disabling the theme debug function in services.yml solves it, but the function(s) can be improved to work with the debug on.

jordik’s picture

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

anybody’s picture

Version: 2.0.1 » 2.0.x-dev
Status: Active » Needs work
johaziel’s picture

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 !

mortona2k’s picture

This issue sounds the same as the related one, I think it's a duplicate: https://www.drupal.org/project/views_aggregator/issues/3472336

The patch in this module seems to fix the calculation and formatting issues: https://www.drupal.org/project/views_aggregator/issues/3482129