1

I'm creating an employee directory. I created a menu path which renders user registration form with profile fields. When I submit the form, I got some unknown errors. I did fill out every fields in the form, but drupal shows "required" errors.

Error
The website encountered an unexpected error. Please try again later.

  • Password field is required.
  • You must enter a username.
  • You must enter an e-mail address.
  • Notice: Undefined index: array_parents in field_default_form_errors() (line 369 of D:\fakepath\testsite\modules\field\field.form.inc).
  • Recoverable fatal error: Argument 2 passed to drupal_array_get_nested_value() must be an array, null given, called in D:\fakepath\testsite\modules\field\field.form.inc on line 369 and defined in drupal_array_get_nested_value() (line 6490 of D:\fakepath\testsite\includes\common.inc).

I have hook_form_alter in which some fields are made hidden. Here is my code snippet in my custom module.

<?php
/*
 * Implementation of hook_menu()
 */
function mymodule_menu(){
    $items['employee/create'] = array(
        'title' => t('Create Directory'),
        'page callback' => 'drupal_get_form',
        'access callback' => 'mymodule_access_directory',
        'page arguments' => array('mymodule_employee_create_form'),
        'type' => MENU_NORMAL_ITEM,
        'weight' => 0,
    );
    return $items;
}
/*
 * Implementation of access callback for the menu
 */
function mymodule_access_directory(){
    global $user;
    # administrator can only access this
    if( in_array('administrator', $user->roles) ){ 
        return TRUE;
    }
    return FALSE;
}
/**
 * Home page which shows latest entered ten legs with cheapest tickets
 */
function mymodule_employee_create_form(){
    drupal_set_title('Create Employee Directory');
    return drupal_get_form('user_register_form');
}

/**
 * Implementation of hook_form_alter
 * Override form structure
*/
function mymodule_form_alter(&$form, &$form_state, $form_id) {

    if($form_id == 'user_register_form'){       
        if($_GET['q'] == 'employee/create'){ # admin only can access this page
            # hide drupal default role selection; default role is employee
            $form['account']['roles']['#default_value'] = array(6);
            _mymodule_hide_elem($form, 'roles', 'account');
            # hide auto assign role selection; default role is employee
            $form['autoassignrole_user']['user_roles']['#default_value'] = 6;
            _mymodule_hide_elem($form, 'autoassignrole_user');
            # hide Status
            _mymodule_hide_elem($form, 'status', 'account');
            # hide Notify
            _mymodule_hide_elem($form, 'notify', 'account');

            # Change "Create new account" to "Create"
            $form['actions']['submit']['#value'] = 'Create';
        }


    }
}   

function _mymodule_hide_elem(&$form, $name, $group=NULL){
    if($group){
        $form[$group][$name]['#prefix']     = '<div style="display:none">';
        $form[$group][$name]['#suffix']     = '</div>'; 
        $form[$group][$name]['#required']   = FALSE;        
    }else{
        $form[$name]['#prefix']     = '<div style="display:none">';
        $form[$name]['#suffix']     = '</div>'; 
        $form[$name]['#required']   = FALSE;
    }
}
?>
1

My mistake in my hook_menu(). The page arguments should be array('user_registration_form') and I removed the custom page callback mymodule_employee_create_form(). It did work as expected.

function mymodule_menu(){
    $items['employee/create'] = array(
        'title' => t('Create Directory'),
        'page callback' => 'drupal_get_form',
        'page arguments' => array('user_register_form'),        
        'access callback' => 'mymodule_access_directory',
        'type' => MENU_NORMAL_ITEM,
        'weight' => 0,
    );
    return $items;
}

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.