Login

My Profile - Add Configuration Controls

In this post we are going to add a configuration page for the system administrator.

Our "My Profile" project has grown. So far the system is set up so that logically and administrator performs all the work. The design assumption so far has been that only a user granted special permissions can edit or delete "my profile" records. A new user is allowed to enter their "my profile alias" when they create an account. However, we haven't facilitated a user editing their alias at some later date.

As with many projects, the business requirements change and increase as the system is put in use. The project team is divided as to whether users should be able to modify their alias. A compromise is reached, the system will now provide the facility for users to change their own alias, however, an administrator can change that feature. Thus, we are going to build a system configuration screen for "my profile".

First, let's add a new menu item. Note the path starts with "admin". The following snippet is added to our hook_menu() implementation:

 $items['admin/settings/my_profile'] = array(
        'title' => 'My Profile System Settings',
        'description' => 'Configures the global My Profile Settings.',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('my_profile_admin_settings'),
        'access arguments' => array('administer site configuration'),
        'type' => MENU_NORMAL_ITEM);

Drupal makes building the rest of our configuration screen easy. We'll use 2 Drupal components to make our task simple.

First, we'll use the "variable" table to store the value of our setting. You get a variable with variable_get(). You set a variable with variable_set().

Next, we will use system_settings_form() to perform most of the processing (validation, submit, storage, retrieval).

All we have to do is create the custom portion of our form (implementing hook_form):

 function my_profile_admin_settings() {
 
        $form['my_profile_system_settings'] = array(
            '#type'=> 'fieldset',
            '#title'=>t('Settings')
        );
        $form['my_profile_system_settings']['my_profile_public']= array(
                '#title'=> t('Set Public Registration of My Profile Alias'),
                '#type'=>'checkbox',
                '#description'=>t('Check to turn on display of My Profile Fields in the Account Registration form'),
 
                '#return_value' => 1,
                '#default_value'=>variable_get('my_profile_public',1),
                '#weight'=>0
        );
 
        $form['my_profile_system_settings']['my_profile_user']= array(
                '#title'=> t('Set Whether Users Can Change Alias'),
                '#type'=>'checkbox',
                '#description'=>t('Check to allow uses\'s to change My Profile Alias '),
 
                '#return_value' => 1,
                '#default_value'=>variable_get('my_profile_user',1),
                '#weight'=>1
        );
 
        return system_settings_form($form);
}

 

Here is a screen shot of how our menu item is added to the main "admin" section of our Drupal website:

 

And here is a screen shot of what our configuration screen looks like:

 

 

In our next post we'll modify the standard "My Account" user/profile menu and edit forms. We'll code the routines that allow users to edit their own "my profile alias".