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

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);

Related

How to add attribute class in theme('pager') in drupal 7?

How to add attribute class in theme('pager') in drupal 7 ?
my code is following:
$directory_table .= theme('pager', array('quantity', $STAFF_COUNT ),array(
'element' => array(
'class' => array('pagination pull-right'))));
but it is not applying the class to the pager.
As the theme_pager function only expects 1 parameter (an array of variables), so your theme function call should look like this:
$directory_table .= theme('pager', array('quantity' => $STAFF_COUNT, 'element' => array('class' => array('pagination pull-right'))));
BUT:
It seems that theme_pager just sets a 'pager' class ignoring any variables which you may send to the function.
So, your best bet is to just override the theme_pager function. Do this by just copying it to your themes template.php file and renaming the function to MYTHEME_pager. Then you can just add the classes you need in that function, Or add the logic you need to add the classes depending on a variable sent to the function.
Remember to clear the theme cache after overriding the function.

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 form with custom ID

Correct me if I'm wrong, after reading drupal fapi related articles, I got the impression that fapi generates 'id' attributes by itself. It allows developers to assign 'name' attribute only. If that's the case, is there a way I can set desire 'id' value for elements? Because, I want my elements to have meaningful 'id' so that html/jquery code would be easier to read as well as save my time from going through already written jquery code to change those all 'id's that I've used inside.
P.S:drupal version - 6.x
Ok found the solution. I can use the #attributes key of the $form element to set any additional attributes (such as class, id, etc.). Thanks for your help so far.
I had a similar issue to deal with. I needed to have multiple forms on the same page so I had to change the ids of the form and its elements to prevent duplicate ids. I did something like the following:
function voci_comment_form($form, &$form_state, $cid) {
$form['#attributes']['id'] = 'voci-comment-form-' . $cid;
$form['#attributes']['class'][] = 'voci-comment-form';
$form['body'] = array(
'#title' => 'Post a comment',
'#type' => 'textarea',
'#resizable' => FALSE,
'#rows' => 1,
);
$form['comment'] = array(
'#type' => 'submit',
'#value' => 'Comment',
);
foreach ($form as $k => &$element) {
$k = str_replace('_', '-', $k);
$element['#attributes']['id'] = "edit-$k-$cid";
$element['#attributes']['class'][] = "edit-$k";
}
return $form;
}
This basically sets unique ids based on the $cid that is passed in. The code also adds classes to each element in the form so you can style it easily. I'm sure a more robust solution is possible but this is the basic idea. Tested in Drupal 7.
It's true that you can set $element['#attributes']['id'] and that will apply to the form field. However, it will break labels and #states in Drupal 7 because the rest of the rendering pipeline reads the ID from somewhere else. So for your labels and #states to keep working, use set the ID to $element['#id'] instead (an undocumented property that nonetheless is how the form API watches ID internally).
Make sure to pass your ID through drupal_html_id as well to ensure no conflicts.
This problem doesn't really have much to do with the Drupal-FAPI itself, but more with how Drupal theme forms (create the markup).
If you want to alter all forms on your site, you can overwrite the theming functions that is used for forms and the different type of form fields.
If you just want to overwrite some forms or form fields, you can set the #theme attribute on the form or an element, to change which function should be used for creating the markup.

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 6 passing variables from Forms to Content, how to?

I created a BLOCK (left) with this simple form.
Now I want to PROCESS and DISPLAY results on PAGE (center)
How can I do it ?
inputs:
name = James
surname = Bond
output I want :
<div style="color:red">Welcome, James Bond</div>
here is a BLOCK which i wrote and works.
<?php
echo drupal_get_form('myForm');
function myForm($form_state) {
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#size' => 20,
'#maxlength' => 10
);
$form['surname'] = array(
'#type' => 'textfield',
'#title' => t('Surname'),
'#size' => 20,
'#maxlength' => 10
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save')
);
return $form;
}
function myForm_submit($form,&$form_state)
{
//??
};
Now I need to display the output :).
Please don't suggest to use VIEWS or any other addon.
I want to learn Drupal from Inside out. Not the other way around ;)
Well, it depends a bit on how you want to do things. Since you are learning how to make a drupal module, you might want to start with an implementation of hook_menu(). This hook is used to define menu items, which basically means that you can register urls with that function. Going that route you can:
Implement hook_menu()
A general way of handling redirects is using drupal_goto(). However, in this case it is much more fitting to use the $form_state['redirect'] as Henrik explained in his comment.
For the url you are redirecting to, you should have a call back function which is where you put your logic, the way you setup the hook_menu and the callback function will determine how you get your variables available. You probably want to look into the arg() function which is what generally is used to get the values from the url.
Run the user input through a filter to make sure that they haven't posted nasty stuff like script tags ect, use check_plain
return a theme function, alternatively make your own, look at theme() and hook_theme()
There are quicker ways to do this, but doing it this way, you will generate urls for every search result that drupal can cache, which is nice. Also not being dependent on the post parameters people can bookmark the search results
Another thing is that you might want to put some basic validation to your form. That would be a good practice to learn. That would look something like this:
/**
* Validation handler for myForm.
*/
function myForm_validate($form, &$form_state) {
$name = $form_state['values']['name'];
// do some checks to $name.
if ($error) {
form_set_error('name', t('error message to be displayed, showing the value of the field: #name', array('#name' => $name);
}
};
You could implement AHAH in your form, and specify an element inside your page's content area as the 'wrapper' (the element in which the results of the callback function will be placed). But, you would need to understand the excellent advice of Mr. Opel before you even attempt it.
How about using drupal_set_html_head() to write a string of script to the head section of the page? I am doing this on specific pages (getting user latitude and longitude and passing them into my gMap function), but I am interested in dong the same thing directly from hook_form_submit(). I have made a few tries at it, and I am obviously outputting the script string but calling the function from submit doesn't seem to work.
if the submit function creates a page full of html output; what is the best way to pass this to a page callback? i doubt that passing as an arg on the url would work.
i stuffed into a session var but maybe a better way?

Resources