0

I have a webform select field called tour and I have set up an email to go out, using the default template:

Submitted on [submission:date:long]
Submitted by user: [submission:user]
Submitted values are:
[submission:values]

The results of this submission may be viewed at:
[submission:url]

When the email goes out, it sends the key (nid):

Tour: 8349

rather than the value of the option:

Tour: 10-Jan-16 - Awesome Tour

I have tried the following tokens:

[submission:values:tour:label]
[submission:values:tour:value]
[submission:values:tour:withLabel]

I am looking for the proper token to output the value in the key/value pair of a select field.

For the tour field, I am replacing the options via a hook. I only mention this step for completeness sake as I don't think it has any impact on my issue.

function MODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'FORM_ID') {

    // get all tours
    $query = db_select('node', 'n');
    $query->fields('n', array('nid', 'title'));
    $query->condition('n.type', 'tour');
    $query->join('field_data_field_tour_type', 't', 't.entity_id = n.nid');
    $query->fields('t', array('field_tour_type_value'));
    $result = $query->execute();

    // group by tour type
    $options = array();
    while ($row = $result->fetchObject()) {

      // tour type 1
      if ($row->field_tour_type_value == 1) {
        $options['Type 1'][$row->nid] = date('d-M-y', strtotime($row->field_event_date_value)) . ': ' . $row->title;
      }

      // tour type 2
      else if ($row->field_tour_type_value == 2) {
        $options['Type 2'][$row->nid] = date('d-M-y', strtotime($row->field_event_date_value)) . ': ' . $row->title;
      }
    }

    ksort($options);

    // set the tour options correctly
    $form['submitted']['tour']['#options'] = $options;
  }
}

1 Answer 1

0

It turns out the issue was with the form alter hook. I had updated the #options which made the form work just fine, however I needed to update the webform component with a line delimited list of key|value pairs.

function MODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'FORM_ID') {

    // get all tours
    $query = db_select('node', 'n');
    $query->fields('n', array('nid', 'title'));
    $query->condition('n.type', 'tour');
    $query->join('field_data_field_tour_type', 't', 't.entity_id = n.nid');
    $query->fields('t', array('field_tour_type_value'));
    $result = $query->execute();

    // group by tour type
    $options = array();
    $flat_options = array();
    while ($row = $result->fetchObject()) {

      // tour type 1
      if ($row->field_tour_type_value == 1) {
        $options['Type 1'][$row->nid] = date('d-M-y', strtotime($row->field_event_date_value)) . ': ' . $row->title;
        $flat_options .= $row->nid . '|' . date('d-M-y', strtotime($row->field_event_date_value)) . ': ' . $row->title . "\n";
      }

      // tour type 2
      else if ($row->field_tour_type_value == 2) {
        $options['Type 2'][$row->nid] = date('d-M-y', strtotime($row->field_event_date_value)) . ': ' . $row->title;
        $flat_options .= $row->nid . '|' . date('d-M-y', strtotime($row->field_event_date_value)) . ': ' . $row->title . "\n";
      }
    }

    ksort($options);

    // set the tour options correctly
    $form['submitted']['tour']['#options'] = $options;
    $form['#node']->webform['components'][7]['extra']['items'] = $flat_options;
  }
}

I've added the $flat_options variables to handle this and assigned it to the proper field in my form array. Keep in mind referencing the component by number [7] is not the cleanest and most maintainable way to do this but it works in a pinch.

Your Answer

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

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