You are here

function _webform_render_optionsmarkup in Webform Options Markup 7

Same name and namespace in other branches
  1. 7.2 components/webform_optionsmarkup.inc \_webform_render_optionsmarkup()

Implements _webform_render_component().

File

components/webform_optionsmarkup.inc, line 181
Webform component that allows markup in checkbox and radio options.

Code

function _webform_render_optionsmarkup($component, $value = NULL, $filter = TRUE) {
  $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
  $element = array(
    '#title' => $filter ? webform_optionsmarkup_filter_content($component['name']) : $component['name'],
    '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
    '#required' => $component['mandatory'],
    '#weight' => $component['weight'],
    '#description' => $filter ? webform_optionsmarkup_filter_content($component['extra']['description'], $node) : $component['extra']['description'],
    '#theme_wrappers' => array(
      'webform_element',
    ),
    // Needed to disable double-wrapping of radios and checkboxes.
    '#pre_render' => array(),
    '#translatable' => array(
      'title',
      'description',
      'options',
    ),
  );

  // Convert the user-entered options list into an array.
  $default_value = $filter ? _webform_filter_values($component['value'], $node, NULL, NULL, FALSE) : $component['value'];
  $options = _webform_optionsmarkup_options($component, !$component['extra']['aslist'], $filter);
  if (isset($component['extra']['optrand']) && $component['extra']['optrand']) {
    _webform_shuffle_options($options);
  }

  // Set the component options.
  $element['#options'] = $options;

  // Set the default value.
  if (isset($value)) {
    if ($component['extra']['multiple']) {

      // Set the value as an array.
      $element['#default_value'] = array();
      foreach ((array) $value as $option_value) {
        $element['#default_value'][] = $option_value;
      }
    }
    else {

      // Set the value as a single string.
      $element['#default_value'] = '';
      foreach ((array) $value as $option_value) {
        $element['#default_value'] = $option_value;
      }
    }
  }
  elseif ($default_value !== '') {

    // Convert default value to a list if necessary.
    if ($component['extra']['multiple']) {
      $varray = explode(',', $default_value);
      foreach ($varray as $v) {
        $v = trim($v);
        if ($v !== '') {
          $element['#default_value'][] = $v;
        }
      }
    }
    else {
      $element['#default_value'] = $default_value;
    }
  }
  elseif ($component['extra']['multiple']) {
    $element['#default_value'] = array();
  }
  if ($component['extra']['multiple']) {

    // Set display as a checkbox set.
    $element['#type'] = 'checkboxes';
    $element['#theme_wrappers'] = array_merge(array(
      'checkboxes',
    ), $element['#theme_wrappers']);
    $element['#process'] = array_merge(element_info_property('checkboxes', '#process'), array(
      'webform_optionsmarkup_expand_select_ids',
    ));

    // Entirely replace the normal expand checkboxes with our custom version.
    // This helps render checkboxes in multipage forms.
    $process_key = array_search('form_process_checkboxes', $element['#process']);
    $element['#process'][$process_key] = 'webform_optionsmarkup_expand_checkboxes';
  }
  else {

    // Set display as a radio set.
    $element['#type'] = 'radios';
    $element['#theme_wrappers'] = array_merge(array(
      'radios',
    ), $element['#theme_wrappers']);
    $element['#process'] = array_merge(element_info_property('radios', '#process'), array(
      'webform_optionsmarkup_expand_select_ids',
    ));

    // Entirely replace the normal expand radios with our custom version.
    $process_key = array_search('form_process_radios', $element['#process']);
    $element['#process'][$process_key] = 'webform_optionsmarkup_process_optionsmarkup_radios';
  }
  return $element;
}