Created
July 29, 2015 00:37
-
-
Save St0iK/d910d705276608c2ddcb to your computer and use it in GitHub Desktop.
wow..
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Implements hook_form_alter(). | |
| */ | |
| function vw_shipping_messages_form_alter(&$form, &$form_state, $form_id) { | |
| if($form_id == 'commerce_checkout_pane_settings_form') { | |
| //echo "called"; | |
| $shipping_messages = variable_get('shipping_messages'); | |
| //exit; | |
| include_once DRUPAL_ROOT . '/includes/locale.inc'; | |
| $form['shipping_messages'] = array( | |
| '#type' => 'fieldset', | |
| '#weight' => -9999, | |
| '#title' => t('Shipping pane message'), | |
| "#prefix" => '<div id="form-shipping_messages">', | |
| "#suffix" => '</div>', | |
| '#collapsible' => FALSE, | |
| ); | |
| $form['shipping_messages']['#tree'] = TRUE; | |
| $form['shipping_messages']['entry_fieldset'] = array( | |
| '#type' => 'fieldset', | |
| '#title' => t('Message'), | |
| '#prefix' => '<div id="entry-fieldset-wrapper">', | |
| '#suffix' => '</div>', | |
| ); | |
| if (empty($form_state['instances'])) { | |
| if(!empty($shipping_messages)){ | |
| $form_state['instances'] = count($shipping_messages['messages']); | |
| }else{ | |
| $form_state['instances'] = 1; | |
| } | |
| } | |
| $form['#attached']['js'] = array( | |
| array( | |
| 'type' => 'file', | |
| 'data' => drupal_get_path('module', 'vw_shipping_messages') . '/js/reorder_fields.js', | |
| ), | |
| ); | |
| for ($i = 0; $i < $form_state['instances']; $i++) { | |
| $form['shipping_messages']['entry_fieldset']['countries'][$i] = array( | |
| '#type' => 'select', | |
| '#title' => 'Select applicable countries', | |
| '#description' => t('Please select the coutries that this message will be applicable'), | |
| '#default_value' => $shipping_messages['countries'][$i], | |
| '#multiple' => true, | |
| '#required' => true, | |
| '#options' => country_get_list() | |
| ); | |
| $form['shipping_messages']['entry_fieldset']['messages'][$i] = array( | |
| '#type' => 'textarea', | |
| '#default_value' => $shipping_messages['messages'][$i], | |
| '#title' => '', | |
| '#required' => true, | |
| '#description' => t('Shipping message for selected countries'), | |
| ); | |
| $form['shipping_messages']['entry_fieldset']['remove_message'][$i] = array( | |
| '#type' => 'submit', | |
| '#name' => 'submit_'.$i, | |
| '#value' => t('Remove'), | |
| '#submit' => array('messages_remove'), | |
| '#attributes' => array( | |
| 'class' => array('messages_remove_'.$i,'messages_remove'), | |
| 'style' => array("margin-bottom:75px;") | |
| ), | |
| '#limit_validation_errors' => array() | |
| ); | |
| } | |
| $form['shipping_messages']['entry_fieldset']['add_message'] = array( | |
| '#type' => 'submit', | |
| '#value' => t('Add another message'), | |
| '#submit' => array('messages_add_more_add_one'), | |
| '#ajax' => array( | |
| 'callback' => 'messages_add_more_callback', | |
| 'wrapper' => 'entry-fieldset-wrapper', | |
| ), | |
| ); | |
| $form['submit']['#submit'][] = 'simple_form_submit'; | |
| // echo "<pre>"; | |
| // print_r($form); | |
| // echo "</pre>"; | |
| // exit; | |
| return $form; | |
| } | |
| } | |
| // --------------- AJAX CALLBACK FUNCTIONS FOR TEXT FIELD----------------- | |
| function messages_add_more_add_one($form, &$form_state) { | |
| if (!isset($form_state['instances'])) { | |
| $form_state['instances'] = 0; | |
| $form_state['instances']++; | |
| } | |
| $form_state['instances']++; | |
| $form_state['rebuild'] = TRUE; | |
| } | |
| function messages_remove($form, &$form_state) { | |
| $data = array(); | |
| $shipping_messages = variable_get('shipping_messages'); | |
| $key_index = $form_state['triggering_element']['#attributes']['class'][0]; | |
| $form_state['to_be_removed'] = substr($key_index, strrpos($key_index, '_') + 1); | |
| unset($shipping_messages['countries'][$form_state['to_be_removed']]); | |
| unset($shipping_messages['messages'][$form_state['to_be_removed']]); | |
| $data['messages'] = array_values($shipping_messages['messages']); | |
| $data['countries'] = array_values($shipping_messages['countries']); | |
| variable_set('shipping_messages',$data); | |
| //$form_state['instances'] = count($shipping_messages['messages']); | |
| //$form_state['rebuild'] = TRUE; | |
| } | |
| function messages_add_more_callback($form, $form_state) { | |
| return $form['shipping_messages']['entry_fieldset']; | |
| } | |
| function messages_remove_callback($form, $form_state) { | |
| return $form['shipping_messages']['entry_fieldset']; | |
| } | |
| function simple_form_submit($form, &$form_state) { | |
| $number_of_messages = COUNT($form_state['values']['shipping_messages']['entry_fieldset']['messages']); | |
| $data = array(); | |
| $data['messages'] = $form_state['values']['shipping_messages']['entry_fieldset']['messages']; | |
| $data['countries'] = $form_state['values']['shipping_messages']['entry_fieldset']['countries']; | |
| variable_set('shipping_messages',$data); | |
| } | |
| function my_module_my_form_clear(){ | |
| return true; | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment