1

In some page I have inserted a link "Contact me for info" that links to a webform page. I would that some fields of the webform form are be pre-populated with the node title and node link from the previous page. How I do it?

1

Use the PrePopulate module and Tokens to build dynamic links on your pages. When clicked, the link's content will be entered into those fields automatically.

https://www.drupal.org/project/prepopulate

There's an example on the project page.

  • I must pass the node title of the current page of the link, with prepopulate I can pass only a value that I manually write in the url – kb8 Feb 14 '16 at 19:19
  • With the token module which you probably already have enabled, you can create the link using the current page's url. example.com/node/… You'd need to change the url and the title element but that's the idea. – Niall Murphy Feb 15 '16 at 1:34
  • As a sidenote, I'd add a hidden entity reference prepopulate field to the webform and fill it as well using the nid token just to help with admin so if you have lots of nodes, you have a link to the node. The entity reference prepopulate is slightly different.. Like "?nodeidfield=123". You put & in between each part then. – Niall Murphy Feb 15 '16 at 1:58
1

You could also do this with a little bit of code:

function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id) {
  // Check if the form is your webform, you could make it
  // more dynamic by checking the form title or something.
  if ($form_id == 'webform_client_form_1') {
    // Get the information from the previous page, the best option
    // to me seems to pass the node id via a parameter.
    $get = drupal_get_query_parameters();

    if (!empty($get['previous_page'])) {
      // Load the previous page.
      $previous_page = check_plain($get['previous_page']);
      $node = node_load($previous_page);

      // Check if the previous page is a valid node.
      if (!empty($node)) {
        // Enter the default values.
        $form['submitted']['YOUR_TITLE_FIELD']['#default_value'] = $node->title;
        $form['submitted']['YOUR_URL_FIELD']['#default_value'] = 'node/' . $node->nid;
      }
    }
  }
}

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.