Score:0

Drupal hubspot forms with Varnish

br flag

I have a Drupal 8 hubspot form built in a custom module which holds many fields among which a "Favorite Pizza" field coming from hubspot and this field is rendered as a required fieldset. The code is very long so I decided to explain to you the process instead:

  • In the Drupal buildForm function, we call an external hubspotbuildForm function
  • Inside this function, I collect the following cookie in php $_COOKIE['pizzaLover'], if this cookie holds the value of false, then the form fieldset Favorite Pizza is no longer shown and is no longer required

Hubspot renders the Drupal form without this field. It doesn't even show up in HTML.

Since the page with this Drupal form will be cached in Varnish, I assume that the buildForm function of Drupal will be executed only one time and hence it will call the hubspotbuildform only once, so if a user has $_COOKIE['pizzaLover'] equals to true, then the Favorite pizza fieldset will become visible and required. Then, if another user visits the page, even if his cookie is set to false, he will be able to see the Favorite Pizza field because the page is cached.

This is not what I want, so I chose instead to go to javacsript and execute a function on document ready to check if the cookie is set through JS, if this cookie is set to false then, I tried to remove the required property of this fieldset but it didn't work since the fieldset has embedded required html code. I tried the .remove() function to remove the html fieldset related to this field. The field went away but when I click on submit, the form is not submitting as if it was exepecting a value from me.

Any idea what to do? I want to remove the required field in javascript without blocking the submission.

Kevin avatar
in flag
Depending on your host, the name of the cookie may have to conform to a certain naming pattern to be passed through the caching layer to the application to be read. This is true of Acquia and Pantheon, for example. On top of that, unless the form is built with AJAX, you probably don't want this page to be cached.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.