By hamakabula on
Hi,
I tried to add a template file to my module which should be shown on a a page with the URL example.com/mymodule
But using the following code in the module's directory does not work for me:
function mymodule_theme() {
return array(
'mymodule' => array(
'arguments' => array('node' => NULL, 'teaser' => FALSE, 'page' => FALSE),
'template' => 'mymodule', // Template file without extension.
),
);
}or
function phptemplate_engine_preprocess_page(&$variables) {
$variables['template_files'][] ='mymodule';
}The functions are not called automatically or they don't force drupal to show the template called mymodule.tpl.php in the mymodule directory, when calling example.com/mymodule, like when I implement mymodule_all() hook. What am I missing?
Thx, hama.
Comments
I found out, that using the
I found out, that using the preprocess function, I have to place my tpl.php file into the theme directory for it to work. How is it possible to just put it in the module's directory and still having the template displayed? Other modules like advanced_profile do this too, so it should be possible.
Btw, the functions above are just for testing purposes, I will add some conditions later not to have the module's template on every page ;)
Thx for any hint, hama.
*push*
*push*
The theme function is
The theme function is correct. But I'm guessing you probably added this code after you had already installed the module. You need to clear the cache before your tpl.php file will be recognized.
Contact me to contract me for D7 -> D10/11 migrations.
Thanks for the answer, but I
Thanks for the answer, but I emptied the chache with the devel block, before. If I just use the mymodule_theme() function, there is an 404 error on the /mymodule page. If I use
instead, at least the template is used, when I put it in the theme's directory. But this is no good solution. I figured out, that it could be a problem reladet to Drupal being in a subdirectory like www.example.com/subdir/mymodule, now could it?
Where are you placing the
Where are you placing the template file? It has to be in the module folder that you are defining hook_theme() in.
Contact me to contract me for D7 -> D10/11 migrations.
I'm sorry to bother you
I'm sorry to bother you again, but for me it looks fine, even though it is probably some stupid mistake I cannot figure out myself.
Thanks a lot for your help,
hama
I'm not sure exactly what you
I'm not sure exactly what you are showing me here. Of course I understand the theme function, but what is all this:
Contact me to contract me for D7 -> D10/11 migrations.
The lines with a preceeding #
The lines with a preceeding # are commands. The first one lists all templates in the directory modules/contactpersons, and one of them is contactpersons.tpl.php. The second one shows the line with the word _theme and the following 8 lines inside contactpersons.module. So it proves that the theme function is in the contactpersons.module which itself is in the same directory as the corresponding template file. I thought it would be more convincing than just to claim that the template file is in the correct directory.
Ok, I get that. Sorry, I'm
Ok, I get that. Sorry, I'm not a 'nix (which I'm guessing/assuming that is) guy, so it was all gibberish to me! haha
Anyways, so far everything looks good there - it should be working. So where/how are you calling the theme function? Can you show the function that contains the call to the theme function?
And just to clarify, you are doing this in a module right?
Contact me to contract me for D7 -> D10/11 migrations.
Anyways, so far everything
Oh my, I thought it would be called automagically like the preprocess function. I really read the theming documentation where hook_theme is mentioned many times. But I still couldn't find a good clue about where I should call the function. Just found a link to hook_theme_registry_alter, which should also provide the ability to add template files, if I understood this correctly. I want the template to be rendered as the node's content. Please give me a hint on where to call the function.
Yes, that's right.
Well, you generate the
Well, you generate the content somewhere in your module. I can't really tell you where, because it will depend on the module. Then you call your theme function using theme([theme_function_name], $variable1, $variable2 ...);
The variables are defined in hook_theme. You then use the function that hook_theme defines to do whatever it is you want with the content being themed.
Contact me to contract me for D7 -> D10/11 migrations.
I can't follow you
I can't follow you completely, so I will try to make the scenario more simple and concrete.
I have mymodule.module, the corresponding .info file and the template file mymodule.tpl.php as the only files in my mymodule directory. Now I want mymodule.tpl.php to be rendered when I call www.example.com/mymodule as the node's content. Therefore I try to register the template by adding the mymodule_theme function to mymodule.module. But the template will be rendered only if I call theme('mymodule_theme', 'variable_visible_in_template'); somewhere in mymodule.module. The problem is that I don't know where to do so. Do I have to call it inside a certain hook? Some preprocess function?
I'll give you a simple
I'll give you a simple example, with all the chaff removed:
Now finally, we have mymodule_mypage.tpl.php:
Hopefully this helps. I didn't test any of it, so there may be syntax bugs, but for the most part it should give you the idea of how it works.
Contact me to contract me for D7 -> D10/11 migrations.
Solved
Hi,
It took me a while, but thanks to your code I do now understand a whole lot more how the menu system works. I remodeled your example to solve my problem and thereafter I understood the hook_theme_registry_alter like in a domino effect. So I came up with the following solution, as well.
ありがとうございます
So long, hama.
どういたしまして :D
どういたしまして :D
Contact me to contract me for D7 -> D10/11 migrations.