Commit 9c4baed6 authored by catch's avatar catch
Browse files

fix: #2735997 Decimal separator and precision settings unavailable when aggregating decimal fields

By: mschudders
By: nikita_tt
By: lendude
By: playful
By: ranjith_kumar_k_u
By: joelpittet
By: asad_ahmed
By: catch
By: Ben Greenberg
By: ramil g
(cherry picked from commit cf863a1d)
parent 69ead8f8
Loading
Loading
Loading
Loading
Loading
+11 −6
Changes for core/modules/views/src/EntityViewsData.php: 11 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -527,6 +527,8 @@ protected function mapSingleFieldViewsData($table, $field_name, $field_type, $co
      default:
        // For most fields, the field type is generic enough to just use
        // the column type to determine the filters etc.
        $views_field['field']['id'] = 'field';
        $views_field['sort']['id'] = 'standard';
        switch ($column_type) {

          case 'int':
@@ -534,13 +536,20 @@ protected function mapSingleFieldViewsData($table, $field_name, $field_type, $co
          case 'smallint':
          case 'tinyint':
          case 'mediumint':
            $views_field['argument']['id'] = 'numeric';
            $views_field['filter']['id'] = 'numeric';
            break;

          case 'float':
          case 'double':
          case 'decimal':
            $views_field['field']['id'] = 'field';
          case 'numeric':
            // Flag decimal values so that the numeric field handler, used
            // when aggregation is enabled, offers precision and decimal point
            // settings.
            $views_field['field']['float'] = TRUE;
            $views_field['argument']['id'] = 'numeric';
            $views_field['filter']['id'] = 'numeric';
            $views_field['sort']['id'] = 'standard';
            break;

          case 'char':
@@ -551,17 +560,13 @@ protected function mapSingleFieldViewsData($table, $field_name, $field_type, $co
          case 'text':
          case 'mediumtext':
          case 'longtext':
            $views_field['field']['id'] = 'field';
            $views_field['argument']['id'] = 'string';
            $views_field['filter']['id'] = 'string';
            $views_field['sort']['id'] = 'standard';
            break;

          default:
            $views_field['field']['id'] = 'field';
            $views_field['argument']['id'] = 'standard';
            $views_field['filter']['id'] = 'standard';
            $views_field['sort']['id'] = 'standard';
        }
    }

+6
Changes for core/modules/views/src/FieldViewsDataProvider.php: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -327,6 +327,12 @@ public function defaultFieldImplementation(FieldStorageConfigInterface $field_st
        'element type' => 'div',
        'is revision' => $type == EntityStorageInterface::FIELD_LOAD_REVISION,
      ];
      // The decimal field type uses the numeric schema column type.
      // Set the float flag for float and numeric columns. The numeric field
      // handler uses the flag to show precision and decimal point settings.
      if (in_array($field_columns[$real_field]['type'], ['float', 'numeric'], TRUE)) {
        $data[$table_alias][$field_alias]['field']['float'] = TRUE;
      }
    }

    // Expose data for each field property individually.
+47
Changes for core/modules/views/tests/modules/views_test_config/test_views/views.view.test_aggregate_decimal.yml: 47 added lines, 0 removed lines.
Original line number Diff line number Diff line
langcode: en
status: true
dependencies: {  }
id: test_aggregate_decimal
label: test_aggregate_decimal
module: views
description: ''
tag: ''
base_table: entity_test
base_field: nid
display:
  default:
    display_options:
      access:
        type: none
      cache:
        type: none
      exposed_form:
        type: basic
      fields:
        field_decimal:
          empty_zero: false
          field: field_decimal
          group_type: sum
          hide_empty: false
          id: field_decimal
          plugin_id: field
          set_precision: true
          precision: 2
          decimal: ','
          separator: ''
          table: entity_test__field_decimal
      group_by: true
      pager:
        type: none
      query:
        options:
          query_comment: ''
        type: views_query
      row:
        type: fields
      style:
        type: default
    display_plugin: default
    display_title: Default
    id: default
    position: 0
+22
Changes for core/modules/views/tests/src/Kernel/Entity/EntityViewsDataTest.php: 22 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -438,6 +438,28 @@ public function testBaseTableFields(): void {
    $this->assertArrayNotHasKey('allow empty', $data['entity_test']['name']['filter']);
  }

  /**
   * Tests decimal and float fields on the base table.
   */
  public function testBaseTableDecimalFields(): void {
    $this->commonBaseFields['decimal'] = BaseFieldDefinition::create('decimal')
      ->setLabel('Decimal');
    $this->commonBaseFields['float'] = BaseFieldDefinition::create('float')
      ->setLabel('Float');
    $this->setUpEntityType($this->baseEntityType, $this->commonBaseFields);

    $data = $this->entityTypeManager->getHandler('entity_test', 'views_data')->getViewsData();

    foreach (['decimal', 'float'] as $field_name) {
      $this->assertNumericField($data['entity_test'][$field_name]);
      // Decimal values are flagged so that the numeric field handler, used
      // when aggregation is enabled, offers precision and decimal point
      // settings.
      $this->assertTrue($data['entity_test'][$field_name]['field']['float']);
    }
    $this->assertArrayNotHasKey('float', $data['entity_test']['id']['field']);
  }

  /**
   * Tests fields on the data table.
   */
+37
Changes for core/modules/views/tests/src/Kernel/FieldApiDataTest.php: 37 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -185,6 +185,43 @@ public function testViewsData(): void {
    $this->assertEquals('Appears in: article, page, news. Also known as: Content: GiraffeA" label', $data[$current_table][$field_storage_string->getName()]['help']);
  }

  /**
   * Tests the float flag for decimal and integer fields.
   */
  public function testViewsDataFloatFlag(): void {
    NodeType::create([
      'type' => 'page',
      'name' => 'Page',
    ])->save();

    $table_mapping = $this->container->get('entity_type.manager')
      ->getStorage('node')
      ->getTableMapping();

    foreach (['decimal' => TRUE, 'float' => TRUE, 'integer' => FALSE] as $type => $float) {
      $field_storage = FieldStorageConfig::create([
        'field_name' => 'field_' . $type,
        'entity_type' => 'node',
        'type' => $type,
      ]);
      $field_storage->save();
      FieldConfig::create([
        'field_name' => 'field_' . $type,
        'entity_type' => 'node',
        'bundle' => 'page',
      ])->save();

      $table = $table_mapping->getDedicatedDataTableName($field_storage);
      $data = $this->container->get('views.views_data')->get($table);
      if ($float) {
        $this->assertTrue($data['field_' . $type]['field']['float'], "The $type field is flagged as float.");
      }
      else {
        $this->assertArrayNotHasKey('float', $data['field_' . $type]['field'], "The $type field is not flagged as float.");
      }
    }
  }

  /**
   * Gets the views data for the field created in setUp().
   *
Loading