Drupal 8中如何将页面节点标题传递至Webform
Hey there, let's sort out this issue where you can't tell which specific job an applicant is applying for in your Drupal 8 Webform emails. I’ve tackled this exact scenario before, so here are a few reliable solutions to get it working:
1. Pass the Job Title via URL Query Parameter (Quickest Fix)
This is the most straightforward approach, perfect if you just need to get the job title into your emails without extra complexity:
- Update the apply button link: Instead of linking directly to your Webform, add a query parameter that pulls the current job node's title using Drupal tokens. For example, set the button URL to:
If your button is part of a Views block or custom template, make sure the token/webform/your-form-machine-name?job_title=[node:title][node:title]is properly resolved to the current job page's title. - Add a hidden field to your Webform: Create a new hidden field (machine name:
job_titleworks well) in your Webform. Under the field's Default Value settings, select "URL query parameter" and enterjob_titleas the parameter name. This auto-fills the field with the job title from the URL—users won’t see it, but the data gets submitted with their form. - Include the field in your email template: Edit your Webform’s email settings, and in the message body, add something like:
Now every submission email will show the exact job the user applied for.*Applied Job Position:* [webform_submission:values:job_title]
2. Use Webform Context Entities (More Elegant for Node-Linked Forms)
If your jobs are Drupal content nodes, you can link the Webform directly to the job node to pull data without extra fields:
- Enable Webform context: Edit your Webform, go to Settings > Form Settings, scroll to the "Context" section, check "Enable context", then add a Node context. Configure it to "Load from URL parameter" (set the parameter to
nodeso it uses the job node's ID). - Update the apply button link: Adjust the button URL to pass the job node's ID:
/webform/your-form-machine-name?node=[node:nid] - Call the node title directly in your email: No need for a hidden field—just add this to your email message body:
This pulls the title straight from the linked job node, keeping your form clean.*Applied Job Position:* [webform_submission:context:node:title]
3. Custom Module Hook (For Complex Logic)
If you need more control (like handling edge cases or additional data), use a custom Drupal hook to inject the job title into the submission:
- Create a small custom module, then add this code to your
.modulefile:function your_custom_module_webform_submission_presave(\Drupal\webform\WebformSubmissionInterface $webform_submission) { // Grab the job title from the URL query parameter $request = \Drupal::request(); $job_title = $request->query->get('job_title'); // If we have a valid title, save it to the submission (use your field's machine name) if (!empty($job_title)) { $webform_submission->setElementData('job_title', $job_title); } } - Make sure you have a
job_titlehidden field in your Webform, then reference it in your email template just like in method 1.
Pro Tip
Always test each solution by clicking apply buttons on different job pages, submitting the form, and verifying the email includes the correct job title. It’s easy to miss a token or parameter name, so a quick test saves headaches later!
内容的提问来源于stack exchange,提问作者Gilles Knockaert

