I had been pulling my hair out on this one for a couple of hours, as usual with Drupal.
Within a TWIG template, I wanted to check the current URL for the presence of a certain string and include a webform if the string was found.
Now this is easy to do using the native block system or also in the Context module, but that particular template was displaying programmed elements, and my webform had to go between them.
the first thing of course is to use the TWIG tweak module (I consider this essential in any Drupal site).
and then there is the question of the current URL. there are different answers to this question on the internet - I tried most out, but this one finally worked:
{% set url = url('<current>') %}
{% if 'STRING-TO-TEST-FOR' in url|render|render %}
<div>{{ drupal_entity('webform', 'WEBFORM-MACHINE-NAME') }}</div>
{% endif %}
no idea why |render|render has to be included, but there it is. of course, replace WEBFORM-MACHINE-NAME with the machine name of your webform.
the above code works for anything that can be included with TWIG tweak, blocks, views, etc.
- Log in to post comments
Comments