You are here

function theme_webform_display_grid in Webform 7.4

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

Format the text output for this component.

1 theme call to theme_webform_display_grid()
_webform_display_grid in components/grid.inc
Implements _webform_display_component().

File

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

Code

function theme_webform_display_grid($variables) {
  $element = $variables['element'];
  $format = $element['#format'];
  if ($format == 'html') {
    $right_titles = _webform_grid_right_titles($element);
    $rows = array();

    // Set the header for the table.
    $header = _webform_grid_header($element, $right_titles);
    foreach (element_children($element) as $question_key) {
      $question_element = $element[$question_key];
      $row = array();
      $questions = explode('|', $question_element['#title'], 2);
      $values = $question_element['#value'];
      $values = is_array($values) ? $values : array(
        $values,
      );
      $row[] = array(
        'data' => webform_filter_xss($questions[0]),
        'class' => array(
          'webform-grid-question',
        ),
      );
      if (isset($element['#grid_questions'][$question_key])) {
        foreach ($element['#grid_options'] as $option_value => $option_label) {
          if (in_array($option_value, $values)) {
            $row[] = array(
              'data' => '<strong>X</strong>',
              'class' => array(
                'checkbox',
                'webform-grid-option',
              ),
            );
          }
          else {
            $row[] = array(
              'data' => '&nbsp;',
              'class' => array(
                'checkbox',
                'webform-grid-option',
              ),
            );
          }
        }
      }
      else {
        $question_element['#title_display'] = 'none';
        $row[] = array(
          'data' => drupal_render($question_element),
          'colspan' => count($element['#grid_options']),
        );
      }
      if ($right_titles) {
        $row[] = array(
          'data' => isset($questions[1]) ? webform_filter_xss($questions[1]) : '',
          'class' => array(
            'webform-grid-question',
          ),
        );
      }
      $rows[] = $row;
    }
    $option_count = count($header) - 1;
    $output = theme('table', array(
      'header' => $header,
      'rows' => $rows,
      'sticky' => $element['#sticky'],
      'attributes' => array(
        'class' => array(
          'webform-grid',
          'webform-grid-' . $option_count,
        ),
      ),
    ));
  }
  else {
    $items = array();
    foreach (element_children($element) as $question_key) {
      $question_element = $element[$question_key];
      if (isset($element['#grid_questions'][$question_key])) {
        $values = $question_element['#value'];
        $values = is_array($values) ? $values : array(
          $values,
        );
        foreach ($values as $value_key => $value) {
          if (isset($element['#grid_options'][$value])) {
            $values[$value_key] = $element['#grid_options'][$value];
          }
          else {
            unset($values[$value_key]);
          }
        }
        $value = implode(', ', $values);
      }
      else {
        $element[$question_key]['#title'] = '';
        $value = drupal_render($element[$question_key]);
      }
      $items[] = ' - ' . _webform_grid_question_header($question_element['#title']) . ': ' . $value;
    }
    $output = implode("\n", $items);
  }
  return $output;
}