If by administrator you mean the user whose ID is 1, I would use the following code.
/*
* Implements hook_form_FORM_ID_alter().
*/
function mymodule_form_comment_form_alter(&$form, &$form_state) {
global $user;
$uid = $user->uid;
if (($uid !== 1 && !isset($form['#node']) || ($uid !== 1 && $form['#node']->uid == $uid)) {
$form['access'] = FALSE;
}
}
If by administrator you mean the users who have the permission to administer comments, I would use use the following code.
/*
* Implements hook_form_FORM_ID_alter().
*/
function mymodule_form_comment_form_alter(&$form, &$form_state) {
global $user;
$permission = user_access('administer permissions');
if ((!$permission && !isset($form['#node']) || (!$permission && $form['#node']->uid == $user->uid)) {
$form['access'] = FALSE;
}
}
If by administrator you mean a user with a different permission, replace 'administer permissions'
with the permission you want to check.
In any case:
- Setting
$form['access']
to FALSE
is better to unset $form
, which would throw many warnings because of module accessing a NULL
value as it would be an array
- Implementing
hook_form_FORM_ID_alter()
is better than implementing hook_form_alter()
as the latter is invoked for every form, while the first is invoked only of a form type