Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 294468

Support Plugins for Views Aggregate

$
0
0

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. Create Annotation Class ViewsAggregate
  2. Create class AggregatePluginBase in src/Plugin/views/aggregate
  3. Add plugin.manager.views.aggregate to views.services.yml
  4. Add plugin functions retrieval in Sql::getAggregationInfo()
  5. Create Plugins for VAR_SAMP & VAR_POP aggregate functions in order to provide implementation example.
  6. Create plugins for all default aggregates
  7. Remove hard-coded default aggregates from Plugin/views/query/Sql.php
  8. Include var_pop and var_samp in a contrib ready stand-alone module "views_aggregate_example".
  9. 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',
        ),
      ),
...

Viewing all articles
Browse latest Browse all 294468

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>