You are here

function webform_expand_grid in Webform 7.4

Same name and namespace in other branches
  1. 6.3 components/grid.inc \webform_expand_grid()
  2. 7.3 components/grid.inc \webform_expand_grid()

A Form API #process function for Webform grid fields.

1 call to webform_expand_grid()
template_preprocess_webform_display_grid in components/grid.inc
Preprocess function for displaying a grid component.
1 string reference to 'webform_expand_grid'
_webform_render_grid in components/grid.inc
Implements _webform_render_component().

File

components/grid.inc, line 243
Webform module grid component.

Code

function webform_expand_grid($element) {
  $options = $element['#grid_options'];
  $questions = $element['#grid_questions'];
  $weights = array();

  // Process questions and options from nested components.
  foreach (element_children($element) as $key) {
    $question = $element[$key];

    // Both forms and grid displays have #webform_component.
    if (isset($question['#webform_component']) && $question['#webform_component']['type'] == 'select' && !$question['#webform_component']['extra']['aslist'] && !$question['#webform_component']['extra']['other_option']) {
      $options = webform_grid_merge_options($options, $question['#options']);
      $weights[$key] = $question['#weight'];
    }
  }

  // Add the internal grid questions.
  $weight = -1000;
  $value = isset($element['#default_value']) ? $element['#default_value'] : array();
  foreach ($questions as $key => $question) {
    if ($question != '') {
      $question_value = isset($value[$key]) && $value[$key] !== '' ? $value[$key] : NULL;
      $element[$key] = array(
        '#grid_question' => TRUE,
        '#title' => $question,
        '#required' => $element['#required'],
        '#options' => $element['#grid_options'],
        '#type' => 'radios',
        '#default_value' => $question_value,
        '#value' => $question_value,
        '#process' => array(
          'form_process_radios',
          'webform_expand_select_ids',
        ),
        // Webform handles validation manually.
        '#validated' => TRUE,
        '#webform_validated' => FALSE,
        '#translatable' => array(
          'title',
        ),
        '#weight' => $weight,
      );

      // Add HTML5 required attribute, if needed.
      if ($element['#required']) {
        $element[$key]['#attributes']['required'] = 'required';
      }
      $weights[$key] = $weight;
      $weight++;
    }
  }
  if (!empty($element['#optrand'])) {
    _webform_shuffle_options($options);
  }
  $element['#grid_options'] = $options;
  asort($weights);
  if (!empty($element['#qrand'])) {
    _webform_shuffle_options($weights);
  }
  if ($weights) {
    $weight = min($weights);
  }
  foreach ($weights as $key => $old_weight) {
    $element[$key]['#options'] = webform_grid_remove_options($options, $element[$key]['#options']);
    $element[$key]['#weight'] = $weight++;
    $element['#grid_questions'][$key] = $element[$key]['#title'];
  }
  return $element;
}