You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:
    /webform/your-form-machine-name?job_title=[node:title]
    
    If your button is part of a Views block or custom template, make sure the token [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_title works well) in your Webform. Under the field's Default Value settings, select "URL query parameter" and enter job_title as 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:
    *Applied Job Position:* [webform_submission:values:job_title]
    
    Now every submission email will show the exact job the user applied for.

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 node so 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:
    *Applied Job Position:* [webform_submission:context:node:title]
    
    This pulls the title straight from the linked job node, keeping your form clean.

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 .module file:
    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_title hidden 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:14:24