Passing data between Drupal module callback, preprocess and template
I've create a module called finder that I want to take parameters from a url, crunch them and then display results via a tpl file. here's the relevant functions...
function finder_menu()
{
$items = array();
$items['finder'] = array(
'page callback' => 'finder_view',
'access callback' => TRUE,
);
return $items;
}
function finder_theme($existing, $type, $theme, $path)
{
return array(
'finder_view' => array(
'variables' => array('providers' => null),
'template' => 'results',
),
);
}
function finder_preprocess_finder_view(&$variables)
{
// put my data into $variables
}
function finder_view($zipcode = null)
{
// Get Providers from Zipcode
return theme('finder_view', $providers);
}
Now I know finder_view is being called. I also know finder_preprocess_finder_view is being called. Finally, I know that result.tpl.php is being used to output. But I cannot wrap my head around how to do meaningful work in the callback, somehow make that data available in the preprocessor to add to "variables" so that i can access in the tpl file.
in a situation where you are using a tpl file is the callback even useful for anything? I've done this in the past where the callback does all the work and passes to a theming function, but i want to use a file for ou开发者_StackOverflowtput instead this time.
Thanks...
UPDATE: This was actually a parameter naming issue. Drupal 6 uses an 'arguments' key in hook_theme and not 'variables'. Once changed it all worked as expected. I removed the preprocessor also as my logic was being performed in the callback.
Your logic should always be in the call back. Which can be in a separate file by specifying the "file" in the menu array.
In your example once you "get providers" from the zipcode, which seams pretty important. :)
In your theme function you can, and should convert your $providers array into something that is more like a traditional drupal $vars arary, or alternatively you can skip both the preprocess and the template and simply do all your theme work in the theme function. preprocess and templates are conveniences and are not always required.
I would rename the theme function to something different or for that matter the callback, which would make things a little easier to understand.
Does that help? I'm not 100% sure what you are really asking, but if you are looking to simplify your code while keeping to well structured, hopefully that will help.
The problem existed in hook_theme where I used 'variables' instead of 'arguments'. Should I just delete this question?
精彩评论