I want to allow a certain role to change the site logo, but I don't want to give the 'administer site configuration' permission to that role.

At first I was hiding some form elements from the theme settings forms using hook_form_alter() and $element['#access'] = FALSE, leaving only the options related to uploading a new site logo; after that I noticed the user would need the 'administer site configuration' permission to access that form.

Do you have any idea?


You could implement hook_menu_alter() to change the permission associated with that page, and use other permissions to decide which form fields should be visible to the currently logged in user. In this case, instead of using $element['#access'] = FALSE, you should use $element['#access'] = user_access('your custom module permission').
In the case you are only interested to allow some users to change the site logo, then you can use your custom permission for the page containing that settings form, and then use the "administer site configuration" permission for the form fields only the site administrators should see ($element['#access'] = user_access('administer site configuration')).

function mymodule_menu_alter(&$items) {
  if (isset($items[$menu_path]) {
    // Use the following line if the access callback already used is not user_access().
    $items[$menu_path]['access callback'] = 'user_access';
    $items[$menu_path]['access arguments'] = array('your custom module permission');

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == $form_to_alter) {
    $form[$element_id]['#access'] = user_access('administer site configuration');
|improve this answer|||||
  • Thanks a lot. I finally changed 'access callback' instead of 'access arguments' because the theme settings form had a callback to function _system_themes_access(). – dusan Dec 15 '11 at 19:23
  • You can change the access callback to 'user_access', and then change the access arguments. If the access callback is going to use user_access() for just a permission, it is better to change the access callback. – kiamlaluno Dec 15 '11 at 19:51
  • That was not the case: return user_access('administer site configuration') && ($theme->status || $theme->name == variable_get('admin_theme', '0')); – dusan Dec 15 '11 at 20:00

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.