Drupal 7 hook_theme() not loading template file - drupal

I'm trying to get a very simple module to load a template file using drupal's hook_theme(). It's pretty much as simple as you can possibly imagine.
function sectionheader_theme ( $existing, $type, $theme, $path ) {
return array(
'sectionheader' => array(
'variables' => array( 'foo' => NULL ),
'template' => 'sectionheader',
),
);
}
The template is named sectionheader.tpl.php. The rest of the module is working as expected. I've cleared the Drupal cache. I've inserted a die("Debug") statement in this function, and it is being executed, but my template is simply not being called, ever. The template merely has some debug text in it so I can see that it's working, but is not visible in any view of the module.
I've done everything in every example I can find, I've even copied and pasted code directly from other modules, and this template will still not load.

Note, if you have put your template file in a /theme subfolder in your module dir ( which is best practice), you'll also need to specify the file path in hook_theme
function example_theme($existing, $type, $theme, $path) {
return array(
'example_function' => array(
'variables' => array('var1' => array(), 'var2' => array(), 'var3' => array()),
'template' => 'example-template',
'path' => drupal_get_path('module', 'example').'/theme'
),
);
}

I had the same problem as yours, and I solved it by clearing the cache, I searched from the database, and in the cid column of table cache, I get something like "theme_registry:*", and I remove them, it works.

As mentioned in the comment above I hit the same problem. Everything was working fine in a development module but when I simply copied this module into a new one that would become my production module the template file no longer worked. I tried everything mentioned above withut luck. The original module was disabled and only the new one was enabled.
I even went back to see if the original module's theme could work and it didn't. hmmmm.
When I changed the name of the theme it suddenly started to work: the template file was located and displayed.
So, it appears that any module that registers a theme name ---- even if it is disabled --- still registers a theme AND it seems that theme names need to be unique throughout the system.
Answer: look for the same theme name being declared in other modules

Related

Drupal 7 Creating Full Page Custom Module

I want to create a full page, custom module. By this, I mean a module that is not wrapped into the main Drupal sites them. Here is my code:
myapp.module:
function myapp_menu()
{
$result = array();
$result['myapp/home'] = array(
'title' => 'My App Title', // Title of our page
'description'=> 'My App Web Site', // Description of our page
'page callback' => 'myapp_function',
'access arguments' => array('access content'), // permission to access this page
'type' => MENU_NORMAL_ITEM, // type of menu item
);
return $result;
}
function myapp_function(){
return theme('my_custom_template');
}
function myapp_theme(){
return array(
'my_custom_template' => array(
'template' => 'myapp-page',
),
);
}
myapp-page.tpl.php
Hello World
The problem is that, when the page is displayed, it is still kept within the main Drupal Sites main theme. I would like to make this page its own, full page, site. Can anyone help with doing this?
thanks
You can hack like this :
function myapp_function(){
print theme('my_custom_template');
exit;
}
Also you can use declaration options listed in https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_theme/7.x
Parameters
array $existing: An array of existing implementations that may be used
for override purposes. This is primarily useful for themes that may
wish to examine existing implementations to extract data (such as
arguments) so that it may properly register its own, higher priority
implementations.
$type: Whether a theme, module, etc. is being processed. This is
primarily useful so that themes tell if they are the actual theme
being called or a parent theme. May be one of:
'module': A module is being checked for theme implementations.
'base_theme_engine': A theme engine is being checked for a theme that
is a parent of the actual theme being used. 'theme_engine': A theme
engine is being checked for the actual theme being used. 'base_theme':
A base theme is being checked for theme implementations. 'theme': The
actual theme in use is being checked. $theme: The actual name of
theme, module, etc. that is being being processed.
$path: The directory path of the theme or module, so that it doesn't
need to be looked up.

How do i set a specific template for a specific module in drupal 6 using hook_theme

Is there any way by which i could assign a template to my custom module.I heard it may be possible.I tried out with the hook_theme function.My hook_theme looks something like this
function special_theme() {
return array(
'special' => array(
'template' => 'special',
'arguments' => array('link' => NULL),
),
);
}
I do have a special.tpl.php file in my module folder.But the tpl file is not called.Its my default template that is been shown as output.Could someone please help me in the right direction.would be very helpful.
What you define via hook_theme() is an available template, not one that is automatically used. In order to use that template you need to call theme('special', $link);.
It is also advised to avoid using simple words for theme names to avoid collisions ( try mymodule_special instead ).
Also note (though basic), that you also need to print the return value of theme(), it does not get automatically printed. So for instance,
print theme('special', $link);

Register your theme functions in Drupal

I am new to Drupal and I am working on creating my own theme for our custom module. Can any one please help me understand how to register our theme functions or share any ideas or links that explains the process from scratch.
Register a theme function means implementing hook_theme in your module.
For example, if your module is called "example", then you need to have a function called example_theme in the example.module file. The theme function must return an array or you'll end up with the famous white screen of death.
In example.module:
<?php
// $Id$
// Implements hook_theme
function example_theme(){
return array(
'mydata' => array(
// Optionally, you can make the theme use a template file:
// this line references the file "mydatafile.tpl.php" in the same folder as the module or in the folder of the active theme
'template' => 'mydatafile',
// these variables will appear in the template as $var1 and $var2
'arguments' => array(
'var1' => null,
'var2' => null,
),
),
'myotherdata' => array(
// these variables will appear in the functions as the first and second arguments
'arguments' => array(
'var1' => null,
'var2' => null,
),
)
);
}
// If you don't want to use a template file, use a function called "theme_THEID" to create the HTML.
function theme_myotherdata($var1, $var2){
return "<div>var1= $var1 and var2= $var2</div>";
}
In mydatafile.tpl.php:
<div>mydatafile.tpl.php was called</div>
<ol>
<li>var1: <?php echo $var1; ?></li>
<li>var2: <?php echo $var2; ?></li>
</ol>
You can then later call the theme function manually if needed:
$html = theme('mydata', 'hello world', 123);
$html = theme('myotherdata', 'hello world', 123);
In which case "mydatafile.tpl.php" and "theme_myotherdata" will receive the value "hello world" in $var1 and the value 123 in $var2.
There are many more options, like changing the name of the function, using patterns instead of a fixed name, being able to have the function in another php file or such, check out the link.
Here are a couple more ressources about theming:
The theme guide
Overriding themable output
The Devel module
The Theme developer module (requires the Devel module)
a longuer example that also creates a form/page to use the theme function
By the way, you will need to rebuild the theme registry cache if you add the functions in the .module file after it has been installed, in which case you can do so by clearing the cache (one way is using the button at the bottom of the the Performances page).
NOTE: Slightly different syntax with Drupal 7 (ex. 'arguments' changes to 'variables')
http://www.davidcalculli.com/blog/2012/01/drupal-7-custom-template-only-displaying-the-first-character

Drupal: How to theme a module

I'm trying to theme a modules output.
In particular i'm working on http://drupal.org/project/service_links
Any idea how that works?
Thanks in advance!
Generally, if you want to theme a module you have a few options.
Overwrite theme functions. You can overwrite the theme functions that the module uses/implements to change the markup, one example of such a function is theme_service_links_node_format. You change make a function in your theme's template.php called 'your_theme_name_service_links_node_format' and make your custom markup in it instead.
CSS. If you don't need to change the actual markup of a modules output, you only need to add the needed css, to theme it into your liking.
In some cases, it doesn't look like sercive links is such a case, you can also make your own templates, and make Drupal use them instead.
Another way, again it doesn't look like service is service links is such a case, is to implement preprocess functions in your template.php. This is needed if you want to alter how certain template variables are generated.
If you want to implement your own theming function services links defines 3 themables. In your theme you should imlement the following
yourtheme_service_links_build_link()
yourtheme_service_links_node_format()
yourtheme_service_links_node_format()
'service_links_build_link' => array(
'arguments' => array(
'text' => NULL,
'url' => NULL,
'title' => NULL,
'image' => NULL,
'nodelink' => NULL,
),
),
'service_links_node_format' => array(
'arguments' => array('links' => NULL),
),
'service_links_block_format' => array(
'arguments' => array('items' => NULL),
),
Have a look at http://drupalcode.org/viewvc/drupal/contributions/modules/service_links/service_links.module?view=markup line 389 and below
What's the problem? I mean, every module should use a different name for main container and so. You can use css selector in clever way to refer the template pages.
For example, the FAQ module use identificator to all part of html output, like faq-question and faq-answer in the main page.
Just inspect your resulting code and css it, if possible modify the module-related css!
If the module implements its own theme hooks you can use that. You can also use CSS.

Drupal theme functions workflow in module

I am a bit newbie in Drupal theming and I can't get one detail in Forum modules theming.
forum.module file contains forum_theme function that controls how this module is themed
and has this line
function forum_theme() {
......
'forum_list' => array(
'template' => 'forum-list',
'arguments' => array('forums' => NULL, 'parents' => NULL, 'tid' => NULL),
),
I also see forum-list.tpl.php file in forum directory, so I start to wonder when this file is called and where it gets data from, but all I can find in forum.module is this function.
function template_preprocess_forum_list(&$variables)
Am I missing something? So in general my question is who and when invokes custom registered theme function, like forum_list
Simple answer is if you in your theme directory put mytheme-forum-list.tpl.php (where mytheme is the name of your theme) and customise it drupal should pick it up (clear the cache first).
This line in template_preprocess_forum calls the Drupal theme function
$variables['forums'] = theme('forum_list',
$variables['forums'],
$variables['parents'],
$variables['tid']);
This will reference the line in forum_theme()
'forum_list' => array(
'template' => 'forum-list',
'arguments' => array('forums' => NULL, 'parents' => NULL, 'tid' => NULL),
),
Which tells the templating enging to look for forum-list.php and provides arguments.
If you install the devel module and turn on the theme developer module. This will show you all of the candidate templates and functions which Drupal will look for when rendering content.
In general (but with specific exceptions) Drupal looks for the best match template and falls back to the pre defined functions.
if there is nothing that matches. Have a look at the theme guide and in specific the section on Overriding themable output you may also find hook_theme of interest.

Resources