0

I have a menu path which rendered User Registration Form. The problem is I did hide fields like "Email", "password" and "status" by setting #access to FALSE. It is not working.

When I have done the same thing in hook_form_alter(), it does work.

This is my code.

/**
 * Implements hook_menu().
 */
function MY_MODULE_menu() {
  $items['admin/author/create'] = array(
    'title' => t('Author'),
    'page callback' => 'MY_MODULE_author_form',
    'type' => MENU_CALLBACK,
    'access callback' => TRUE,
  );

  return $items;
}

/**
 * Author form.
 */
function MY_MODULE_author_form() {
  drupal_set_title('Create Author');

  // Getting User Register Form
  $form = drupal_get_form('user_register_form'); 

  // Change Username to Author Name
  $form['account']['name']['#title'] = 'Author Name';

  // Exclude User Registeration Elements
  $form['account']['mail']['#access'] = FALSE;
  $form['account']['pass']['#access'] = FALSE;
  $form['account']['status']['#access'] = FALSE;

  // The path to which the form will be submitted.
  $form['#action'] = url('admin/people/create'); 

  return $form;
}

I think I can alter the form in MY_MODULE_author_form().

Is there anything wrong with my code?

0

Follow Drupal's standards and change your hook_menu:

/**
 * Implements hook_menu().
 */
function MY_MODULE_menu() {
  $items['admin/author/create'] = array(
    'title' => t('Author'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('MY_MODULE_author_form'),
    'type' => MENU_CALLBACK,
    'access callback' => TRUE,
  );

  return $items;
}
0

As Mohammad mentioned, you could set drupal_get_form as page callback in your hook_menu.
You would not need to create your own page arguments. You just need to set it to user_registration_form. It will render user registration form at your new menu path.

Assuming you have implemented hook_form_alter, you can alter user registration form upon the different path.
I think you don't need $form['#action']. User registration form will be submitted as usual.

/*
 * Implementation of hook_menu()
 */
function MY_MODULE_menu() {
    $items['admin/author/create'] = array(
        'title' => t('Author'),
        'page callback' => 'drupal_get_form',
        'page arguments' => array('user_register_form'),        
        'access callback' => TRUE,
        'type' => MENU_NORMAL_ITEM,
        'weight' => 0,
    );
    return $items;
}


/**
 * Implementation of hook_form_alter
 * Override form structure
*/
function MY_MODULE_form_alter(&$form, &$form_state, $form_id) {
    if($form_id == 'user_register_form'){       
        elseif($_GET['q'] == 'admin/author/create') { 
            # user registration form invoked by your new menu path
            # change your fields as your need here
        }   
        if($_GET['q'] == 'admin/people/create') { 
            # user registraion form when logged in as administrator
            # change your fields as your need here
        }
        elseif($_GET['q'] == 'user/register') {
            # user registration form for anonymous user
            # change your fields as your need here
        }
    }   
}
1
  • I've already tried form alteration in hook_form_alter(). I just want to know why I can't alter form elements in my call back MY_MODULE_author_form(). Any idea ?
    – user8887
    Nov 30 '12 at 5:08

Your Answer

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