Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Implement hook_theme in drupal 7

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 494
    Comment on it

    As per Drupal's definition "A hook is a PHP function that is named foo_bar(), where "foo" is the name of the module (whose filename is thus foo.module) and "bar" is the name of the hook. Each hook has a defined set of parameters and a specified result type."

    hook_theme is one of the most important hook in Drupal and basic purpose of this is to theme output. hook_theme basically returns HTML or it can also specify how a particular render array is to be rendered as HTML.

    hook_theme can be used in template.php or in any module file.

    In template.php it can be used to theme the theme's template like header.tpl.php. For example:

    function mytheme_theme($existing, $type, $theme, $path){
        return array(
            'mytheme_header' => array(
                'template' => 'header',
                'path' => $path . '/templates',
                'type' => 'theme',
                'variables' => array(
                    'title' => NULL,
                    'some_text' => NULL,
                ),
            ),
        );
    }
    

    In module file, we can assign html template for any page. For example:

    /*
     * Returns custom content to Drupal
     */
    function my_page_function() {
        // Call theme() function, so that Drupal includes the custom-page.tpl.php template
        return theme('my_custom_template');
    }
    
    /*
     * Implement hook_theme().
     */
    function custom_example_theme(){
        return array(
            'my_custom_template' => array(
                // file name will be custom-page.tpl.php
                'template' => 'custom-page',
            ),
        );
    }
    

    I hope this will help someone.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: