Problem/Motivation
When using aggregation in Views, changing the aggregation method on a field to anything other than the default "group" (such as "count", "sum", etc.) triggers an AJAX error on subsequent edits, preventing users from saving or modifying the view.
The technical cause (as investigated in the duplicate issue #3385479) lies in a mismatch during the form lifecycle within `views_ui`:
1. The `EntityField` class (which extends `FieldPluginBase`) is expected to submit the form and handles the definition of `group_column` and `group_columns`.
2. However, the `NumericField` class is incorrectly used during the construction of the form in `ConfigHandlerGroup->buildForm()`.
3. Because `NumericField` doesn't define those fields, reloading or re-editing the aggregation method results in a `TypeError: array_filter(): Argument #1 ($array) must be of type array, null given` within `EntityField->submitGroupByForm()`, crashing the AJAX request with a 500 error.
Steps to reproduce
1. Create or edit a View and enable **Aggregation: Yes** under Advanced settings.
2. Select an entity field and change its aggregation type from "Group together" to "Count" (or any other type). Save it.
3. Re-edit the aggregation settings for that same field.
4. Try to save the form again or interact with the options. An AJAX HTTP 500 error will occur.
Proposed resolution
Ensure that the same handler (`EntityField` instead of `NumericField`) is consistently used both for building and submitting the form inside the `ConfigHandlerGroup->buildForm` class of the `views_ui` module.
Remaining tasks
- [x] Create automated tests to replicate the bug and prevent regressions (Done).
- [x] Review by maintainers (Done).
- [ ] Merge into core.
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #35 | aggregation_in_views-3344910-35.patch | 1.52 KB | turneight |
| #8 | aggregation_in_views-3344910-8.patch | 1.56 KB | turneight |
| #2 | 3344910-2-views-aggregation-error.patch | 851 bytes | ovidenov |
Issue fork drupal-3344910
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
ovidenov commentedComment #3
quietone commentedI happen to be testing with Views right now and saw this issue. I tried to reproduce this on Drupal 10.1.x, umami install and, so far, have not been able to.
Can you elaborate on what you did to "set the aggregation to some value"? Thanks.
Comment #4
rop commentedI get a simular error:
TypeError: array_filter(): Argument #1 ($array) must be of type array, null given in array_filter() (regel 687 van /var/www/[site]/web/core/modules/views/src/Plugin/views/field/EntityField.php)I can save the view though. The error occurs when trying to change the aggregation function on one of my fields from COUNT to anything else. The modal won't close.
Drupal: 9.5.8
Php:8.1.18
I guess the provided patch #2 is not really a solution, since the provided value SHOULD be an array, so the question is why is it NULL instead? The problem is something other, so If we just skip handling it when it is not an array we are only hiding an error, not fixing it.
Comment #5
mkindred commentedI ran across this issue today. I was trying to create a simple admin report showing the number of images (media) attached to each node of a particular content type. (I need to limit the number of images that my client adds to an existing 'product' content type, but first we need a report showing how many he's currently using per product.)
Here are the steps I took (in D9.5.10):
As mentioned in #4, I'm not sure patch #2 is the correct solution, but it prevents the error, allows me to save the aggregation settings per field, and gives me the view I want.
Comment #6
lendudeClosed #3385479: Aggregation in views causes ajax error as a duplicate, very similar patch there.
Thanks for the steps to reproduce, we will need an automated test for this too.
Comment #7
besek commentedI had the same issues, with very similar steps to reproduce as described in #5.
Patch from #2 wroked well for me.
Comment #8
turneight commentedI report the patch proposed in the discussion closed because it was duplicated
Comment #9
xavier.massonThanks for the patch, the patch from #8 works as expected on Drupal core 9.5.11 !
Comment #10
crutch commentedPatch doesn't apply to 10.1.8
When manually changing /core/modules/views/src/Plugin/views/field/EntityField.php and not changing /core/modules/views_ui/src/Form/Ajax/ConfigHandlerGroup.php then the error is gone and works normally again.
Comment #11
dqd@#8 this hasn' addressed #6 yet. This issue needs a reroll of #2 or #8 with the points made in #6.
Comment #12
turneight commented#8 on drupal 10.2 and php 8.3 is still working for me.
#2 hides the problem but does not solve it, the aggregation configuration remains blocked.
I'm sorry but I am currently unable to develop the tests.
Comment #13
dqd@Turneight: Thanks for coming back on this! +1
Comment #14
lendudeRereading this, feels like a duplicate of #2815881: Switching on aggregation generates fatal "Column not found: 1054 Unknown column" SQL error when using multi-column Fields can somebody check if the work there helps fix their issues?
Comment #15
crutch commentedFor me, this issue was happening having a view with (1) Integer (2) Embed containing a single integer field which is aggregated and a (3) Simple Math Field to achieve a result. I simply could not modify aggregation at (2) at some point and found this issue. #14 speaks to multi-column fields which I wasn't using a display field with multi-columns, but in the (2) Embed field, there is a date filter which is related to this original issue. Aggregation is applied to filters and the display integer field at (2). I assume date is a multi-column field like image.
Comment #16
2pha#8 seems to work for me
Comment #17
monaw commentedi have the same issue using D10.2.3, with default theme. I get this error if i try to aggregate decimal or text fields.
Here's the error I'm getting in javascript console:
and here's the error in the recent log:
I applied patch #8, the errors went away but the aggregation for DISTINCT COUNT still doesn't work...
Comment #18
johnpitcairn commentedRe #14 and #15: This is not specific to multi-column fields. I can reproduce it with a single entityreference field in a view of commerce subscriptions.
I can set aggregation for the field to COUNT, and apply.
But thereafter I cannot remove or change the aggregation for the field. Clicking apply will produce the ajax error.
Un-postponing.
Comment #19
guillaumeduveau#8 seems to work for me too.
Comment #20
yonailoYes #8 works for me too (I am running Drupal 10.3.0)
Comment #25
binoli lalani commentedHello,
I created MR as part of new process of contribution.
Please review.
Thanks!
Comment #26
smustgrave commentedPreviously tagged for test which still appear to be needed.
Issue summary also appears to be incomplete.
Comment #27
alfthecat commentedThe latest MR fixed the issue on my end: I could not set the aggration settings to "Count Distinct" which was breaking aggregation in my view all together. After the patch, it all worked.
Thanks everyone for the great work on this so far.
Comment #28
loze commentedThis MR works. I'm constantly running into this issue with aggregated views. Thanks!
Comment #29
gaddman commentedPatch #8 working for me, Drupal 10.4.7.
Thanks!
Comment #30
joshua1234511I attempted to add a test, but reproducing the AJAX behavior and accurately triggering the failure in a test scenario is significantly more complex than the actual fix itself. Given that the MR directly resolves the issue.
Tested the merge Request - Resolves the issue.
I would suggest proceeding with merging the fix as is, and leaving a follow-up issue open specifically for adding test coverage.
Comment #31
sagarsingh24 commented### Manual review
**Reproduced**
* Clean Drupal **11.2.x** install.
* Enabled *Aggregation* in **Views → Advanced → Other**.
* Added a **Date** field with an aggregation function.
* Saving the View triggered the Ajax error reported.
**Patch tested**
Applied **#2**
**Result**
* View now saves/updates without Ajax errors.
* Aggregated results render correctly.
* No new PHP warnings/notices observed.
**Environment**
Drupal 11.2 • PHP 8.3 • MySQL 8.0 • Local stack (DDEV + Docker)
---
✅ The patch fixes the issue for me.
Comment #32
bramdriesenRe #31, what did you actually test? You're referencing the patch from #2, but also the MR !11081 which contains more changes as the patch...
Comment #33
smustgrave commentedWe don’t really merge in fixes and push test coverage unless it’s critical which this does not count, sorry
Comment #34
sagarsingh24 commentedRE #32 Sorry for the mix‑up.I was juggling several tickets and accidentally copied the wrong reference. I’ve tested patch #2 on a fresh Drupal 11.2 setup (PHP 8.3) and it fixes the Ajax error which i was able to replecate on my drupal 11 setup by following the above mention steps . All tests were done manually. I’m still new to this and will double‑check ticket numbers from now on.Thank u for highlighting my mistake from now on i will keep this in my mind
Comment #35
turneight commentedDrupal 11.3 still has this problem, but it needs a patch update which I'm attaching.
Comment #36
bramdriesen@turneight It's better to update the merge request. Patch workflows are deprecated, and you did not provide an interdiff, so it's very difficult to see what changed in your patch.
Comment #38
turneight commented@bramdriesen: It was just a merge issue, no other changes.
I sent the merge request... I hope it's correct.
Comment #42
turneight commentedMerge updates only work for versions 11.4+ because they depend on the views.plugin_managers service.
For Drupal 11.3, use patch 35, and for older versions, use patch 8.
I removed the changes to the views module because I believe that errors in the backend should be displayed and not ignored.
Comment #43
turneight commentedAdded test coverage to merge request MR !14136
Comment #44
smustgrave commentedFirst thing noticed when opening the ticket was that the summary was incomplete and missing the one of the most import parts, what's the proposed solution.
I left a comment on the MR as it read just too much AI verboseness to me. 0 issue with using AI but it does need to be disclosed just fyi.
Comment #45
mortona2k commentedThe patches #2/8 have changes in EntityField->submitGroupByForm() that are not MR 14136.
Is that intentional?
Comment #46
turneight commented@smustgrave Thanks for looking at the code.
I certainly used AI, but the development and testing are manual; I spent almost a whole day on it.
The comments are intentionally verbose to allow you to understand the rationale behind the code, but they can be significantly reduced in production.
As highlighted in #30, it's an easy bug to reproduce manually, but not with tests.
I didn't find any suitable tests to modify; having more fields would have greatly complicated the test. Dialog boxes don't make things easy, and I didn't find any functions in the core to handle them.
It could be simplified by starting with an aggregation view, but that part of the test would be lost.
The tests don't seem particularly burdensome.
@mortona2k I purposely removed those changes because they hid this bug (and perhaps others) without fixing it.
I described the source of the problem in [#3385479-11]
Proposed solution:
Use the same handler used to submit the form to build it.
Comment #47
turneight commentedCleaned up verbose comments.
Comment #48
turneight commentedMoving this to RTBC as the requested tests have been added, verified, and approved by the maintainer.
The automated test suite is passing successfully.
Thank you for the review!
Merge compatible with Drupal 11.4+
Comment #49
smustgrave commentedThis needs to be reviewed and marked by someone who didn’t work on the MR
Comment #50
joelpittetI have a fix for this issue #3613882: Aggregation settings form builds and submits with different handlers it's similar but different approach.