I asked a similar question a few months ago, but the true is that there is very few information about the correct way of doing this.
The simplest is using arg() functions, but as you can read in the docs, is the most wrong way of doing it. Trying to follow the correct steps, that is what I have so far:
A menu with the paths which we need:
/*
* Category search section
*/
$items['products/search/%/%'] = array(
'title' => 'Products search filtering',
'description' => 'Show review categories.',
'page callback' => 'ThemeSearchProducts',
'page arguments' => array(2,3),
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
'file' => 'includes/mymodule_reviews.inc',
);This arguments are sended via ... arguments (sorry for the joke). to the themesearchproducts function
/**
* THEME List of reviewable products
*
*/
function ThemeSearchProducts($arg1, $arg2){
// $output = theme('search_products', $arg1);
$output = theme('search_products', 'test');
echo 'args in theme: ' . $arg1 . $arg2 . ' || ';
return $output . theme('pager', 10);
}The echo works fine, but now the problem. How do I send this variables to the hook_theme of the module, and from there to the tpl.php? I have something like that:
function MYMODULE_theme ($existing, $type, $theme, $path) {
$path = drupal_get_path('module', 'MYMODULE'); // . '/templates';
// ADD THE templates DIRECTORY
$path = $path . '/templates';
return array(
// as in 'theme('verbose_method',...)
'main_reviews' => array(
'template' => 'MYMODULE-main_page_reviews',
'path' => $path,
'variables' => array('forums' => NULL, 'topics' => NULL),
),
as you can see, the module now calls the tpl in templates/MYMODULE-main_page_reviews.tpl.php, but i cannot find the way of sending a variable from here the previous callback to the hook_theme. Adding a new argument to the MYMODULE_theme throws an error, and I cannot get rid of it adding for example this new argument in ThemeSearchProducts, like this:
$output = theme('search_products', 'test');I think that using variable_set is also wrong, isn't it? I am trying to follow the "Drupal correct way" as much as possible, but I cannot understand how to do this last step.
Thank you very much in advance
Comments
well, i finally got it
well, i finally got it :-).
my _theme function was wrong. I was using variables instead of arguments. That is how it looks now:
// LIST OF PRODUCTS, POSSIBLY FILTERED'search_products' => array(
'template' => 'bounty-search_products',
'path' => $path,
'arguments' => array('argument1' => NULL, 'argument2' => NULL),
),
Then, from the previous function i have:
function ThemeSearchProducts($arg1, $arg2){
$output = theme('search_products', array('argument1' =>$arg1, 'argument2' =>"test"));
return $output . theme('pager', 10);
}
theme() does the magic. It sends the arguments in a single array, which it can be read in tpl.php like that:
dpm($argument1);or, of course:
echo '<br>argument 1::';
echo $argument1['argument1'];
echo '<br>argument 2::';
echo $argument1['argument2'];