I have created simple module.i want to display my content and images on the my custom page with my own style sheet.What i want to do now???What are the functions i want to use for template file creation??i am beginner for module development...pls help me..........

Comments

jaypan’s picture

Here is an example.

1) Register your page with hook_menu():

function MYMODULE_menu()
{
  $menu['path/to/page'] = array
  (
    'title' => 'My Page',
    'page callback' => 'MYMODULE_my_page',
    'access callback' => TRUE,
  );
  return $menu;
}

2) Create your data for your page in your page callback. Set '#theme' to be the theme for your page. Attach your CSS in the #attached property of the render array you return from the callback:

function MYMODULE_my_page()
{
  // Create some data
  $data = array
  (
    '#markup' => t('This is the data for my page'),
    '#prefix' => '<div id="my_page_data">',
    '#suffix' => '</div>',
  );

  // Create a $page render array that will be returned
  // from your callback. Attach the CSS using the #attached
  // element of this array
  $page = array
  (
    'MYMODULE_my_page' => array
    (
      // Note - we will register theme_my_page in the next step
      '#theme' => 'my_page',
      '#contents' => $data,
    ),
    '#attached' => array
    (
      'css' => array
      (
        array
        (
          'type' => 'file',
          'data' => drupal_get_path('module', 'MYMODULE') . '/css/my_page.css',
        ),
      ),
    ),
  );

  return $page;
}

3) Register your theme function in hook_theme():

function MYMODULE_theme()
{
  return array
  (
    'my_page' => array
    (
      'variables' => array('contents' => array()),
      // Register the template my-page.tpl.php
      'template' => 'my-page', // don't add .tpl.php
    ),
  );
}

4) Create your template (my-page.tpl.php)

<h2><?php print t('My Page'); ?></h2>
<div id="my_page_wrapper">
  <?php print render($contents); ?>
</div>

5) Add any CSS in MODULEFOLDER/css/my_page.css

#my_page_wrapper
{
  width:80%;
  margin:0 auto;
}

Contact me to contract me for D7 -> D10/11 migrations.

Nithyanantham’s picture

Thanks for response..Sir i want to change default layout only for my custom page.Now my content and my images displayed content region.i want to remove default region only for that page.i have to create new layout only for my custom page.how can i do sir?pls help me

jaypan’s picture

You need to create a custom theme with the layout you want. You can then implement hook_custom_theme() to set your custom theme to be used on the page you want.

Contact me to contract me for D7 -> D10/11 migrations.

Nithyanantham’s picture

Thanks...now i can able change the theme for custom pages.

cdmo’s picture

This is nice, thank you Jaypan. hook_theme and making templates work can be fairly opaque even with the available documentation in my opinion, and this example helps.

One note, I think print $contents; should be print render($contents);

Thanks again.

-cdmo

jaypan’s picture

You're right, I've altered the post accordingly. Glad it helped.

Contact me to contract me for D7 -> D10/11 migrations.