0

I am completely new to drupal, and just started learning.

Here's my issue:

I have created a module, called 'doom'. It has only one form.

I would like to remove all the default elements ( header, footer, sidebar ) and to add custom elements.

I tried this,

/**
 * Implements hook_theme_registry_alter().
 */
function doom_theme_registry_alter(&$theme_registry) {
    $module_path = drupal_get_path('module', 'doom');
    $template_file_objects = drupal_find_theme_templates($theme_registry, '.tpl.php', $module_path);
    foreach ($template_file_objects as $key => $template_file_object) {
        if (!isset($theme_registry[$key]['theme path']) || !preg_match('#/themes/#', $theme_registry[$key]['theme path'])) {
            $theme_registry[$key]['theme path'] = $module_path;
            $theme_registry[$key] = array_merge($theme_registry[$key], $template_file_object);
            $theme_registry[$key]['type'] = 'module';
        }
    }
}

This code changes all the pages in the site. But I just wanted to customize the template for 'doom' page. http://example.com/drupal/doom

2
  • Have a look at Drupal 7 Template (Theme Hook) Suggestions, there's a section about page template. Scroll down or search for "page--[front|internal/path].tpl.php".
    – Ben
    Commented May 19, 2015 at 1:28
  • Means you want to create custom template file to display your changes on that particular page only. Am I right ?
    – Nishant
    Commented May 19, 2015 at 5:08

1 Answer 1

0

As I understood, you need something like this:

/**
 * Implements hook_menu().
 */
function doom_menu() {
  $items['doom'] = array(
    'page callback' => 'doom_page_callback',
    'page arguments' => array(),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
}

/**
 * Implements hook_theme().
 */
function doom_theme() {
  // Get the path to this module and define theme dir.
  $module_path = drupal_get_path('module', 'doom');
  $theme_dir   = $module_path . '/theme';

  // Themable elements. This will look for "sample-template.tpl.php" file
  // in the "YOUR_MODULE_DIR/theme" folder. That file can be anything.
  return array(
    'sample-template' => array(
      'template' => 'sample-template',
      'variables' => array('content' => NULL),
      'path' => $theme_dir,
    ),
  );
}

/**
 * Menu callback.
 */
function doom_page_callback() {
  // Get your template and display ONLY the contents of that file.
  print theme('sample-template', array('content' => t('Optionally, pass dynamic content here.')));
}

And the sample-template.tpl.php file:

<div id="sample-template">
  <?php print $content; ?>
</div>

This should give you the idea. If you still want to theme the HTML header/footer from the front end theme, you should be able to create page--doom.tpl.php file there and customize what is displayed. In that case, just return the output instead of printing it in doom_page_callback().

Hope this helps.

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.