2

I'm creating a webform with multiple pages. On the first page, I ask for "Name" (textfield), and on the second page, I would like to have a markup component that incorporates the value entered for "Name" on the first page.

I have tried using webform's token system in Webform 3.x and the new token functionality in Webform 4.x, as well as hook_form_alter(), all to no avail.

In regard to the later, in glue.module I defined:

function glue_form_alter(&$form, &$form_state, $form_id) {
  if (preg_match("/webform/i", $form_id)) {
   dsm($form);
  }
}

and the resulting array displays the "name" component value (from page one) as empty on page two.

Would someone please help me?

3

For one I would be wary of matching $form_id like that. It means you will be altering any webform you might add in future. Plus it is possible for other form id's to contain webform, for example webform_node_form, which is the node add/edit form for the webform content type. There is also webform_components_form, webform_component_edit_form and probably lots of others.

If you really want to regex it I would use webform_client_form_\d+

However if I wanted to do all webforms I would use hook_form_BASE_FORM_ID_alter(), like this:

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function hook_form_webform_client_form_alter(&$form, &$form_state, $form_id) {

}

and to do just one webform I would use hook_form_FORM_ID_alter(), like this:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function hook_form_webform_client_form_922_alter(&$form, &$form_state) {

}

Now for your actual question, you can accomplish this in form alter:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function hook_form_webform_client_form_922_alter(&$form, &$form_state) {
  // If we are on the second page.
  if ($form['details']['page_num']['#value'] == 2) {
    // You can get the value via the submitted_tree using the field key.
    $name = $form_state['values']['submitted_tree']['name_field_key'];
    // However I would recommend instead using submitted.
    // The reason is that with fieldsets the submitted_tree gets more complex,
    // and also, users can change the field key (who knows why that's allowed)
    // so that is unreliable. The submitted array uses the webform field cid
    // so it won't change.
    // (1 is the id of my name field)
    $name = $form_state['values']['submitted'][1];

    // There are 2 main ways you can add the markup.
    // 1. If you actually have a webform markup component on the second page
    // and you want to set its value. This means you can control the field via
    // the webform UI.
    // I use check plain because this is data entered by the user and needs to
    // be sanitised. You could also use filter_xss() if the field might
    // contain HTML.
    $form['submitted']['markup_field_key']['#markup'] = '<div class="my-markup-field">' . check_plain($name) . '</div>';
    // 2. If you don't have a webform markup component and just want to add
    // markup in, you can do this, just using regular form API.
    $form['submitted']['my_markup_field'] = array(
      // Defaults to type #markup.
      '#markup' => '<div class="my-markup-field">' . check_plain($name) . '</div>',
      '#weight' => 10,
    );
  }
}

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.