You are here

function _webform_conditional_expand in Webform 7.4

Form API #process function to expand a webform conditional element.

1 call to _webform_conditional_expand()
webform_conditional_expand in ./webform.module
Form API #process function to expand a webform conditional element.

File

includes/webform.conditionals.inc, line 354
Form elements and menu callbacks to provide conditional handling in Webform.

Code

function _webform_conditional_expand($element) {
  $default_operator = 'and';
  $element['#tree'] = TRUE;
  $element['#default_value'] += array(
    'andor' => $default_operator,
  );
  $wrapper_id = drupal_clean_css_identifier(implode('-', $element['#parents'])) . '-ajax';
  $element['#prefix'] = '<div id="' . $wrapper_id . '">';
  $element['#suffix'] = '</div>';
  $element['#wrapper_id'] = $wrapper_id;

  // Note: When rules or actions are added, the new rules are inserted into
  // $form_state['values']. So that FAPI can merge data from the post,
  // $form_state['input'] must be adjusted to. To make this easier, hidden
  // fields are added to the conditional_start and _end rules to ensure that
  // each rule is represented in the POST.
  $level = 0;
  $andor_stack[0] = array(
    'value' => $element['#default_value']['andor'],
    'parents' => array_merge($element['#parents'], array(
      'andor',
    )),
    'rid' => 0,
    'first' => TRUE,
  );
  $last_rid = -1;
  foreach ($element['#default_value']['rules'] as $rid => $conditional) {
    switch ($conditional['source_type']) {
      case 'conditional_start':
        $element['rules'][$rid] = array(
          '#level' => $level,
          'source_type' => array(
            '#type' => 'hidden',
            '#value' => 'conditional_start',
          ),
          // The andor operator is located in the first child, which is
          // guaranteed to exist. Therefore, don't add a 'value' element here.
          'add_subconditional' => _webform_conditional_add_expand($element, $rid, TRUE),
          'add' => _webform_conditional_add_expand($element, $rid, FALSE),
          'remove' => _webform_conditional_remove_expand($element, $rid),
        );
        $andor_stack[++$level] = array(
          'value' => isset($conditional['operator']) ? $conditional['operator'] : $default_operator,
          'parents' => array_merge($element['#parents'], array(
            'rules',
            $rid,
            'operator',
          )),
          'rid' => $rid,
          'first' => TRUE,
        );
        break;
      case 'conditional_end':
        --$level;
        $element['rules'][$rid] = array(
          '#level' => $level,
          'source_type' => array(
            '#type' => 'hidden',
            '#value' => 'conditional_end',
          ),
          'add_subconditional' => _webform_conditional_add_expand($element, $rid, TRUE),
          'add' => _webform_conditional_add_expand($element, $rid, FALSE),
          'remove' => _webform_conditional_remove_expand($element, $rid),
          'andor' => _webform_conditional_andor_expand($andor_stack[$level]),
        );

        // Remove the last nested and/or.
        unset($element['rules'][$last_rid]['andor']);
        break;
      case 'component':
        $element['rules'][$rid] = _webform_conditional_rule_expand($element, $rid, $conditional, $level, $andor_stack[$level]);
        break;
      default:
        drupal_set_message(t('Unexpected conditional rule source type found (rule id @rid). Contact the administrator.', array(
          '@rid' => $rid,
        )), 'error');
    }
    $last_rid = $rid;
  }

  // Remove the last and/or.
  unset($element['rules'][$rid]['andor']);
  foreach ($element['#default_value']['actions'] as $aid => $action) {
    $element['actions'][$aid] = _webform_conditional_action_expand($element, $aid, $action);
  }
  return $element;
}