function _webform_render_matrix in Webform Matrix Component 6
Same name and namespace in other branches
- 7.4 components/matrix.inc \_webform_render_matrix()
- 7 components/matrix.inc \_webform_render_matrix()
- 7.2 components/matrix.inc \_webform_render_matrix()
- 7.3 components/matrix.inc \_webform_render_matrix()
Implements _webform_render_component().
File
- components/
matrix.inc, line 314 - Webform module matrix component.
Code
function _webform_render_matrix($component, $value = NULL, $filter = TRUE) {
module_load_include('inc', 'webform', '/components/date');
$nid = $component['nid'];
$cid = $component['cid'];
$formkey = $component['form_key'];
$pid = $component['pid'];
$element = array();
$element['#weight'] = $component['weight'];
$element['#webform_component'] = $component;
$element['#attached']['css'][] = drupal_get_path('module', 'webform_matrix_component') . '/webform_matrix_component.css';
if ($component['extra']['title_display'] !== 'none') {
$element['#main_header'] = _webform_filter_xss($component['name']);
}
$datepicker = FALSE;
if (isset($component['extra']['matrix_col'])) {
$editvalue = unserialize($value[0]);
$matrix_col = $component['extra']['matrix_col'];
$matrix_row = $component['extra']['matrix_row'];
for ($prow = 1; $prow <= $matrix_row; $prow++) {
if (isset($component['extra']['element'])) {
$element['#headers'] = array();
foreach ($component['extra']['element'] as $elekey => $elevalue) {
list($j, $i) = explode('-', $elekey);
$eletype = $elevalue['type'];
$title = isset($elevalue['title']) ? $elevalue['title'] : $elekey;
$element['#headers'][] = $title;
$title_display = "none";
// Only first row is manadatory.
$required = FALSE;
if ($prow == 1) {
// If component is mandatory.
$required = $component['mandatory'] ? TRUE : FALSE;
if (!$required) {
// If particular element is manadatory.
$required = $elevalue['mandatory'] ? TRUE : FALSE;
}
}
switch ($eletype) {
case 'textfield':
$element[$prow][$i] = array(
'#type' => 'textfield',
'#title' => $title,
'#maxlength' => 50,
'#default_value' => $editvalue[$prow][$i],
'#required' => $required,
'#title_display' => $title_display,
'#size' => !empty($elevalue['size']) ? $elevalue['size'] : 15,
'#maxlength' => !empty($elevalue['maxlength']) ? $elevalue['maxlength'] : 50,
'#pre_render' => array(
'webform_element_title_display',
),
);
break;
case 'select':
//Choose Select Type Radio/ListBox/Checkbox
$select_type = isset($elevalue['select_type']) ? $elevalue['select_type'] : 'list';
$is_multiple = isset($elevalue['multiple']) ? $elevalue['multiple'] : 0;
//Options
$options = $elevalue['option'];
$option_array = array();
//Convert option to string to array
$option_array = _webform_matrix_component_select_option_from_text($options);
switch ($select_type) {
case 'list':
$element[$prow][$i] = array(
'#type' => 'select',
'#title' => $title,
'#options' => $option_array,
'#default_value' => $editvalue[$prow][$i],
'#required' => $required,
'#title_display' => $title_display,
'#empty_option' => 'Select',
'#empty_value' => '_none',
'#pre_render' => array(
'webform_element_title_display',
),
);
if ($is_multiple) {
$element[$prow][$i]['#multiple'] = TRUE;
}
break;
case 'radio':
//If multiple is TRUE then checkbox else radio
$element[$prow][$i] = array(
'#title' => $title,
'#options' => $option_array,
'#required' => $required,
'#title_display' => $title_display,
'#pre_render' => array(
'webform_element_title_display',
),
);
if ($is_multiple) {
$element[$prow][$i]['#type'] = 'checkboxes';
$element[$prow][$i]['default_value'] = is_array($editvalue[$prow][$i]) ? $editvalue[$prow][$i] : array(
$editvalue[$prow][$i],
);
}
else {
$element[$prow][$i]['#type'] = 'radios';
$element[$prow][$i]['default_value'] = $editvalue[$prow][$i];
}
break;
}
break;
case 'date':
$datepicker = isset($elevalue['datepicker']) ? $elevalue['datepicker'] : FALSE;
$element[$prow][$i] = array(
'#type' => 'date',
'#title' => $title,
'#title_display' => $title_display,
'#required' => $required,
'#start_date' => !empty($elevalue['startdate']) ? $elevalue['startdate'] : '-2 years',
'#end_date' => !empty($elevalue['enddate']) ? $elevalue['enddate'] : '+2 years',
'#year_textfield' => 0,
'#default_value' => !empty($editvalue[$prow][$i]) ? $editvalue[$prow][$i] : $elevalue['default_date'],
'#timezone' => 'user',
'#process' => array(
'webform_expand_date',
),
'#theme' => 'webform_date_matrix',
'#theme_wrappers' => array(
'webform_element',
),
'#element_validate' => array(
'webform_validate_date',
),
'#datepicker' => $datepicker,
'#pre_render' => array(
'webform_element_title_display',
),
);
break;
case 'label':
$label_name = $elevalue['label_name'];
$element[$prow][$i] = array(
'#type' => 'item',
'#title' => $title,
'#markup' => $label_name,
'#attributes' => array(
'class' => array(
'form-item',
),
),
'#default_value' => $editvalue[$prow][$i],
'#title_display' => $title_display,
'#pre_render' => array(
'webform_element_title_display',
),
);
break;
}
drupal_alter('webform_render_matrix', $component, $elevalue, $element[$prow][$i]);
}
}
}
}
if ($datepicker) {
$element['#attached']['library'] = array(
array(
'system',
'ui.datepicker',
),
);
}
$element['#theme'][] = 'webform_matrix';
$element_validate_array = array();
foreach (module_implements('webform_matrix_validate') as $module) {
$element_validate_array[] = $module . '_webform_matrix_validate';
}
if (!empty($element_validate_array)) {
$element['#element_validate'] = $element_validate_array;
}
return $element;
}