How to open standard content pages in Drupal 7's administrative overlay

One of the new things introduced in Drupal 7 as compared to D6 is utilization of modified Seven as default administration theme, which - combined with new Overlay module added to core - brings completely new experience to admin pages.

One of the new things introduced in Drupal 7 as compared to D6 is utilization of modified Seven as default administration theme, which - combined with new Overlay module added to core - brings completely new experience to admin pages.

Although overlay by default is designed to be used only for administrative pages, it does not mean that it cannot be used for other pages as well. All it requires, as usual with Drupal, is just a simple trick.

To illustrate it on an example - let's assume we have Terms & Conditions page defined as follows:

/**
 * Implements hook_menu().
 */
function MYMODULE_menu() {
  $items = array();
  $items['terms-and-conditions'] = array(
    'title' => t('Terms & Conditions'),
    'page callback' => 'MYMODULE_page_terms_and_conditions',
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

Going to http://mysite.com/terms-and-conditions will open our T&Cs as any other standard content page, using default site's theme.

Now, as overlay is meant to be used only for admin pages (just try to force opening any standard page in overlay - in our example it would be http://mysite.com/#overlay=terms-and-conditions - it will just ditch the overlay and redirect user back to standard version of the page) (see overlay_init() function in overlay.module for details), we need to let the system know that our page is an admin page.

To do this, we need to implement hook_admin_paths() and define our T&Cs URL as admin one:

/**
 * Implements hook_admin_paths().
 */
function MYMODULE_admin_paths() {
  $paths = array(
    'terms-and-conditions' => TRUE,
  );
  return $paths;
}

Defining such non-really-admin path as admin one does not introduce any security concern, as it just changes (or, actually, could change) the way such page is being displayed. Quoting after hook_admin_paths() description:

Modules may specify whether or not the paths they define in hook_menu() are to be considered administrative. Other modules may use this information to display those pages differently (e.g. in a modal overlay, or in a different theme).

Finally, last thing to do is to grant Access the administrative overlay and View the administration theme permissions to authenticated and/or anonymous users.

Now, every time anyone clicks on the Terms & Conditions link anywhere on the site, the page will automatically open in administrative overlay.

Two things need to be noted here though:

  • all pages in administrative overlay (including our T&Cs page) will be using administration theme (Seven, or anything else defined in Appearance section), not site's default theme,

  • going to http://mysite.com/terms-and-conditions directly by typing it in the browser's address bar will still open the page as a standard page (without overlay) and using default site's theme.

More about Overlay: