Skip to content

Instantly share code, notes, and snippets.

@ewillhite
Created August 21, 2014 15:28
Show Gist options
  • Save ewillhite/59fe36744935e157f2e9 to your computer and use it in GitHub Desktop.
Save ewillhite/59fe36744935e157f2e9 to your computer and use it in GitHub Desktop.
Form Alter to move Drupal fields
<?php
/**
* Implements hook_form_FORMID_alter().
*/
// Checkout Form Alter
function seedco_form_commerce_checkout_form_checkout_alter(&$form, &$form_state, $form_id) {
// SECTION IN QUESTION
$account_section = $form['account'];
$form['customer_profile_billing']['commerce_customer_address']['und'][0]['account'] = $account_section;
$form['customer_profile_billing']['commerce_customer_address']['und'][0]['account']['#weight'] = -99;
unset($account_section);
$form['#submit'][] = 'seedco_checkout_form_submit';
// END SECTION IN QUESTION
// Set Checkout Page Title
drupal_set_title("An Invitation to Give");
// Add Secure Giving text
$form['cart_contents']['#prefix'] = "<span>Secure Giving</span>";
// Rename Fieldset Titles
$form['cart_contents']['#title'] = t("1. Select Your Gift");
$form['customer_profile_billing']['#title'] = t("3. Your Information");
$form['commerce_payment']['#title'] = t("4. Payment Information");
// Set Placeholders
$form['account']['login']['mail']['#attributes']['placeholder'] = t('E-mail Address');
$form['customer_profile_billing']['commerce_customer_address']['und'][0]['name_block']['first_name']['#attributes']['placeholder'] = t('First Name');
$form['customer_profile_billing']['commerce_customer_address']['und'][0]['name_block']['first_name']['#rules'][] = array('rule' => 'regexp[/^[a-zA-z\. ]*$/]', 'error' => 'Please, use only alphabet characters at %field.');
$form['customer_profile_billing']['commerce_customer_address']['und'][0]['name_block']['last_name']['#attributes']['placeholder'] = t('Last Name');
$form['customer_profile_billing']['commerce_customer_address']['und'][0]['name_block']['last_name']['#rules'][] = array('rule' => 'regexp[/^[a-zA-z\. ]*$/]', 'error' => 'Please, use only alphabet characters at %field.');
$form['customer_profile_billing']['commerce_customer_address']['und'][0]['street_block']['thoroughfare']['#attributes']['placeholder'] = t('Address 1');
$form['customer_profile_billing']['commerce_customer_address']['und'][0]['street_block']['premise']['#attributes']['placeholder'] = t('Address 2 (Optional)');
$form['customer_profile_billing']['commerce_customer_address']['und'][0]['locality_block']['locality']['#attributes']['placeholder'] = t('City');
$form['customer_profile_billing']['commerce_customer_address']['und'][0]['locality_block']['postal_code']['#attributes']['placeholder'] = t('Postal Code');
$form['customer_profile_billing']['field_telephone_']['und'][0]['value']['#attributes']['placeholder'] = t('Phone # (Optional)');
$form['customer_profile_billing']['commerce_customer_address'][LANGUAGE_NONE][0]['locality_block']['administrative_area']['#options'][''] = t("St");
// Next button
$form['customer_profile_billing']['field_telephone_'][LANGUAGE_NONE][0]['#suffix'] = "<div class='next'><a href='#' class='btn disabled'>Next</a></div>";
// Get Line Item (only first - would need to change if allowing multiples)
$lineItem = commerce_line_item_load($form_state['order']->commerce_line_items[LANGUAGE_NONE][0]['line_item_id']);
$form['commerce_payment']['payment_details']['credit_card']['number']['#attributes']['placeholder'] = t('Card number');
$form['commerce_payment']['payment_details']['credit_card']['code']['#attributes']['placeholder'] = t('Security code');
// If a OneVerse line item
if (isset($lineItem->field_one_verse) && $lineItem->field_one_verse[LANGUAGE_NONE][0]['value'] == 1) {
$form['commerce_payment']['payment_details']['recurring']['#default_value'] = 1;
$form['commerce_payment']['payment_details']['recurring']['#title'] = 'Bill Me Monthly (Required for OneVerse)';
$form['commerce_payment']['payment_details']['recurring']['#disabled'] = TRUE;
}
$product = commerce_product_load($lineItem->commerce_product[LANGUAGE_NONE][0]['product_id']);
if ($product->type == 'impact_gift') {
unset($form['commerce_payment']['payment_details']['recurring']);
}
}
// Attempt to reset field placement in submit (tried validate as well)
function seedco_checkout_form_submit(&$form, &$form_state) {
$form['account'] = $form['customer_profile_billing']['commerce_customer_address']['und'][0]['account'];
unset($form['customer_profile_billing']['commerce_customer_address']['und'][0]['account']);
return $form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment