0

I want my custom module pages to be rendered using the admin theme, at the moment they only render using the admin theme for User 1 (see below).

I have a module called mymodule that defines many administrative views. It's setup in the following structure:

function mymodule_menu() {
    return [
        'admin/content/foo'     => ['page callback' => 'handle_foo', /*other opts*/],
        'admin/content/foo/bar' => ['page callback' => 'handle_foobar', /*other opts*/], 
        /* list goes on*/
    ];
 }

 function handle_foo() {
    return ['#theme' => 'handle_foo'];
 }

 function theme_handle_foo() {
    /*database logic here, then return compiled html to be rendered. */
    return '<div> I am a foo!</div>';
 }

Say I have 2 users:

  • User 1: All permissions available(including administration)
  • User 2: 1 custom permission defined in the module

When logged in as User 1, all pages that follow admin/* are rendered with the drupal admin_theme including mymodule paths.

When logged in as User 2 all of mymodule pages are rendered using the frontend theme.

I've tried using hook_admin_paths but this doesn't work:

function mymodule_admin_paths() {
  return [
    'admin/content/foo'   => TRUE,
    'admin/content/foo/*' => TRUE,
    '/admin/content/foo'  => TRUE,
  ];
}

I have also read on here that any path that follows the admin/* convention should use the admin_theme by default which appears to be true for user 1 but not user 2.

My take on this is that I need to give user 2 some kind of permission, however I don't want them to be able to do anything other than login and access the paths set out in mymodule

(I don't want to use 3rd party modules)

  • What role user#2 does have ? Are you sure it has the permission "view the administration theme" ? – TytooF Aug 11 '16 at 11:46
  • User 2 is a custom role with 1 permission. You are correct though I realised this just as you posted, thanks for replying :) – Kiee Aug 11 '16 at 11:59
1

I found 2 solutions to this:

Solution one

Use hook_custom_theme(): see hook_custom_theme()

function mymodule_custom_theme() {
 $use_admin = workout_when_you_want_to_use_admin(); //true or false
 if ($use_admin)
   return variable_get('admin_theme');
}

Solution 2 (preferred)

Ensure that the user account has the permission View the administration theme attached to their role.

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.