Posted by urwen on November 15, 2012 at 4:04pm
I'm working in a module, which has a _menu with arguments. The are passed fine to the functions which the menu calls itself.
Now, the problem. I have a custom tpl.php for this module, called from this function, That's the idea:
/**
* THEME List of Hospitals
*
*/
function ThemeMYMODULE($param){
$output = theme('hospitals',"testando");
//$output = "test arguments: " . $param;
return $output;
}From there we go to hook_theme, but i don't know how to pass this argument from ThemeMYMODULE to MYMODULE_theme, and make it available in the .tpl.php template.
function MYMODULE_theme($existing, $type, $theme, $path){
return array(
...
'hospitals' => array(
'template' => 'bountyportraitsHospitals',
'path' => $path,
'variables' => array('param' => "params", 'topics' => NULL,'parents' => NULL, 'tid' => NULL, 'sortby' => NULL, 'forum_per_page' => NULL),
),
);
}What am I doing wrong or what i'm forgetting ??
Thanks a lot in advance.
Comments
Read this
Read this http://drupal.org/node/223430 for some background info.
What you need to do is:
So my take on it would be this, step 1:
<?php/**
* Implements hook_theme().
*/
function MODULENAME_theme($existing, $type, $theme, $path) {
return array(
'hospitals' => array(
'render element' => 'element',
'template' => 'bounty-portraits-hospitals', // template file would be called bounty-portraits-hospitals.tpl.php
'path' => drupal_get_path('module', 'MODULENAME') . '/templates', // this is the path to a folder in your module folder
),
);
}
?>
Step 2:
Create the template file called bounty-portraits-hospitals.tpl.php in a folder called templates and place that folder in your module folder. So you end up with YOURMODULEFOLDER/templates/bounty-portraits-hospitals.tpl.php
Step 3:
Create the preprocess function. This will take the argument from theme() and pass it to this function, you can do any processing you want here but is optional.
<?phpfunction template_preprocess_hospitals(&$variables) {
// download and enable the Drupal module and take a look inside $variables
dsm($variables);
// you will see that the argument you passed in theme() is here
// the key name of that variable is the variable name available in your tpl
}
?>
Step 4:
Call the theme function.
<?phptheme('hospitals', 'testando');
?>
which will result in $element being available to your template file.
I create a quick module to show the code, click here to download. If you haven't already I'd install the Devel module first. I'm assuming you are using Drupal 7.
wow, thank you so much for
wow, thank you so much for all the explanations and work :-).
After testing it, something is not working in the last step. I see the dsm output, but no variables are printed there.
We are working in D6 ( we have to migrate to D7 in the next months, I know :-( ).
That's my flow:
Variables and function call from menu:
$items['bountyportraits/hospitals/%'] = array('title' => ' Hospitals',
'description' => 'Show Hospitals page.',
'page callback' => 'ThemeHospitals',
'access arguments' => array('access content'),
'page arguments' => array(1),
'access callback' => 'user_access',
// 'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'includes/bountyportraits.inc',
);
function callback:
function ThemeHospitals($param){return theme(
'hospitals',
array(
'param' => "TITULO ARGUMENT",
)
);
}
As you recommended, the preprocess function which works fine:
function template_preprocess_hospitals(&$variables) {// download and enable the Drupal module and take a look inside $variables
dsm($variables);
// you will see that the argument you passed in theme() is here
// the key name of that variable is the variable name available in your tpl
}
and the theme hook:
/**
* Implements hook_theme().
*/
function bountyportraits_theme($existing, $type, $theme, $path){
$path = drupal_get_path('module', 'hspportraits'); // . '/templates';
// ADD THE templates DIRECTORY
$path = $path . '/templates';
return array(
'hospitals' => array(
'template' => 'prtHospitals',
'path' => $path,
'variables' => array('param' => NULL, 'topics' => NULL,'parents' => NULL, 'tid' => NULL, 'sortby' => NULL, 'forum_per_page' => NULL),
),
);
}
but... no variables printed in the dsm call. I've cleaned the cache, reinstalled the module... nothing :-(.
Thanks a lot for your help.
ok, that's what i've
ok, that's what i've achieved.
function template_preprocess_hospitals(&$variables) {
$variables['hospitals'] = array(
'list item 1',
getHospitals(arg(2)),
);
}
now i have the variable 'hospitals' inside $variables variable.
But, my question is. Is that the correct way of doing this? I was trying to pass the arguments from hook_menu to the next callbacks, but using this way i'm overriding the rest of calls and simply using arg(2) function to get the arguments that I need.
Thank you again :-).
.
.
Drupal docs recommend to pass
Drupal docs recommend to pass as an argument to the page callback rather than using arg(), as described here. So I wouldn't override or create in the preprocess function, in your case.
Also, in your hook_menu you have
<?php'page arguments' => array(1)
?>
but do you mean
<?php'page arguments' => array(2)
?>
I don't use Drupal 6 but your code does work for me here. I would add the below into the template:
<?php print_r(get_defined_vars()); ?>just to double check the variables are 100% not there. If they are I wouldn't bother with the preprocess function.