12

I'm using Webform 4. It uses Drupal's tokens, not it's own % tokens like it used to in version 3, and pretty much all what I found was about webform 3.

Say I want to make multi-page subscription form. On first page there is only an email field. On second one, I want a markup that will say something like:

Detailed subscription configuration for molot@example.com

Similarly, I'd like to be able to use them as default values for future fields, like asking user for billing address, and then for delivery address.

Are there any tokens for that? What I did for now is:

function mymodule_token_info() {
  $type = array(
    'name' => t('Webform submitted values'),
    'description' => t('Tokens related to $_POST[\'submitted\'].'),
  );
  $tokens = array();
  if(isset($_POST['submitted'])) {
    foreach($_POST['submitted'] as $key => $val) { //discard $val now
      $tokens[$key] = array(
        'name' => $key,
        'description' => "\$_POST['submitted']['$key']",
      );
    }
  }
  return array(
    'types' => array('webform_submitted' => $type),
    'tokens' => array('webform_submitted' => $tokens),
  );
}

function mymodule_tokens($type, $tokens, $data, $options) {
  if($type == 'webform_submitted') {
    $replacements = array();
    foreach ($tokens as $name => $original) {
      //Hide errors below. We don't care if it really exists yet. If empty, so be it.
      @$replacements[$original] = (string)$_POST['submitted'][$name];
    }
    return $replacements;
  }
}

It works, but it only shows values from one step earlier, so requires some dirty tricks with hidden fields to carry email value across multiple steps. I admit - it was just quick and dirty. Is there any built-in way to do it, or properly maintained contributed module for this? Or a way to make this code work independently of $_POST['submitted'], to make it carry values across many steps and to make it's tokens (from available Webform elements) visible on token list in edit windows?

  • Could you set a session variable when you submit the page with the desired value on it? – Darvanen Jan 18 '15 at 22:19
  • @Darvanen possibly I could, but the value is already saved somewhere, right? So I would prefer to use what's already there. And I hardly believe I'm the first one to need this. Probably I just failed to find solution that's out there? – Mołot Jan 19 '15 at 7:33
  • I'm not sure about this $form_state['storage'] but I searched 2 or 3 posts they mentioned all data stored in $form_state['storage'] so I think you can access the data, source stackoverflow.com/questions/6408170/… and drupal.org/node/717750 – Bala Jan 19 '15 at 9:36
  • 2
    @Bala this works in all hook alter forms all right. But how would I get form state in tokens? I mean, I'm quitting this job in two weeks. Guy who will take care for this form is not a coder, he can do it but that's not his primary skill - that's why I need tokens. – Mołot Jan 19 '15 at 9:39
  • yeah you mentioned for future needs I forgot completely. – Bala Jan 19 '15 at 9:44
1

This does not answer the question in your title, but reading your use case it sounds like the scenario that Profile2 was written for. Related stackoverflow post

Webform is great for surveys and such, but when you want "to be able to use them as default values for future fields" they need to be connected to the user. So you would then need more custom code to fetch the user's historic webform submission to pull these values in a form alter for your billing address. Profile(2) is written precisely to make this easier.

Also, the less last-minute custom coding you are doing before handing this off to some one else, the less that some one else will curse you in their heart when they are trying to make updates.

  • All fine and nice, but one thing this form wasn't is a profile :( I mean, we didn't want to create profiles for people asking for subscriptions. Different laws apply when you do, more strict ones. Or so I was told by law department. Also, now I need it for totally unrelated use cases, if at all :) – Mołot Jul 13 '15 at 16:02
  • Okay, that is interesting what your legal team said. Bottom line, if you want to prepopulate forms with data connected to the user, you need data connected to the user. The Profile module was an easier way to maintain that data capability, and a pretty close match for the functions you were requesting. Change the site permissions so no one can view profiles, and it is now the same as what you want but with more-standard code. Just because the module is named Profile does not mean it has to be deployed as such. – DeveloperWeeks Jul 13 '15 at 20:57
0

Decalre the variables as global and pass from_state[values][] into them. This worked for me, in multi stage forms with ctools modal popup.

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.