Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

here is a good one that has always stumped me.

I would love to see a solution in both D6 and D7.

I create custom modules and usually work with the menu api to create menu type -> MENU_CALLBACK.

Here is an example:

function example_menu() {
  $items['example/page1'] = array(
    'page callback'     => 'example_page1_callback',
    'access callback'   => TRUE,
    'type'              => MENU_CALLBACK,
  );
  return $items;
}

I would like to see the exact code solution to provide three different variables to be printed in a custom theme template based on the following code. To add to that challenge, I would like the code to be written in the menu callback function only. I know there are many possible ways to do it but could never figure out how to do it in a menu callback function which would be the most useful way.

//****************************************
// This code block goes in a custom module called example_menu_variables.module

// hook_menu example
function example_menu() {
  $items['example/page1'] = array(
    'page callback'     => 'example_page1_callback',
    'access callback'   => TRUE,
    'type'              => MENU_CALLBACK,
  );
  return $items;
}

// hook_meny callback function
function example_page1_callback() {
  // please provide code within this function to turn these next three
  // variables into separate output variables for the template.
  $variable_to_print1 = 'Foo';
  $variable_to_print2 = 'Bar';
  $variable_to_print3 = array('bing', 'bang', 'bong');
  return;
}
//****************************************



//****************************************
// This block of code represents the part of the custom theme template related to the menu location from the hook_menu call
// D6 template name would be page-example-page1.tpl.php
// D7 template name would be page--example--page1.tpl.php

<div><?php print  $variable_to_print1?></div>
<div><?php print  $variable_to_print2?></div>
<div><?php print  $variable_to_print3[0] . ' ' . $variable_to_print3[1] . ' ' . $variable_to_print3[2]?></div>

/*
output should look like:
Foo
Bar
bing bang bong
*/

//****************************************

Hope this is the kind of problem that someone wouldn't mind solving. Thanks for your time. :)

Ray James

share|improve this question
If I understand you correctly, that is the purpose of hook_theme(). Why does the code need to be contained within the menu callback function? – Adam Balsam May 3 at 17:07
Hi Adam and thanks for the response. I guess it would be great to see what the code would look like both ways. Using hook_theme() requires a ton of setup just to get one custom variable into the theme layer. I would like a simple solution that can be done in the same place that you would be gathering your data from the database to display as output. It would be really great to have a function that just sends that variable like this -> drupal_add_template_variable($var, $varname); or something like that. I am not sure if such a thing exists in Drupal. Thanks. – rayjamesfun May 3 at 19:04

2 Answers

I don't believe you can do this without using hook_theme, but this is exactly what it's for and it's quite simple. (This code is for Drupal 6.)

/**
 * Implements hook_menu().
 */
function example_menu() {
  $items['example/page1'] = array(
    'page callback'     => 'example_page1_callback',
    'access callback'   => TRUE,
    'type'              => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Page callback function
 */
function example_page1_callback() {
  $variable_to_print1 = 'Foo';
  $variable_to_print2 = 'Bar';
  return theme('example_contents', $variable_to_print1, $variable_to_print2);
}

/**
 * Implements hook_theme().
 */
function example_theme() {
  return array(
    'example_contents' => array(
      'arguments' => array('variable_to_print1' => null, 'variable_to_print2' => null),
      'template' => 'example-contents',
    ),
  );
?>

$variable_to_print1 and $variable_to_print2 are now available in example-contents.tpl.php.

share|improve this answer
Hi Adam, thanks for the code. I put it together and ran it but got a ton of errors stating that all the other standard variables are undeclared -> Notice: Undefined variable: logo in include() (line 92 of C:\xampp\htdocs\d7themeaddvar\site\themes\bartik\templates\example-contents.tpl.‌​php) I put the files in a git repository for easy download -> github.com/rayjames/d7themeaddvar – rayjamesfun May 3 at 20:39
content-example.tpl.php does not replace page.tpl.php. Only the defined variables will be available in it (in this case $variable_to_print1 and $variable_to_print1). If you want to make a new variable available to page.tpl you should look into hook_preprocess(). – Adam Balsam May 3 at 20:47
Hmm, we seem to be going in a different direction here. I'm not sure what your code does anymore. As per the initial problem, I asked for code to make variables for print in a custom template, but I guess I need to be more specific because I assumed everyone would understand that the hook_menu item would point to a "page" on the site and that there would be a custom template for said "page" that the variables would be available for. I can't figure out how to get your code to print the variables. Thanks. – rayjamesfun May 3 at 21:03
1  
@rayjamesfun With the best will in the world, you're heading in entirely the wrong direction at the moment. Drupal has a templating system, if you want to take advantage of that you need to follow its rules, which Adam has outlined perfectly. If you don't, then you'll need to resort to plain ol' PHP which we can't get into here; we offer Drupal solutions here, not solutions to get around Drupal :) – Clive May 3 at 21:53
Hi Clive. I hear ya, and I totally agree. I'm in now way trying to go around Drupal, just looking for an easier solution that I already thought was there. I can't get Adam's code to work. I am no newb to Drupal. I have built some extremely complex sites but still can't seem to figure out how to wield the theme system like a ninja. Just looking for a complete example solution at this point in the way of the Drupal. Thanks. – rayjamesfun May 3 at 22:02
show 2 more comments

The code written by Adam is correct (my +1) Your variables are available in example-contents.tpl.php file - it is a template file of your module output.

However, if you need these variables in page.tpl file, then you can just create them:

in your module file:

/**
 * Page variables
 *
 * @return
 *  custom page variables
 */ 
function custom_variables(){

  $variable_to_print1 = 'Foo';
  $variable_to_print2 = 'Bar';
  $variable_to_print3 = array('bing', 'bang', 'bong');

  return array( 'variable_to_print1'    => $variable_to_print1,
                'variable_to_print2'    => $variable_to_print2,
                'variable_to_print3'    => $variable_to_print3
  );
}

in your template.php file:

/**
 * Override or insert variables into the page templates.
 *
 * @param $variables
 *   An array of variables to pass to the theme template.
 * @param $hook
 *   The name of the template being rendered ("page" in this case.)
 */
function YOURTHEME_preprocess_page(&$variables, $hook) {

  // pass your module variables to page--example1.tpl.php file
  if (in_array('page__example1', $variables['theme_hook_suggestions'])){
    if (module_exists('your_module')){
        $your_module_variables = custom_variables();
        $variables['variable_to_print1'] = $your_module_variables['variable_to_print1'];
        $variables['variable_to_print2'] = $your_module_variables['variable_to_print2'];
        $variables['variable_to_print3'] = $your_module_variables['variable_to_print3'];
    }
  }
}
share|improve this answer
Hi Jack, is this a D6 or D7 solution? I was wondering if you could link to the tutorial or Drupal page that might explain what is going on in your code better too. I am assuming that you do not know of a solution that can be implemented within the menu callback, correct? I am also assuming that you would/could write a different function for each "page" so as to keep the variables organized. This doesn't look like it would work with a page that has dynamic page arguments either. Thanks. – rayjamesfun May 4 at 2:08
1  
All above mentioned examples works perfect. My answer concerning a D7 solution. It doesn't matter if your page has dynamic arguments or not. Your variables will be available on every "example1" page ("example1" it's a content type) If you need them on every content type page, then just remove the first IF statement in preprocess_page from my example. You can create your variables anywhere (in your module for example) but then, they have to pass through YOURTHEME_preprocess_page function, because it's the place where your variables are defined as a page variables. – Jack-PL May 4 at 12:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.