0

I create a field with field_countdown. Now I want hide some part of my node text when the timer is zero.

For this when I use this code:

{% set a = 'now'|date("U") %}
{% set b = node.field_timer.value|date("U") %}
{% if a < b %}
    Show
{% else %}
    Hide
{% endif %}

Work but not currectly. And in first page send me an error.

The website encountered an unexpected error. Please try again later. TypeError: DateTime::__construct() expects parameter 1 to be string, array given in DateTime->__construct() (line 519 of vendor\twig\twig\lib\Twig\Extension\Core.php).

In second way I create a field like my field_timer and just show secondes and after my timer show zero IF command work but in this way I add a field and it's not good.

How can I do this with other codes in Drupal 8 ?

5
  • The field Countdown module uses jQuery Countdown Timer. So I think you would need to do this client-side too.
    – 4uk4
    Sep 3, 2017 at 8:28
  • Thanks but how?I do two way that say in question but not good and not correctly. countdown uses jQuery but {{ node.field_timer.value }} show the end of time. how can I use that for show the time finished.
    – user60258
    Sep 3, 2017 at 8:34
  • Who can help me ?
    – user60258
    Sep 4, 2017 at 5:51
  • The error says it all - you are passing an array instead of a string. What is the value of node.field_timer.value?
    – Kevin
    Sep 13, 2017 at 14:04
  • @Kevin 's answer says it all. With a var_dump of node.field_timer.value {{ dump(node.field_timer.value)}} you should be able to identify the proper key to use for the filter date |date("U") Jan 15, 2019 at 13:24

1 Answer 1

0

You might use twig without method. It will print all configured content to be rendered without the selected fields/properties.

{% set a = 'now'|date("U") %}
{% set b = node.field_timer.value|date("U") %}
{% if a < b %}
    {# Show #}
    {{ content }}
{% else %}
    {# Hide - print the content without the field_timer #}
    {{ content|without('field_timer') }}
{% endif %}

For some cacheability issues it is recommended to always print content.

1
  • Thanks but read my question and comment.I use this but worked not correctly and when go to first page I have an error.
    – user60258
    Sep 3, 2017 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.