Problem/Motivation
Since the introduction of return types through #3478883: Parameter typing and return type hints, any field values that don't return a string will result in a TypeError:
Drupal\views_aggregator\Plugin\views\style\Table::getCellRaw(): Return value must be of type string, [Markup/Float/FALSE/NULL] returned in [...]
Original Problem/Motivation
TypeError when showing webforms with a Twig element. Using the views_aggregate module, I want to display a table of webform submissions. That used to work with code prior to commit 5aac9552eb039f30982b62763283806433e8fae0.
I am using views_aggregator version 2.1.1.
Complete error message: TypeError: Drupal\views_aggregator\Plugin\views\style\Table::getCellRaw(): Return value must be of type string, Drupal\Core\Render\Markup returned in Drupal\views_aggregator\Plugin\views\style\Table->getCellRaw() (Zeile 754 in /opt/drupal/web/modules/contrib/views_aggregator/src/Plugin/views/style/Table.php).
The call stack lists this
#0 /opt/drupal/web/modules/contrib/views_aggregator/src/Plugin/views/style/Table.php(648): Drupal\views_aggregator\Plugin\views\style\Table->getCellRaw(Object(Drupal\webform_views\Plugin\views\field\WebformSubmissionField), Object(Drupal\views\ResultRow), true)
Steps to reproduce
I have a webform with the following element:
sum_float:
'#type': webform_computed_twig
'#title': 'Sum as float'
'#description': 'For Views aggregate'
'#title_display': none
'#display_on': none
'#template': |-
{% set sum = 0.0 %}
{% for f in data.themen %}
{% set val = f.betrag|trim|replace({',': '.'}) %}
{% if (val) %}
{% set sum = sum + val|round(2) %}
{% endif %}
{% endfor %}
{{ sum|number_format(2) }}
'#store': true
'#ajax': true
Proposed resolution
Set return type to mixed.
Remaining tasks
Adjust Tests for verifying that it also works for float and markup values.
User interface changes
None.
API changes
None.
Data model changes
None.
| Comment | File | Size | Author |
|---|---|---|---|
| #12 | 3493815-typeerror-in-getcellraw-12.patch | 10.48 KB | grevil |
Issue fork views_aggregator-3493815
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:
- 3493815-typeerror-in-getcellraw
changes, plain diff MR !37
- 2.1.x
changes, plain diff MR !31
Comments
Comment #2
gogowitsch commentedThis bug was introduced by issue #3478883: Parameter typing and return type hints
Comment #4
gogowitsch commentedComment #5
tr commentedCan you write a test case for that Webform? None of the webform-related code in this module is being tested.
Basically just a functional test which creates a webform with your element, creates a view, then visits the page for that view. The test should fail with the current views_aggregator code and pass with your proposed patch.
Having at least one test for Webforms will allow us to test and eliminate other bugs that are related to webforms, and will serve as an example for the other Webform-related patches that are stuck because we don't have test cases for any of them.
Comment #6
grevil commentedThis is a good workaround for now. But I am unsure whether we should do it like this or simply set the return type of
getCellRaw()andgetCell()to mixed. This way we keep the orginal field type.Both seems to work fine, but since it worked fine before #3478883: Parameter typing and return type hints, I'd suggest setting the type to mixed, so we keep the original field type.
Comment #7
grevil commentedComment #10
grevil commentedMaybe someone can adjust the test to also include Markup and Float (decimal) fields? That way we would be all set, but again since it worked like this without problems before #3478883: Parameter typing and return type hints we don't critically need it.
@maintainer, make sure to give contribution to @tame4tex for providing the test!
Comment #11
grevil commentedComment #12
grevil commentedPatch for the time being.
Comment #13
grevil commentedSorry, forgot to write that for us the problem are floats being returned instead of a string.
Comment #14
grevil commentedComment #15
hoporr commentedpatch #12 worked for me.