2

I'm developing a module where I use hook_menu.

I know that I can pass to 'page callback' a function that return string with HTML/PHP code.

function mymodule_menu(){
    $items = array();

    $items['test'] = array(
        'title' => 'page test',
        'description' => 'my test',
        'page callback' => 'my_page',
        'access arguments' => array('access content'),
        'type' => MENU_NORMAL_ITEM,
        'menu_name' => 'main-menu',
    );

    return $items;
}

function my_page(){
    return '<h1>hello</h1>';
}

How can I pass a whole PHP/HTML page as an argument to 'page callback' => '[my_page.php]' instead of creating a string inside my_page() function ?

1 Answer 1

7

The value to 'page callback' can only be a function name.

If you must use 'a whole PHP/HTML page' then work around by using the hook_theme.

<?php
function my_example_menu() {
  $items['my/example'] = array(
    'title' => 'My title',
    'page callback' => 'my_example_page', /* callback function */
'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
);

return $items;
}

/**
 * Implements hook_theme
 *
 */
function my_example_theme($existing, $type, $theme, $path) {
  return array(
    'the_html_page' => array(
    'template' => 'the-html-page', /* create file with this name + .tpl.php */
  ),
);
}

function my_example_page(){
  return theme('the_html_page');
}
?>

The value of 'template' in hook_theme() is the name of your file. Create the template file with .tpl.php extension and place it in the same directory as your module. You can put any html that you need in the file.

2
  • if I have in 'the-html-page' a form how I recieve callback from it?
    – NickF
    Feb 9, 2014 at 16:18
  • Why would you have a form in 'the-html-page'? If calling back a form is your objective, set the menu item's 'page callback' to drupal_get_form and then use a hook_form() to define your form.
    – sri20198
    Feb 20, 2014 at 11:00

Your Answer

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

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