1

I have a form module that I would like to have on its own page, with no header, footer, etc.. I thought I was successful with adding hook_menu() and a page callback to render the form's block, then added hook_page_alter() to cycle through all available regions and unset them:

function mymodule_menu () {
  $menuitems['myform-page'] = [
    'title' => 'Survey Form',
    'page callback' => 'mymodule_form_page',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  ];
  return $menuitems;
}

function mymodule_form_page (&$page) {
  dpm($GLOBALS['_GET']['q']);
  $current_path = $GLOBALS['_GET']['q'];
  if ($current_path === 'myform-page') {
    $module = 'mymodule';
    $delta = 'mymodule_block';
    $block = block_load($module, $delta);
    $form_page = _block_get_renderable_array(_block_render_blocks(array($block)));
    return $form_page;
  }
}

function mymodule_page_alter (&$page) {
  $current_path = $GLOBALS['_GET']['q'];
  if ($current_path === 'myform-page') {
    $regions = ['top','header','footer_1','footer_2'];
    foreach($regions as $region) {
      foreach($page as $key => $value) {
        if ($region === $key) {
          unset($page[$key]);
        }
      }
    }
  }
}

After all of that, I clear cache and visit the "myform-page", er, page. All the undesired regions are gone (though their HTML wrapper DIVs are still rendered, but I'm okay with hiding those with CSS). I leave that page, going to the front page, for example, then manually type in the address for "myform-page" in the browser and hit enter. The header and footer regions are back!

The "mymodule_page_alter" function only runs once and never again until I clear cache again. The "mymodule" block loads, looks, and functions exactly how I want it to, except for the regions returning when I navigate back to the page. I've reproduced this in a virtual OS environment, so I'm afraid I done goofed. I just don't know where. I've tried using hook_theme() to load a simple page template per info found here: PHP Output in module ...with varying results, ultimately with the undesired regions still showing up. Would anyone know why the page alter hook stops working after executing once? I'd appreciate everyone who can point me in the right direction.

1 Answer 1

1

I believe I resolved my issue. Both the page callback function and mymodule_page_alter function were in a mymodule.pages.inc file that is linked to in the mymodule_menu function. Drupal doesn't like the hook being in the .inc file, so I moved it to the .module file, cleared cache, and reloaded the page. The form's page seems to be loading the content I want to see and nothing else, even after navigating away from the page and coming back to it later.

Lesson learned -- I'll just keep all API hooks in the module file unless taught to do otherwise. Or, unless hooks being outside of the .module file isn't possible anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.