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.

CommentFileSizeAuthor
#12 3493815-typeerror-in-getcellraw-12.patch10.48 KBgrevil
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

gogowitsch created an issue. See original summary.

gogowitsch’s picture

This bug was introduced by issue #3478883: Parameter typing and return type hints

gogowitsch’s picture

Issue summary: View changes
tr’s picture

Issue tags: +Needs tests

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

grevil’s picture

Title: TypeError in getCellRaw: string expected, Markup received » TypeError in getCellRaw: string expected, Markup/NULL/FALSE/Float received
Version: 2.1.1 » 2.1.x-dev
Status: Needs review » Needs work
Issue tags: -Needs tests

This 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() and getCell() 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.

grevil changed the visibility of the branch 2.1.x to hidden.

grevil’s picture

Status: Needs work » Needs review

Maybe 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!

grevil’s picture

grevil’s picture

StatusFileSize
new10.48 KB

Patch for the time being.

grevil’s picture

Sorry, forgot to write that for us the problem are floats being returned instead of a string.

grevil’s picture

Priority: Normal » Major
Issue summary: View changes
Issue tags: -Needs issue summary update
hoporr’s picture

patch #12 worked for me.