Support Plugins for Views Aggregate

Created on 4 February 2016, over 8 years ago
Updated 7 February 2023, over 1 year ago

Problem/Motivation

Views supports a minimal set of aggregate functions that are hard coded into the default query plugin. It would be useful to have a way to add support for the full range of SQL aggregates available in MySQL & PostgreSQL (GROUP_CONCT/STRING_AGG, VARIANCE, ...), as well as providing a means of supporting the extended set of OGC spatial aggregates supported by PostgreSQL (ST_Union, ST_ClusterWithin, ...), and allowing any other user-defined aggregates that may be available.

Proposed resolution

Provide a plugin for defining new aggregates.
Use Annotation-based methods to expose plugin API, but maintain the current array format of aggregate type definition (see "Implementation Example" at bottom of issue for existing aggregate definition array format)..
Adding a new aggregate will be achieved by simply providing a properly formatted annotation and an empty sub-class of AggregatePluginBase(see Code 1).
The proposed implementation supports the existing aggregate definition array structure, and also how to provide backwards compatibility with a Drupal 7 approach

Code 1: Annotation block and empty class for VAR_POP plugin. This example will work inside a contrib module, if located in "modules/my_module_name/src/Plugin/views/aggregate/Var_Pop.php", or in Views core as "core/modules/views/src/Plugin/views/aggregate/Var_Pop.php".

<?php
namespace Drupal\views\Plugin\views\aggregate;

use Drupal\views\Plugin\views\PluginBase;

/**
 * Aggregate plugin for SQL VAR_POP function.
 *
 * @ViewsAggregate(
 *   id = "var_pop",
 *   function = "var_pop",
 *   title = @Translation("Variance (pop)"),
 *   method = "aggregationMethodSimple",
 *   handler = {
 *     "argument" = "groupby_numeric",
 *     "field" = "numeric",
 *     "filter" = "groupby_numeric",
 *     "sort" = "groupby_numeric"
 *   },
 *   help = @Translation("Population Variance."),
 * )
 */
class Var_Pop extends AggregatePluginBase {


  // Class methodsโ€ฆ
}

For further details, see "Implementation Example", and for further details a related issue with patch for implementation in D7 " Support Plugins for Views Aggregate in D7 โ†’ ").

Remaining tasks

  1. Test

User interface changes

None.

API changes

Add Annotation-based plugin support for ViewsAggregate

Data model changes

None.

Implementation Example

Code 2: Plugin definition array excerpt.

    return array(
...
      'count' => array(
        'title' => $this->t('Count'),
        'method' => 'aggregationMethodSimple',
        'handler' => array(
          'argument' => 'groupby_numeric',
          'field' => 'numeric',
          'filter' => 'groupby_numeric',
          'sort' => 'groupby_numeric',
        ),
      ),

Code 3: Sample implementation of VAR_SAMP in Drupal 7 using Ctools plugin.

function views_views_plugins() {
  return array(
    'query_aggregate' => array(
      'var_samp' => array(
        'title' => t('Sample Variance'),
        'method' => 'views_query_default_aggregation_method_simple',
        'handler' => array(
          'argument' => 'views_handler_argument_group_by_numeric',
          'filter' => 'views_handler_filter_group_by_numeric',
          'sort' => 'views_handler_sort_group_by_numeric',
        ),
      ),
...
โœจ Feature request
Status

Needs work

Version

10.1 โœจ

Component
Viewsย  โ†’

Last updated about 2 hours ago

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States robertwb

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.

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.

Production build 0.69.0 2024