Access Control of Form Element
We can use the #access directive of the form element so that a permission can be specified using user_access(). For instance:
$form['menu'] = array(
'#type' => 'fieldset',
'#title' => t('My Menu'),
'#access' => user_access('administer menu'),
'#collapsible' => TRUE,
'#collapsed' => !$link['link_title'],
);
In this case, only users with the “administer menu” permission will see this field rendered.
Disable Form Element for All Users
Note that in the previous example the #access directive is accepting a boolean TRUE from user_access() if the permission is valid. A simple way to disable a form element for ALL users will be to skip the access check and simply set #access to FALSE:
$form['menu'] = array(
'#type' => 'fieldset',
'#title' => t('My Menu'),
'#access' => FALSE,
'#collapsible' => TRUE,
'#collapsed' => !$link['link_title'],
);
Additional Info
- Log in to post comments