From 3d5948fe059189c5ee130c60fc168b0c5744114a Mon Sep 17 00:00:00 2001
From: Dan Chadwick <dan899@gmail.com>
Date: Tue, 24 Jun 2014 16:57:25 -0400
Subject: [PATCH] Issued #1534788 by DanChadwick: Added default value to grid
 component.

---
 components/grid.inc   |   19 +++++++++++++++----
 components/select.inc |    5 +++++
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/components/grid.inc b/components/grid.inc
index 94327c1..80920dc 100644
--- a/components/grid.inc
+++ b/components/grid.inc
@@ -18,6 +18,7 @@ function _webform_defaults_grid() {
     'required' => 0,
     'pid' => 0,
     'weight' => 0,
+    'value' => '',
     'extra' => array(
       'options' => '',
       'questions' => '',
@@ -69,13 +70,13 @@ function _webform_edit_grid($component) {
     $form['options']['options'] = array(
       '#type' => 'options',
       '#options' => _webform_select_options_from_text($component['extra']['options'], TRUE),
-      '#optgroups' => FALSE,
-      '#default_value' => FALSE,
-      '#default_value_allowed' => FALSE,
+      '#default_value' => $component['value'],
+      '#default_value_allowed' => TRUE,
       '#optgroups' => FALSE,
       '#key_type' => 'mixed',
       '#key_type_toggle' => t('Customize option keys (Advanced)'),
       '#key_type_toggled' => $component['extra']['custom_option_keys'],
+      '#default_value_pattern' => '^%.+\[.+\]$',
     );
 
     $form['questions'] = array(
@@ -92,7 +93,6 @@ function _webform_edit_grid($component) {
       '#optgroups' => FALSE,
       '#default_value' => FALSE,
       '#default_value_allowed' => FALSE,
-      '#optgroups' => FALSE,
       '#key_type' => 'mixed',
       '#key_type_toggle' => t('Customize question keys (Advanced)'),
       '#key_type_toggled' => $component['extra']['custom_question_keys'],
@@ -123,8 +123,18 @@ function _webform_edit_grid($component) {
       '#wysiwyg' => FALSE,
       '#element_validate' => array('_webform_edit_validate_select'),
     );
+    $form['value'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Default value'),
+      '#default_value' => $component['value'],
+      '#description' => t('The default option of the grid identified by its key.') . ' ' . theme('webform_token_help'),
+      '#size' => 60,
+      '#maxlength' => 1024,
+      '#weight' => 1,
+    );
   }
 
+
   $form['display']['optrand'] = array(
     '#type' => 'checkbox',
     '#title' => t('Randomize Options'),
@@ -175,6 +185,7 @@ function _webform_render_grid($component, $value = NULL, $filter = TRUE) {
     '#description' => $filter ? webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
     '#grid_questions' => $questions,
     '#grid_options' => $options,
+    '#default_value' => $component['value'],
     '#optrand' => $component['extra']['optrand'],
     '#qrand' => $component['extra']['qrand'],
     '#theme' => 'webform_grid',
diff --git a/components/select.inc b/components/select.inc
index 1817b76..4592980 100644
--- a/components/select.inc
+++ b/components/select.inc
@@ -271,6 +271,11 @@ function _webform_edit_validate_options($element, &$form_state) {
   // Options saved for grid components.
   else {
     $form_state['values']['extra']['custom_' . rtrim($key, 's') . '_keys'] = $element_options['custom_keys'];
+    // There is only one 'value', but grids have two options widgets, one for questions and one for options.
+    // Only options have a default 'value'. Note that multiple selection is now allowed for grid options.
+    if ($key == 'options') {
+      $form_state['values']['value'] = $element_options['default_value'];
+    }
   }
 }
 
-- 
1.7.8.msysgit.0

