Drupal: Passing custom variable from custom module to the template

customizationdrupaldrupal-6drupal-modulespreprocessor

I realize this question has been asked, but I either simply don't understand or the previous answers don't apply (or I don't understand how to apply them) to my circumstance. Here goes:

I have a custom module named:

"my_module" in /sites/all/modules/custom/my_module

I have a module file:

/sites/all/modules/custom/my_module/my_module.module

I have a page template name "page-mypage" which is NOT in my module:

/sites/all/themes/mytheme/pages/page-mypath-mypage.tpl.php

I made the hook menu for this:

$items['mypath/mypage'] = array(
    'title'         => 'My Page!',
    'page callback'         => 'my_module_mypage',
    'page arguments'    => array(1,2),
    'access callback'   => true,
    'type'          => MENU_CALLBACK,
);

In the function, I build up some content like so:

function my_module_mypage($x, $y) {
    $output = "foo AND bar!";
    return $output;
}

In the template (again, NOT in my module folder, but in the THEME subfolder "pages", I have:

<?php print $content ?>

When I go to http://mysite/mypath/mypage I get "foo AND bar!"

Now for the question. I want a new variable, defined in my_module_mypage(), called '$moar_content'. I want to output $moar_content in my page-mypath-mypage.tpl.php. I only need to do this for this module and for this template. I do not need it theme-wide, so I don't think using mytheme's 'template.php' is appropriate.

I think I need to use some kind of preprocessing, but everything I try fails, and everything I read seems to be missing some kind of magic ingredient.

My thinking was:

function my_module_preprocess_page_mypath_mypage(&$variables) {
    $variables['moar_content'] = 'OATMEAL';
}

or

function my_module_preprocess_my_module_mypage(&$variables) {
    $variables['moar_content'] = 'OATMEAL';
}

or something. I'm pretty sure I'm on the right track, but I'm hitting a brick wall.

Best Answer

To do the job, you must follow Drupal's best practices, supposing you are using D6, so you can insert some variables to your template like this :

// You menu path is good
$items['mypath/mypage'] = array(
    'title' => 'My Page!',
    'page callback' => 'my_module_mypage',
    'page arguments' => array(1,2),
    'access callback' => true,
    'type' => MENU_CALLBACK,
);

Second thing, we define the theme hook for our page

// We define here a new theme file for your your page
// Your theme file must be located in your module's folder
// you can use a subfolder to group all your module's theme files
// E.g : themes/my-module-theme.tpl.php
// Note that in theme files, we change _ by -
function my_module_theme() {
    return array(
        'my_module_theme' => array( // Keep that name in your mind
            'template' => 'my_module_theme',
                'arguments' => array(
                'my_var' => NULL,
                'my_var2' => NULL,
            ),
        )
    );
}

Now we can create a file "my-module-theme.tpl.php" in the root folder of our module, and paste something like "foo AND bar!" Back to our my_module.module, the callback must be something like :

function my_module_mypage($x, $y) {
    // $x and $y are optionnal, so this is the first manner
    // to inject variables into your theme's file
    $output = theme("my_module_theme", $x, $y);
    return $output;
}

Also you can use preprocess hook to insert variables

// The hook must be named like this : template_preprocess_NAME_OF_THEME_FILE
// where NAME_OF_THEME_FILE is the name that you kept in your mind ;)
function template_preprocess_my_module_theme(&$variables) {
    // Do some job
    $var1 = 'Foobar';

    // So in "my-module-theme.tpl.php", $my_var1 will print Foobar
    $variables['my_var1'] = $var1;
}
Related Topic