JoeJoe
JoeJoe

Reputation: 83

How to display a web form fields based on the user interacts

I have to develop a system which allows me to give a preview of the fields based on the user is connected. For example, we have 3 Combobox. The administrator can see and use all 3 Combobox, while simple one user only 2. How can I do this on a Webform? Thank you

Upvotes: 0

Views: 174

Answers (1)

M.Be
M.Be

Reputation: 322

The better Drupal way to do it is to use Permission.

In you module, declare the following :

/**
 * Implements hook_permission
 */
function yourmodule_permission() {
    return array(
        'access combobox 1' => array(
            'title'       => t('Access combobox 1'),
            'description' => t('Allow user to view combobox 1')
        ),
        'access combobox 2' => array(
            'title'       => t('Access combobox 2'),
            'description' => t('Allow user to view combobox 2'),
        ),
    );
}

then, in your form, use this permissions to display your combobox :

// Définition du composant du choix du schéma de base.
    $form['combobox_1'] = array(
            '#access'        => user_access('access combobox 1'),
            '#type'          => 'select',
            '#title'         => 'yourtitle',
            '#options'       => array_keys(...),
    );

    $form['combobox_2'] = array(
            '#access'        => user_access('access combobox 2'),
            '#type'          => 'select',
            '#title'         => 'yourtitle',
            '#options'       => array_keys(...),
    );

And then, just need to check which user role have this permission in your "/admin/people/permissions" page ;)

Upvotes: 1

Related Questions