0

I'm building a conference registration site in Drupal 8. I've set up a register link for a custom user role that includes a query string (/register?reg-id=custom-id) and would like to pass this value (custom-id) back to a hidden field when my form is displayed to my user so that I can have users that are of the same role but with varying reg-id depending what they signed up for.

Ideally I would like to set it up using the user fields available under account settings and not through the template settings as more than one template may be used in the future (on a per conference basis). Alternatively a module might work, but I can't seem to find a starting point for this in Drupal 8. As a side note the custom-id is generated from a taxonomy term field on the referring page - just in case there an option to feed the form the variable another way.

I have looked at the following, and numerous others, but they don't quite meet the requirements for my use case:
How to retrieve GET parameter in Twig template
https://www.drupal.org/project/userreference_url (seems ideal but not ported to D8)
How do I pass a value from the url to a custom field in the registration form? (for D7)

Any assistance would be greatly appreciated.

1

You may be able to do a hook_form_alter() and call \Drupal::request()->query->all() (or \Drupal::request()->query->get('reg-id')) to access the query parameters to pass it to your hidden form field.

See https://api.drupal.org/api/drupal/vendor%21symfony%21http-foundation%21Request.php/property/Request%3A%3Aquery/8.2.x and https://www.drupal.org/node/2150267.

For example:

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_alter().
 */
function example_form_alter(&$form, FormStateInterface $form_state) {
  // Get the query parameter with 'reg-id' if it exists.
  if ($custom_id = \Drupal::request()->query->get('reg-id')) {
    $form['hidden_field'] = // Do whatever you need here;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.