Aggregation option don't work with generated datas from custom field handler class

Created on 13 March 2018, almost 7 years ago
Updated 25 April 2023, over 1 year ago

Hello,

I have created a json field to store datas (Json/JsonB field module). For use and extract datas inside a view, I use a custom class to alter the views field handler and extends NumericField. If I use directly the modified data without reprocess data, I get the data.
But, if I want to use these "extracted" datas with an aggregation option (SUM, MAX...), I get 0 or 0.00000 or no results. I haven't error, but the calcul is not processed.

How can I debug/display which datas are called to build the aggregation? Do I must build data in the query() function and not in the render() function of my custom field handler? Or, inside aother function before the render() function?

I think that aggregation use the default value of the field (stored in databse) and not the value generated in the render() function. Can you explain me how to proceed to use aggregation option with this type of data?

Here is the code on my custom field handler:

/**
 * @file
 * Definition of Drupal\mymodule\Plugin\views\field\MyCustomClass
 */
 
namespace Drupal\mymodule\Plugin\views\field;
 
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\NumericField;
use Drupal\views\ResultRow;
 
/**
 * Field handler to show a custom_views_field of the current row.
 *
 * @ingroup views_field_handlers
 *
 * @ViewsField("my_custom_class")
 */
class MyCustomClass extends NumericField {

  /**
   * Retrieve metrics from the api directory
   */
  protected function metrics() {  
	// Build a function with defined metrics
    module_load_include('php', 'mymodule', '/src/Plugin/api/metrics');
    // Return metrics
    return metrics();  
  }
 
  /**
   * {@inheritdoc}
   */
  public function usesGroupBy() {
    // Enable the aggregation option
    return TRUE; 
  }
 
  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    // Load defined metrics
    $metrics = $this->metrics();  
    // Define options
    $options = parent::defineOptions();
    $options['metrics'] = array('default' => key($metrics));
    // Return options
    return $options; 
  }
 
  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {  
    // Load defined metrics
    $metrics = $this->metrics();
    // Define the field options with defined metrics
	$options = [];
	foreach ($metrics as $parameter => $metric) {
      $options[$parameter] = $metric['title']->render();
	}
    $form['metrics'] = array(
      '#title' => $this->t('Choose a metric'),
      '#type' => 'select',
      '#default_value' => $this->options['metrics'],
      '#options' => $options,
    );
    // Build options
    parent::buildOptionsForm($form, $form_state);  
  }
 
  /**
   * {@inheritdoc}
   */
  public function query() {
    // do nothing -- to override the parent query.
  }
	
  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
    // Customize the field value
	$datas_json = $values->_entity->get('field_datas')->value;
	$datas = json_decode($datas_json);
	$metric = $this->options['metrics'];
	$metric_value = $datas->$metric; 
    // Return metric value
    return $metric_value;
    
  }
 
}

Thanks for your help. Yours explanations will help me to understand exactly how to the aggregation option works (and views) for this specific case.

P.S. If I must alter the query() can you give me an example of code?

πŸ› Bug report
Status

Needs work

Version

10.1 ✨

Component
ViewsΒ  β†’

Last updated about 9 hours ago

Created by

πŸ‡«πŸ‡·France kumkum29

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

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 bwong

    This patch was initially submitted to address an integration issue with modules like Views Raw SQL β†’ .

    As it turns out, I didn't fully understand the entire mechanism for implementing aggregation. Turns out that selection a method other than Group By replaces the handler with NumericField that essentially ignores the existing handler and it assumes that a field is involved. There are two ways to address this. The more complicated way, and possibly better, is to come up with a NumericField replacement and change how the handler is changed. The replacement could keep the details of the original field support in place. The other alternative is to put any support for aggregation into the desired plugin.

    I chose the latter with the Views Raw SQL #15023002 patch #5 πŸ› the Value is lost with Aggregation Needs review . This essentially keeps the Group By setting but hides it so the NumericField substitution does not occur. The aggregation support is handled by replacing the options form selection with a new one and using the value in the aggregation support handled by the original field support.

    For Views Raw SQL, this solution works for all of the field types including field, filter, sort and argument.

    The solution I presented here does not really address the issue as it is trying to adjust changes imposed by NumericField. Assuming either approach above, the need for an aggregation override is not needed. If this is the case then this issue should be closed.

  • Status changed to Needs work over 1 year ago
  • The Needs Review Queue Bot β†’ tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide β†’ to find step-by-step guides for working with issues.

Production build 0.71.5 2024