Register your theme functions in Drupal - 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

Related

Silverstripe date and image error

This is the .ss template file:
<ul>
<% control Menu(1) %>
<li>$MenuTitle</li>
<% end_control %>
</ul>
<div>
<h1>$Title</h1>
<div>Date : $Date.nice</div>
$layout
$Content
</div>
This is the php file:
class ArticlePage extends Page {
static $db = array(
'Date' => 'Date',
);
private static $has_one = array(
'SingleImage' => 'Image'
);
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Content.Main', new DateField('Date'), 'Content');
$fields->addFieldToTab(
'Root.Upload',
$uploadField = new UploadField(
$name = 'SingleImage',
$title = 'Upload a single image', "Content"));
return $fields;
}
}
The date and image are not showing up on the page.
This could be any number of things.
It could be that you have not set the page type properly in the CMS.
It could be that you have not flushed your template cache.
To do this append ?flush=all to the end of your URL.
It could be that you have not done either of the above and have not completed a database build yet either.
To do this visit yoursite/dev/build.
It could be that you have not named the template correctly. It must mirror the php Class name (ArticlePage.ss)
It could be that you have not put this in the correct place. It must be in a templates folder in either the selected theme or your project folder. A flush is required after relocating the file (see above).
If you are using a theme it could be that you do not have the correct theme selected. See the documentation on this.
It could be because you're not being diligent with your code. The dot method for formatting a date nicely is .Nice, not .nice. $Date.asdf would also cause nothing to show up - as you are describing. Template variables fail silently if there is no sensible value to output.
Similar to the above - the layout variable is $Layout, not $layout (although this is irrelevant to this particular problem). $Layout also only works in 'main' templates (in the base 'templates' folder, not in the 'Layout' subfolder).
If all of the above checks out, then it could be simply that you have not put any information in through the CMS yet. You must of course complete this step before anything will show 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);

Drupal 7 hook_theme() not loading template file

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

Drupal 7: how to add template file to /node/add/content-type

In Drupal 7.
I want to themming /node/add/content-type by template file as page--node--add--content-type.tpl.php.
Please help me.
You need to use
page--node--add--content_type.tpl.php
instead of
page--node--add--content-type.tpl.php
underscore(_) instead of dash(-)
You will need to add the template as a suggestion in a preprocess function or you can create a custom theme function for the form, that will use the desired template. This is much similar to how it was done in Drupal 6, only you can do form_alters in your theme now.
you can add this function to your template.php:
you can print_r() your form and see what are the fields.
function yourThemeName_form_yourFormID_alter(&$form, &$form_state, $form_id) {
// Modification for the form with the given form ID goes here. For example, if
// FORM_ID is "user_register_form" this code would run only on the user
// registration form.
print_r($form);
}
You are adding tpl file for node form. For this you have to define tpl file name in template.php under your theme.
First create your_theme_name_theme()
Print content on tpl form.
Example: In below example contact_site_form is the form id of content type.
your_theme_name_theme() {
$items['contact_site_form'] = array(
'render element' => 'form',
'path' => drupal_get_path('theme', 'your_theme_name') . '/templates',
'template' => 'contact-site-form',
);
}
now create a file contact-site-form.tpl.php in your path location as specify above.
<?php echo render($form['name']); ?>
<?php echo render($form['mail']); ?>
.
.
.
<?php print drupal_render_children($form); ?>
You can render each element separately in given method.

Drupal theme() function and custom template

I have a custom module that returns data from a web service call. It comes back from an XML response, which I am converting to an array.
Once I have the array, I do:
$output = theme('search_srs_results', $data);
return $output;
But I am getting a white screen. No apache/php/watchdog errors.
I've done this before in another module without any difficulty. My theme hook is defined, and points to a template file, passing the $data argument. If I dump $output before it's returned, its NULL.
$data definitely has a populated array before being themed.
If I do theme('item_list', $data);, it renders, no white screen.
I tried reading the docs again on hook_theme and theme() but I don't seem to be doing anything wrong.
Here are the theme functions:
/**
* Implementation of hook_theme()
*/
function srs_finder_theme() {
return array(
'search_srs_results' => array(
'template' => 'srs-finder-results',
'arguments' => array('data' => null),
),
);
}
/**
* Implementation of hook_preprocess()
*/
function srs_finder_preprocess_search_srs_results(&$vars) {
$data = $vars['data'];
}
Whats missing?
I don't understand why you need hook_preprocess() function at all. $data should automatically be available to srs-finder-results.tpl.php. Thats because you're passing this variable in the call theme('src_src_results', $data) and the fact that you have declared that there is 1 argument in hook_theme().
srs-finder-results.tpl.php file should reside in the src_finder module folder. You need to implement the code for that! (Alternatively as nikit has commented above, provide a theme_search_srs_results function. In that case you will need to remove the template array entry)
[Note: If other users of the module want to override this theme template they can always provide their own implementation of srs-finder-results.tpl.php in the theme folder of the theme that is active.]

Resources