Reputation: 21
I have created a custom block and would like to associate it with a tpl in the associated module.
Currently I can associate only a tpl file in the theme folder.
I would like it to be a tpl of the module and then use the hook_menu and pass some data to it, which is impossible (to my knowledge) with a tpl in the theme folder.
Is that even possible?
If this is not possible I would like to use the tpl in my theme as a container and use the hook_menu to pass its contents but I don't know how to return the tpl/theme I will create in the module.
Can someone help me?
Upvotes: 0
Views: 950
Reputation:
The way I do this is as follows...
function YOURMODULE_menu(){
$items['somepage'/%] = array(
'title' => 'Some page title',
'page callback' => 'YOURMODULE_page',
'page arguments' => array(1),
'type' => MENU_CALLBACK,
'access arguments' => array('access content'),
);
return $items;
}
function YOURMODULE_page($data){
$output = 'value from YOURMODULE module! = '.$data;
return theme('theme_file',array('results' => $output));
}
function YOURMODULE_theme() {
$path = drupal_get_path('module', 'YOURMODULE');
return array(
'theme_file' => array(
'variables' => array('results' => null),
'template' => 'theme_file',
'path' => $path,
),
);
}
Place your tpl file theme_file.tpl.php in your module directory and inside it use the below code.
<?php print $results; ?>
or
function YOURMODULE_theme() {
return array(
'theme_file' => array(
'variables' => array('results' => null),
'template' => 'theme_file',
),
);
}
Place your tpl file theme_file.tpl.php in your theme directory and inside it place the below code
<?php print $results; ?>
Go to your http://yourdomain.com/somepage/somedata to see the result.
Upvotes: 0
Reputation: 1861
I hope the below example helps you
function MYMODULEBLOCK_block_info() {
$blocks['MYMODULE_BLOCK_NAME'] = array(
'info' => t('MYMODULE BLOCK TITLE'),
'cache' => DRUPAL_NO_CACHE, //there are a number of caching options for this
);
return $blocks;
}
function MYMODULEBLOCK_block_view($delta = ''){
switch($delta){
case 'MYMODULE_BLOCK_NAME':
if(user_access('access content')){ //good idea to check user perms here
$block['subject'] = t('MYBLOCK_TITLE');
$block['content'] = MYMODULE_BLOCK_FUNCTION_ITEMS();
return $block;
}
break;
}
}
function MYMODULE_BLOCK_FUNCTION_ITEMS(){
$items = array();
$items['VAR_ONE'] = array('#markup' => 'VAR_ONE_OUTPUT'); //this is the simplest kind of render array
$items['VAR_TWO'] = array(
'#prefix' => '<div>',
'#markup' => 'VAR_TWO_OUTPUT',
'#suffix' => '</div>',
);
// this is where the $items get sent to your default MYMODULE_BLOCK.tpl.php that gets
// registered below
return theme('MYMODULE_BLOCK_FUNCTION_ITEMS', array('items' => $items));
}
//here you are registering your default tpl for the above block
function MYMODULE_theme() {
$module_path = drupal_get_path('module', 'MYMODULE');
$base = array(
'path' => "$module_path/theme",
);
return array(
'MYMODULE_BLOCK_FUNCTION_ITEMS' => $base + array(
'template' => 'MYMODULE_BLOCK', //leave off .tpl.php
'variables' => array('items' => NULL,),
),
);
}
All the stuff in CAPITALS (except DRUPAL_NO_CACHE) can be named whatever you want
And then in a subfolder in your module called theme/ there should be a file called MYMODULE_BLOCK.tpl.php which could have this in it:
<?php
$items = $variables['items'];
print render($items['VAR_ONE']);
print render($items['VAR_TWO']);
And if you wanted to, you could actually overwrite the "default" module implementation you just made for MYMODULE_BLOCK.tpl.php in your theme as you wish in block--MYMODULE--DELTA.tpl.php
Upvotes: 1