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.
Comments
Comment #2
cilefen commentedwebform_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.
Comment #3
spivey commentedI'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.
Comment #4
cilefen commentedI 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.Comment #5
spivey commentedComment #6
spivey commentedI see the logic in not classifying it as a bug however I do not think this is supposed to work how it is working. I've updated this issue to reflect it is not a bug however I'm still not clear on how to solve the problem.
Comment #7
cilefen commentedWhat is the datatype you are passing to
|date? All evidence is that it is an array.Comment #8
jrockowitz commentedComment #9
spivey commentedThe only datatype 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. Also why would the datatype change when conditions are applied? As I mentioned before if there are no conditions set on the computed twig element the form works. When any type of condition is used the error happens.
Comment #10
cilefen commented