I am creating a page where I have a view listing about 100 nodes of a specific content type and then underneath that I have a contact form. When visitors (logged out users) come to my site I would like them to be able to select some nodes from this view that they like. Either with a checkbox next to each one of them or by some other flagging system. When they then complete my form the specific nodes they have chosen will be sent with the form submission.

Possible Solutions:

  1. Create a template file for the view and add in a checkbox in front of each node.
  2. Use the flags module and pass the selected nodes when submitting.

I've never done something like this before so any help would be appreciated!

  • No sorry. Not even close to what I want.
    – Dan
    May 3, 2014 at 4:46
  • It only means you failed to describe what you want. From what we see here and there it is the same problem. If you can ask it in a way that shows clearly how that other Q&A is not for your use case, please do so. I'll be happy to cast reopen vote. Or you can ask another question, just be careful to write it clearly, to avoid second closure.
    – Mołot
    May 4, 2014 at 17:54

1 Answer 1


Here is a solution with webform and flags:

1. Create a small module. You can create a very simple module for it - an info file and a module file with your functions. https://drupal.org/node/1074360

2. Implement hook_webform_submission_presave(). This will intercept the form data on the webform submission form submission form before it gets saved in the database.

3. Get the flags that the current user flagged with flag_get_user_flags(). You will receive an array of flags, that you can process as you like, add to form results array for later use, or, if you have a hidden text area called 'flags' in your form, insert that data there as a formatted text.

So, in totality, it will be something like this:

function leadflag_webform_submission_presave($node, &$submission) {
  $nodes_list = '';
  $title = '';
  // Get the flags.
  $flags = flag_get_user_flags('node');
  // Get the nodes.
  if (!empty($flags)) {
    $flags = $flags['flag_name'];
    foreach ($flags as $flag) {
        $nid = $flag->entity_id;
      // Get node data. Delete to not get node titles.
      $node = node_load($nid);
      $title = $node->title;
      // Combine data.
      $nodes_list .= 'node/' . $nid . ' ' . $title . ",\n";

  // Update some component's value before it is saved.
  $component_id = 4;
  $submission->data[$component_id][0] = $nodes_list;

This function gets flagged nodes and populates the webform component 4 with node/nids and titles, like this:

node/10 Node Title of node 10,
node/21 Node Title of node 21, etc

The function gets the flags and puts them in form component 4. You need to hover your mouse over your textarea component edit link on the webform components edit page to see what component number it actually is, in the edit link url. Replace the component number in code with your actual component number.

There is a section (lines 13-15), that loads nodes to get their titles. Doing node loads is somewhat an expensive function. It's Ok unless you expect dozens of submissions per minute though. But to not have node_loads, and to only have a list like 'node/10, node/15' without titles, you can delete these lines.

  • Thanks for the reply @Alexei, the Flags module does in fact work with anonymous users. Using the Sessions API you can add that feature. The question would then be how to send that info when they submit the form.
    – Dan
    Apr 26, 2014 at 15:13
  • @Dan: That is great news. Then you can get that data easily. I have updated my answer to include steps how to do it. Apr 27, 2014 at 5:15
  • Alexei would you be so kind to help me merge the flag information into my form? I was able to add the flag link to each of my view links. Flag machine name is "conference_leads" and I have generated a text field in my webform with the key "flag_capture". What would be the best way to transfer $flags into that field?
    – Dan
    May 2, 2014 at 1:52
  • @Dan: I have updated the code to use with flag and webform specifically. You should add a hidden textarea, and that code will populate it. Now, if you are asking about populating the textarea from flags in live time, that will either require AJAX, or some JQuery script. But given that the users don't need to see that list (since they have flags), I don't see any reason to do it via JQuery script. May 2, 2014 at 6:58
  • @Alexie: I really appreciate the help, but I am struggling to get webform_submission_presave to even work. I have tried to simply have that function change a field and it will not work. I just can't get it to change anything. Example: function THEME_webform_submission_presave($node, &$submission) { $component_id = 30; $submission->data[$component_id]['value'][0] = 'This is working'; }
    – Dan
    May 2, 2014 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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