I have a webform with a Document File field. In my twig email template, I want to test if that field is empty. For text area fields for example, this syntax works :

{% if webform_token('[webform_submission:values:question_text_area:value]', webform_submission) %}
 <br>
 <table>
  <tr>
  <th>Questions</th>
  </tr>
  <tr>
   <td>{{ webform_token('[webform_submission:values:question_text_area:value]', webform_submission) }}</td>
  </tr>
 </table>
 {% endif %}

But for a Document File field, the same syntax does not work, meaning that if no file is uploaded, that part of my template is still printed.

I tried

{% if webform_token('[webform_submission:values:join_a_file:value]', webform_submission) %}

or

{% if webform_token('[webform_submission:values:join_a_file:value]', webform_submission) is not null %}

or

{% if webform_token('[webform_submission:values:join_a_file]', webform_submission) is not null %}

and none of them work.

Can anybody help me get the proper syntax for what I'm trying to do? Thanks!

CommentFileSizeAuthor
#2 webform.webform.issue_2969437.yml4.06 KBjrockowitz
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

a_godin created an issue. See original summary.

jrockowitz’s picture

Status: Active » Needs review
FileSize
4.06 KB

The issue is that tokens are no longer being cleared when no value is being stored in the database.

When no file is uploaded the call to webform_token('[webform_submission:values:join_a_file:value]', webform_submission) returns the original token which is [webform_submission:values:join_a_file:value].

The trick/solution is to add a clear attribute to the token by changing [webform_submission:values:join_a_file:value] to [webform_submission:values:join_a_file:value:clear].

The attached webform renders FILE UPLOADED or NO FILE UPLOADED using...

{% if webform_token('[webform_submission:values:file:clear]', webform_submission) %}
  FILE UPLOADED
{% else %}
  NO FILE UPLOADED
{% endif %}

@see #2920199: Stop clearing missing tokens

BTW, for general support questions, can you please post them to https://drupal.stackexchange.com/questions/tagged/webforms.

jrockowitz’s picture

Status: Needs review » Fixed
a_godin’s picture

Thank you very much for your answer, that solved it for me.

And I posted my question here as a last resort ... I had posted the same question on drupal stackexchange ( https://drupal.stackexchange.com/questions/260143/how-to-test-the-existe... ) and on the Drupal forum ( https://www.drupal.org/forum/support/theme-development/2018-04-09/how-to... ) but did not get an answer that solved the problem. I will update both with your solution.

Thanks again!

jrockowitz’s picture

No problem. Thanks for reposting the solution.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

AswathyAjish’s picture

The above solution worked for me. Thanks.

{% if webform_token('[webform_submission:values:file:clear]', webform_submission) %}
FILE UPLOADED
{% else %}
NO FILE UPLOADED
{% endif %}