Computed Twig on Date/ Time with Conditions results in php error

Created on 1 December 2023, 12 months ago
Updated 29 January 2024, 10 months ago

Problem/Motivation

Calculating the time difference using the Date/ Time elements with any conditions applied to the Computed Twig element results in the php error "TypeError: strtotime(): Argument #1 ($datetime) must be of type string, array given in strtotime()"

Steps to reproduce

Add 2 Date/ Time elements, in my case "Start Date and Time" and "End Date and Time".
Add a Computed Twig element (Computed Time Worked).
Code in "Computed value/markup":

{# calculate the difference (in days) between two date elements #}
{% set startDate = webform_token('[webform_submission:values:start_date_and_time:raw:clear]', webform_submission) ?: 0 |date('d.m.Y H:i:s') %}
{% set endDate = webform_token('[webform_submission:values:end_date_and_time:raw:clear]', webform_submission) ?: 0 |date('d.m.Y H:i:s') %}
{% set difference = endDate|date('U') - startDate|date('U') %}
{% set differenceDays = (difference / 60 / 60 )%}
{{ differenceDays }}

Set Visible or Enabled condition to any value.

Save elements and fill out the webform. The browser returns:

The website encountered an unexpected error. Please try again later.

Drupal logs the following:

TypeError: strtotime(): Argument #1 ($datetime) must be of type string, array given in strtotime() (line 188 of (redacted)/web/modules/contrib/webform/src/Plugin/WebformElement/DateBase.php)

Removing the condition from the Computed Twig element allows it to function properly.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ’¬ Support request
Status

Closed: outdated

Version

6.2

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States spivey

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @spivey
  • webform_token can return an array. That would explain what you are seeing. I think this is a support request.

    Can you limit that code to the first line that throws the TypeError? I think it could be the first line.

  • πŸ‡ΊπŸ‡ΈUnited States spivey

    I'm not sure what you mean by that, but I changed the code in the "Computed Field/ Markup" to only have:
    {% set startDate = webform_token('[webform_submission:values:start_date_and_time:raw:clear]', webform_submission) ?: 0 |date('d.m.Y H:i:s') %}

    Filled the form out and received the same errors.

  • I believe webform_token('[webform_submission:values:start_date_and_time:raw:clear]', webform_submission) is returning an array, which it may well do. And that would cause exactly this TypeError. I do not see how this represents a bug in webform.

  • πŸ‡ΊπŸ‡ΈUnited States spivey
  • πŸ‡ΊπŸ‡ΈUnited States spivey

    I see the logic in not classifying it as a bug however I do not this is supposed to work how it is working. I've updated this is issue to reflect it is not a bug however I'm still not clear on how to solve the problem.

  • What is the datatype you are passing to |date? All evidence is that it is an array.

  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY
  • πŸ‡ΊπŸ‡ΈUnited States spivey

    The only datetype that should be in use is whatever datatype the Date/ Time element uses. I'm not using any custom fields or elements from outside Webforms.

  • Status changed to Closed: outdated 10 months ago
Production build 0.71.5 2024