0

I'm trying to create a new page template for a custom form I'm building in my module. My goal is to use create something like a new page.tpl.php for this specific form and it looks like this method is creating the equivalent of a custom node.tpl.php within the content area of my site instead of generating a separate page template.

For example

/*
 * Implements hook_menu
 */
function mymodule_menu() {
  $items = array();
  $items['foo/my_custom_form'] = array(
    'title' => 'My custom form',
    'page callback' => 'mymodule_custom_page',
    'access callback' => TRUE, // Everyone has access.
  );
  return $items;
}

function mymodule_custom_page(){
  return theme('mymodule_template');
  return drupal_get_form('mymodule_form');
}

/*
 * Implements Hook_theme
 */
function mymodule_theme() {
  return array(
    'mymodule_template' => array(
      'template' => 'custompage', // template file called custompage.tpl.php
      'arguments' => array(),
    ),
  );
}

function mymodule_form($node, &$form_state){
//my form in here
}

I'm having an issue with the form not appearing.

Maybe I'm going down the wrong path and should be trying to create function in my theme's template.php, and then use a hook_preprocess_page to set some variables to create a page--custom.tpl.php for my form?

I can achieve what I'm trying to do by making some configuration changes to some blocks and redrawing the content area with CSS, but not sure if that's the best practice.

  • 1
    return does exactly what it says on the tin. It returns from the function, and any code after it is ignored. So return drupal_get_form('mymodule_form'); can never be executed. Your question about theming is quite a broad one, I'd recommend starting at the Theming Guide Docs. Also check out the Simple PHP tutorial to make sure you're happy with usage of the language – Clive Mar 30 '13 at 12:57
1

Here is your issue, your can't return twice in a function.

function mymodule_custom_page(){
  return theme('mymodule_template');
  return drupal_get_form('mymodule_form');
}

What you need to do is just the first line, return theme('mymodule_template'); You also need to change your form function parameters to &$form, &$form_state.

Then, in your custompage.tpl.php, you do the drupal_get_form('mymodule_form');

Result:

/*
* Implements hook_menu
*/

function mymodule_menu() {
  $items = array();
  $items['foo/my_custom_form'] = array(
    'title' => 'My custom form',
    'page callback' => 'mymodule_custom_page',
    'access callback' => TRUE, // Everyone has access.
  );
  return $items;
}

function mymodule_custom_page(){
  return theme('mymodule_template');
}

/*
* Implements Hook_theme
*/

function mymodule_theme() {
  return array(
    'mymodule_template' => array(
      'template' => 'custompage', // template file called custompage.tpl.php
      'arguments' => array(),
    ),
  );
}

function mymodule_form(&$form, &$form_state){
  //my form in here
}

And in custompage.tpl.php:

print drupal_get_form('mymodule_form');
0

IF the page you want to theme differently is going to be on the path foo/mycustomform, you can use Drupal's built in template suggestions and "just" create a page--foo--mycustomform.tpl.php (D7) or page-foo-mycustomform.tpl.php (D6) file and put it along with any other templates in your theme's directory.

Clear your caches and you should be on your way.

More information about template suggestions are available for D7 here: http://drupal.org/node/1089656 and D6 here: http://drupal.org/node/1089642 but basically Drupal will recognize page-PATH template suggestions.

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.