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

Issue fork drupal-3344910

Command icon 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

ovidenov created an issue. See original summary.

ovidenov’s picture

StatusFileSize
new851 bytes
quietone’s picture

Title: Ajax error on views with aggregation. » Ajax error on views with aggregation
Issue summary: View changes
Status: Active » Postponed (maintainer needs more info)
Issue tags: +Bug Smash Initiative

I 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.

rop’s picture

I 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.

mkindred’s picture

I 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):

  1. Create a view for a single content type, table list of fields.
  2. Add a relationship to each of two media fields (images in my case).
  3. Add two image fields to the view, each using one of the two relationships above.
  4. Turn on aggregation. The aggregation works as expected, except that the two fields' counts are multiplied by each other.
  5. I tried to change the aggregation type for the two image fields from 'Count' to 'Count DISTINCT'. But the modal won't close, and I'm seeing an error in watchdog:

TypeError: array_filter(): Argument #1 ($array) must be of type array, null given in array_filter() (line 687 of /var/www/html/drupal/web/core/modules/views/src/Plugin/views/field/EntityField.php)

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.

lendude’s picture

Version: 9.5.x-dev » 11.x-dev
Status: Postponed (maintainer needs more info) » Needs work
Issue tags: +Needs tests

Closed #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.

besek’s picture

I had the same issues, with very similar steps to reproduce as described in #5.
Patch from #2 wroked well for me.

turneight’s picture

StatusFileSize
new1.56 KB

I report the patch proposed in the discussion closed because it was duplicated

xavier.masson’s picture

Thanks for the patch, the patch from #8 works as expected on Drupal core 9.5.11 !

crutch’s picture

Patch 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.

dqd’s picture

@#8 this hasn' addressed #6 yet. This issue needs a reroll of #2 or #8 with the points made in #6.

turneight’s picture

#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.

dqd’s picture

@Turneight: Thanks for coming back on this! +1

lendude’s picture

Rereading 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?

crutch’s picture

For 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.

2pha’s picture

#8 seems to work for me

monaw’s picture

i 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:

[Error] Failed to load resource: the server responded with a status of 500 () (ref_pollutant_category, line 0)
[Error] AjaxError:
An AJAX HTTP error occurred.
HTTP Result Code: 500
Debugging information follows.
Path: /admin/structure/views/ajax/handler-group/recommended_ef_charts/block_2/field/ref_pollutant_category
StatusText: error
ResponseText: The website encountered an unexpected error. Try again later.TypeError: array_filter(): Argument #1 ($array) must be of type array, null given in array_filter() (line 692 of core/modules/views/src/Plugin/views/field/EntityField.php). Drupal\views\Plugin\views\field\EntityField->submitGroupByForm(Array, Object) (Line: 106)
Drupal\views_ui\Form\Ajax\ConfigHandlerGroup->submitForm(Array, Object)
call_user_func_array(Array, Array) (Line: 264)
Drupal\views_ui\ViewUI->standardSubmit(Array, Object)
call_user_func_array(Array, Array) (Line: 129)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 67)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 597)
Drupal\Core\Form\FormBuilder->processForm('views_ui_config_item_group_form', Array, Object) (Line: 325)
Drupal\Core\Form\FormBuilder->buildForm('Drupal\views_ui\Form\Ajax\ConfigHandlerGroup', Object) (Line: 215)
Drupal\views_ui\Form\Ajax\ViewsFormBase->Drupal\views_ui\Form\Ajax\{closure}() (Line: 627)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 217)
Drupal\views_ui\Form\Ajax\ViewsFormBase->ajaxFormWrapper('Drupal\views_ui\Form\Ajax\ConfigHandlerGroup', Object) (Line: 127)
Drupal\views_ui\Form\Ajax\ViewsFormBase->getForm(Object, 'block_2', 'ajax') (Line: 38)
Drupal\views_ui\Form\Ajax\ConfigHandlerGroup->getForm(Object, 'block_2', 'ajax', 'field', 'ref_pollutant_category')
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 627)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 181)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 28)
Drupal\Core\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 32)
Drupal\big_pipe\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 36)
Drupal\Core\StackMiddleware\AjaxPageState->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

and here's the error in the recent log:

Location: https://wfrd.ddev.site/admin/structure/views/ajax/handler-group/recommen...
Message: TypeError: array_filter(): Argument #1 ($array) must be of type array, null given in array_filter() (line 692 of /var/www/html/web/core/modules/views/src/Plugin/views/field/EntityField.php).
Backtrace:
#0 /var/www/html/web/core/modules/views/src/Plugin/views/field/EntityField.php(692): array_filter(NULL)
#1 /var/www/html/web/core/modules/views_ui/src/Form/Ajax/ConfigHandlerGroup.php(106): Drupal\views\Plugin\views\field\EntityField->submitGroupByForm(Array, Object(Drupal\Core\Form\FormState))
#2 [internal function]: Drupal\views_ui\Form\Ajax\ConfigHandlerGroup->submitForm(Array, Object(Drupal\Core\Form\FormState))
#3 /var/www/html/web/core/modules/views_ui/src/ViewUI.php(264): call_user_func_array(Array, Array)
#4 [internal function]: Drupal\views_ui\ViewUI->standardSubmit(Array, Object(Drupal\Core\Form\FormState))
#5 /var/www/html/web/core/lib/Drupal/Core/Form/FormSubmitter.php(129): call_user_func_array(Array, Array)
#6 /var/www/html/web/core/lib/Drupal/Core/Form/FormSubmitter.php(67): Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object(Drupal\Core\Form\FormState))
#7 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(597): Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object(Drupal\Core\Form\FormState))
#8 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(325): Drupal\Core\Form\FormBuilder->processForm('views_ui_config...', Array, Object(Drupal\Core\Form\FormState))
#9 /var/www/html/web/core/modules/views_ui/src/Form/Ajax/ViewsFormBase.php(215): Drupal\Core\Form\FormBuilder->buildForm('Drupal\\views_ui...', Object(Drupal\Core\Form\FormState))
#10 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(627): Drupal\views_ui\Form\Ajax\ViewsFormBase->Drupal\views_ui\Form\Ajax\{closure}()
#11 /var/www/html/web/core/modules/views_ui/src/Form/Ajax/ViewsFormBase.php(217): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#12 /var/www/html/web/core/modules/views_ui/src/Form/Ajax/ViewsFormBase.php(127): Drupal\views_ui\Form\Ajax\ViewsFormBase->ajaxFormWrapper('Drupal\\views_ui...', Object(Drupal\Core\Form\FormState))
#13 /var/www/html/web/core/modules/views_ui/src/Form/Ajax/ConfigHandlerGroup.php(38): Drupal\views_ui\Form\Ajax\ViewsFormBase->getForm(Object(Drupal\views_ui\ViewUI), 'block_2', 'ajax')
#14 [internal function]: Drupal\views_ui\Form\Ajax\ConfigHandlerGroup->getForm(Object(Drupal\views_ui\ViewUI), 'block_2', 'ajax', 'field', 'ref_pollutant_c...')
#15 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
#16 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(627): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#17 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#18 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
#19 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(181): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#20 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#21 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#22 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#23 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#24 /var/www/html/web/core/modules/big_pipe/src/StackMiddleware/ContentLength.php(32): Drupal\Core\StackMiddleware\ContentLength->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#25 /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\big_pipe\StackMiddleware\ContentLength->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#26 /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#27 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#28 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#29 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#30 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#31 /var/www/html/web/core/lib/Drupal/Core/DrupalKernel.php(704): Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#32 /var/www/html/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#33 {main}

I applied patch #8, the errors went away but the aggregation for DISTINCT COUNT still doesn't work...

johnpitcairn’s picture

Status: Postponed (maintainer needs more info) » Needs work

Re #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.

guillaumeduveau’s picture

#8 seems to work for me too.

yonailo’s picture

Yes #8 works for me too (I am running Drupal 10.3.0)

binoli lalani made their first commit to this issue’s fork.

binoli lalani changed the visibility of the branch 3344910-ajax-error-on to hidden.

binoli lalani changed the visibility of the branch 3344910-ajax-error-on to active.

binoli lalani’s picture

Status: Needs work » Needs review

Hello,

I created MR as part of new process of contribution.

Please review.

Thanks!

smustgrave’s picture

Status: Needs review » Needs work

Previously tagged for test which still appear to be needed.

Issue summary also appears to be incomplete.

alfthecat’s picture

The 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.

loze’s picture

This MR works. I'm constantly running into this issue with aggregated views. Thanks!

gaddman’s picture

Patch #8 working for me, Drupal 10.4.7.
Thanks!

joshua1234511’s picture

Issue summary: View changes
Status: Needs work » Needs review

I 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.

sagarsingh24’s picture

### 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.

bramdriesen’s picture

Re #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...

smustgrave’s picture

Status: Needs review » Needs work

We don’t really merge in fixes and push test coverage unless it’s critical which this does not count, sorry

sagarsingh24’s picture

RE #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

turneight’s picture

StatusFileSize
new1.52 KB

Drupal 11.3 still has this problem, but it needs a patch update which I'm attaching.

bramdriesen’s picture

@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.

turneight’s picture

Component: views.module » views_ui.module
Assigned: ovidenov » Unassigned

@bramdriesen: It was just a merge issue, no other changes.
I sent the merge request... I hope it's correct.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

turneight changed the visibility of the branch 11.x to hidden.

turneight changed the visibility of the branch 3344910-ajax-error-on to hidden.

turneight’s picture

Merge 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.

turneight’s picture

Status: Needs work » Needs review

Added test coverage to merge request MR !14136

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update

First 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.

mortona2k’s picture

The patches #2/8 have changes in EntityField->submitGroupByForm() that are not MR 14136.

Is that intentional?

turneight’s picture

Issue summary: View changes

@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.

turneight’s picture

Status: Needs work » Needs review

Cleaned up verbose comments.

turneight’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests, -Needs issue summary update

Moving 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+

smustgrave’s picture

Status: Reviewed & tested by the community » Needs review

This needs to be reviewed and marked by someone who didn’t work on the MR

joelpittet’s picture