James Shields
James Shields

Reputation: 339

Apply custom page template to content from Drupal 7 module

I am developing a Drupal module. Part of it opens a pop-up window, displays some elements in it, and uses JavaScript to transfer input back to the main page.

As this is a small window, I don't want it to display the full theme borders from the site's theme.

In Drupal 6 I was able to achieve this with the following:

function MyPopupPageHandler() {
  @content = "Content...";
  //Add additional content to @content;
  print theme("page", @content);
}

However, Drupal 7 expects the second parameter of the theme() function to be an array, and none of the examples I've seen show how I set the main content of the page.

What I'd like is a custom page template in the module, that can be overridden by the site theme if it provides one.

I would like to know:

  1. What elements do I need to put in the array I pass to the theme() function?
  2. What should I call my template file?
  3. How do I tell Drupal where to find my template, as it needs to be in the module by default?

Appreciate any help you can offer.

James

Upvotes: 1

Views: 3503

Answers (2)

Mehul Jethloja
Mehul Jethloja

Reputation: 637

Try this First of all create a menu in .module file

function MYMODULE_menu()
{
    $items['Demo'] =array(
            'title' => 'Demo Page',
            'page callback' => 'demo_page', // call a function
            'access arguments' => array('access content'),
    );
     return $items;

}

After you have create a function

function demo_page()
{
    $select = db_select('node', 'n');
    $select = $select->fields('n', array('id'))
    ->extend('PagerDefault');

    $queried_nodes = $select->execute()
    ->fetchAllAssoc('id');
    $pager = theme('pager');

    return  theme('demo_template', array('nodes' => $queried_nodes , 'pager' => $pager)); // call a theme or you have no pass any argument in theme to change a 'nodes'=> NULL or 'pager'=>NULL 
}

after i have create a theme function

function MYMODULE_theme()
{
    return array(
      'demo_template' => array(
        'template' => 'demo-page',//this is file name of template file
        'variables' => array('nodes' => NULL,'pager' => NULL), //this is pass avarible of templates file
        'path' => drupal_get_path('module', 'MYMODULE_NAME').'/template' // set a path of file 
    ),
 );

}

after you have create file name like demo-page.tpl.php in sites/all/modules/MYMODULENAME/template/

and clear caches

Upvotes: 2

Djouuuuh
Djouuuuh

Reputation: 1167

1) The second parameter of theme() function must be an associative array. Your function should look like :

function MYMODULE_custom_function() {
  $content = "Some stuff";
  return theme('MYMODULE_custom_output', array('content' => $content));
}

2) I guess you meant "Where should I call my template file?" In the hook_theme() function in your same .module file :

function MYMODULE_theme() {
  return array(
    'MYMODULE_custom_output' => array(
      'variables' => array('content' => array()),
      // You can also use template file here : 'template' => 'MYMODULE-template'
      // OR use the following theme_function() if you don't want to create a new file
    ),
  );
}

// If you don't use template file
function theme_MYMODULE_custom_output($variables) {
  $output = // Arrange your html output here
  return $output;
}

3) To tell where to find your custom template file if you decide to use one, you can read this : https://drupal.org/node/715160 and I hope it will help.

Please stay indulgent because I'm still new with Drupal and I did try to do my best here :o)

Upvotes: 0

Related Questions