Tags/topics: 
1
Answers
Vote up!
0
Vote down!

How to add a field to review page?

How could i add a custom field to review page if i dont want it to be part of order data?

I would like to add a checkbox to review page and store value of it for a user related data. I am using the Site Disclaimer module and i would do function of it on review page so i need a simple checkbox without using pane.

What i tryed is the following:

<?php
/**
 * Implements hook_form_FORM_ID_alter().
 */
function MYMODULE_form_commerce_checkout_form_review_alter(&$form, &$form_state) {
       
$form['I_agree'] = array(
           
'#type' => 'checkbox',
           
'#title' => $checkbox_label,
           
'#required' => TRUE,
           
// The following adds "required" validation to the checkbox (patches core missing functionality as of D6.16)
           
'#element_validate' => array('_site_disclaimer_validate_checkbox'),
           
'#return_value' => variable_get('site_disclaimer_version', 1),
           
'#weight' => 0,
        );
}
?>

I tryed to add a required checkbox with review form alter but when the checkbox is not filled and the form is submitted there wont be any error message(cos it is a required field.). I have noticed one interesting thing. Somebody suggested me to use pane for this. I tryed his code with an example required textfield. Both of them were(checkbox as above and the textfield in pane) on form. When none of them were filled and i have hit the submit button all fields were validated and there were error messages, but without pane nothing happenes at all.

So how could i add a custom field to review page without having to do a pane version? And where could i add my submit function to checkbox? Thx in advance!

Asked by: Bela Marai
on June 14, 2014

1 Answer

Vote up!
1
Vote down!

Meantime i found a solution:

<?php
function MYMODULE_form_commerce_checkout_form_review_alter(&$form, &$form_state) {
       
$form['buttons']['I_agree'] = array(
           
'#type' => 'checkbox',
           
'#title' => $checkbox_label,
           
'#required' => TRUE,
        );

       
$form['buttons']['continue']['#validate'] = array_merge(array('MYMODULE_site_disclaimer_validate'), $form['buttons']['continue']['#validate']);

       
$form['buttons']['continue']['#submit'] = array_merge(array('MYMODULE_site_disclaimer_submit'), $form['buttons']['continue']['#submit']);

}

/**
* Validation function for site disclaimer checkbox.
*/
function MYMODULE_site_disclaimer_validate($form, &$form_state) {
   
$element = $form['buttons']['I_agree'];
   
$value = $form_state['values']['I_agree'];

   
// @see _form_validate() in form.inc, line 1393.
   
if (isset($value) || (!empty($element['#required']))) {
       
// A simple call to empty() will not cut it here as some fields, like
        // checkboxes, can return a valid value of '0'. Instead, check the
        // length if it's a string, and the item count if it's an array.
        // An unchecked checkbox has a #value of integer 0, different than string
        // '0', which could be a valid value.
       
$is_empty_multiple = (!count($value));
       
$is_empty_string = (is_string($value) && drupal_strlen(trim($value)) == 0);
       
$is_empty_value = ($value === 0);
        if (
$is_empty_multiple || $is_empty_string || $is_empty_value) {
           
// Although discouraged, a #title is not mandatory for form elements. In
            // case there is no #title, we cannot set a form error message.
            // Instead of setting no #title, form constructors are encouraged to set
            // #title_display to 'invisible' to improve accessibility.
           
if (isset($element['#title'])) {
               
form_error($element, t('!name field is required.', array('!name' => $element['#title'])));
            }
            else {
               
form_error($element);
            }
           
//see commerce_checkout_form_validate().
           
$form_state['rebuild'] = TRUE;
        }
    }
}
?>
Answer by: Bela Marai
Posted: Jun 19, 2014