3

Here's my current situation: I have an external authentication platform that loads in Drupal via an iframe. When the user is authenticated, the external application redirects the user to a Drupal endpoint I set up using:

function mymodule_menu() {

    $items['mymodule/oauth'] = array(
        'title' => 'My Module Receiver',
        'page callback' => 'mymodule_oauth',
        'access callback' => TRUE,
        'type' => MENU_CALLBACK,
        'file' => 'mymodule.pages.inc'
    );

    return $items;
}

And then my callback:

function myapp_oauth() {

    // Does some stuff, authenticates the user, etc

    echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<p>Please wait...</p>
<script type="text/javascript">
  // Check if we are in an iframe or not
  if (window.location.href != window.parent.location.href) {
    // Reload the parent page
    window.parent.location.reload();
  } else {
    // Not in an iframe, so go to the home page
    window.location.href = "' . base_path() . '";
  }
</script>
</body>
</html>
';
    exit;

}

This seems like an incredibly sloppy way to handle this to me, I would prefer not to have to exit the application, and ideally I would want to simply define a tpl template file to load in place of the theme files for this page. Can someone recommend a better way to handle this?

  • To which Drupal version are you interested? – kiamlaluno May 12 '11 at 9:40
  • I'll need to do it with both 6 & 7, but right now I'm on 6. – bhamrick May 12 '11 at 18:34
3

You can define a theme function that uses a template file, and then use theme function to for the menu callback output.
The following is example code for Drupal 7; similar code can be used for Drupal 6.

function mymodule_theme() {
  return array(
    'mymodule_oath' => array(
      'variables' => array('id' => NULL), // This is the list of the variables that will be passed to the template file. 
      'template' => 'mymodule_oath', // Change this to the template file name.
    ),
  );
}

function mymodule_oath() {
  // Does some stuff, authenticates the user, etc.

  print theme('mymodule_oath', array('id' => 10));
  return NULL;
}
  • This seems to be almost what I'm looking for. I got the template to load, but in the context of the theme. Since I'm looking to do is display template contents that contain javascript that will display a message and close the iframe, so I don't want the theme wrapper. – bhamrick May 12 '11 at 18:34
  • @bhamrick I have updated the answer. Returning NULL will cause Drupal 7 to not call drupal_render_page(). – kiamlaluno May 12 '11 at 19:02
  • If anyone is looking at this, wondering how to get the template to load in the context of the theme (like I was), I got it to work using return theme('mymodule_oath'... instead of print theme('mymodule_oath'... – Mark Mar 8 '16 at 19:20
1

If you return false from your menu handler, Drupal will not call page.tpl.php.

So:

function myapp_oath() {
    // Echo your page here
    return false;
}

How to define the template depends a little on which version of Drupal you are using, but have a look at hook_theme() and theme() as a starting point.

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.