I have a webform which has a field for email address. I am embedding the webform on each node, with a field on the node for the email address.
There is a form_alter function in my .theme file that loads the node using:
$node = Drupal::routeMatch()->getParameter('node');
I can then load the node and get the proper email address and set it in the webform.
The problem is that the node is cached for anonymous users. It doesn’t matter what node I’m on, echo $node->id(); always returns the same, cached node id.
I have tried:
Drupal::service('page_cache_kill_switch')->trigger();
and
$vars['#cache']['max-age'] = 0;
But it does not matter — that node id is always the same.
I understand that I have to clear cache tags and cache context, but none of the examples show exactly how to do this within form_alter in my theme file.
Can anyone help me clear this cache properly?
Thanks!