I have this commerce order, and I want to hook into it when an administrator review the order (edit it to set it as complete, canceled, or whatever state).

I want at that moment to use a specific field to check using ajax if the name I'm typing in a specific field, already exists as a username.

So I checked out how it works the username_check module, and found that it does add that automatic checking using ajax. It does that check when the user types her desired username at the register form.

How may I adapt it in order to use that functionality for other fields?

The field machine name is field_commerce_username

I've looked into commerce_order.api, at hook_form_alter api, commerce_order_ui and specially commerce_order_ui_form_commerce_order_ui_order_form_alter in order to know what to do.

The username_check module uses hook_form_alter:

function username_check_form_user_profile_form_alter(&$form) {
  $mode     = variable_get('username_check_mode', 'auto');
  if($mode != 'off'){
    $form['account']['name']['#field_suffix'] = '<span id="profile-check-informer">&nbsp;</span>';
    $form['account']['name']['#suffix'] = '<div id="profile-check-message"></div>'; 

So, I thought in doing something like this:

function commerce_order_ui_form_commerce_order_ui_order_form_alter(&$form, &$form_state) {
  $mode     = variable_get('username_check_mode', 'auto');
  if($mode != 'off'){
    $form['element']['#field_name'] = 'commerce_customer_profile';

Now, if I try to add that example code to the username_check.module (just for testing purposes), I get this error:

Fatal error: Cannot redeclare commerce_order_ui_form_commerce_order_ui_order_form_alter() in /home/sitio/public_html/sites/all/modules/username_check/username_check.module on line 75

(75 line if where I declare my function).

I just need to be pointed to the right direction here, why is this error?

commerce_order_ui_form_commerce_order_ui_order_form_alter() wasn't declared before in the file... and it won't work even if I comment all _form_alter functions in the module.


I'm guessing by your rep that you already know most of the following but it pays to check:

  1. Hooks can only exist once per module
  2. Hooks must begin with the module name

So this means commerce_order_ui_form_commerce_order_ui_order_form_alter(&$form, &$form_state) won't work inside username_check.module

If you really want to put the code inside the username_check module call it:

username_check_form_commerce_order_ui_order_form_alter(&$form, &$form_state)


Of course, you shouldn't put test code inside an existing module. The best way to test code is to make your own custom module, which is pretty easy once you get the hang of it.

| improve this answer | |
  • And of course clear your cache before testing =) – Darvanen Dec 15 '14 at 5:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.