hook_form_alter - in module or in template.php - drupal

I wanted to perform a quick alteration of ubercart form data (I want to remove links to product nodes in the shopping-cart). I found a solution that involves implementing hook_form_alter() by creating a new module (link and code below).
My question is, do I have to make a new module or can I just add this function into my theme's template.php file? I tried the later but I couldnt get it to work (I renamed the function to theNameOfMyTheme_form_alter).
(The bigger issue here is me trying to get my head round all this drupal 6 override stuff. Eg if I come across some code that I want to alter, how can you tell if you are supposed to create a module or change your theme?).
http://www.ubercart.org/forum/support/2298/remove_product_links_shopping_cart
function your_module_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'uc_cart_view_form') {
foreach($form['items'] as $key => $item) {
if(!empty($item['desc']['#value'])) {
$form['items'][$key]['desc']['#value'] = strip_tags($item['desc']['#value']);
}
}
}
}

Adding to Clive's solution, just to confirm for anyone else looking for this: hook_form_alter isn't called for themes in Drupal 6.

hook_form_alter() is one of the hooks that's invoked for both themes and modules so putting it in your template.php file is fine.
It's actually documented to that effect on the version of the function for Drupal 7, but I guess they missed it on the Drupal 6 version.
Remember to clear Drupal's cache once you've added the hook so it gets picked up.

Related

theme_status_messages() is not firing in my Drupal 6 theme

I have a template.php file with a theme called
themename_status_messages
It's not being called/invoked by my theme.
When I use devel themer info on a test dsm output, I am told the candidate functions are:
theme_messages_alter_status_messages()
themename_messages_alter_status_messages()
I'm not sure why the status_messages() call isn't being called during page load. Any ideas?
Looks like the problem was there was a module enabled that changed how this was handled, and I didn't know the module was there and did that. The module was Messages Alter. Taught me a good less on in check the modules page for mysteries.
On your page.tpl.php, is there a $messages var being printing on the page anywhere?
A better way to see if $messages are being passed to your theme is to use a THEME_preprocess_page(&$vars) function in template.php:
function THEME_preprocess_page(&$vars) {
dpm($vars);
// or use $dpr($vars) for a textual array printout
// Replace 'THEME' in the function name with the name of your theme.
}

Wordpress plugin functions: check if function exists

I'm developing a Wordpress site that relies on a plugin to be activated for the site to function properly.
The plugin has a few useful functions that I'm using in the site's template files. When the plugin is active, everything works perfectly. If the plugin is deactivated, the content doesn't load.
Wrapping these functions in if(function_exists(...) obviously fixes that, but I'm wondering if there's a cleaner way of doing that in Wordpress. Is there a function that can be placed in the theme's functions.php file that can check if these functions are available every time I call them, and if not provide a safe fallback without me having to wrap them in the function_exists()?
Thanks.
If you're only using it sparingly (1-2 times), use if( function_exists() ). If you're calling the function several times through in different template files, I'd suggest using something like
In your functions.php
function mytheme_related_posts( $someparams = nil ) {
if( function_exists( 'related_posts' ) ) {
related_posts( $someparams );
} else {
echo 'Please enable related posts plugin';
}
}
Then use mytheme_related_posts() in your template.
I think this is the most clear way. It prevents all problems. I think you can write a function instead which can check if these functions are available every time you call them, but I'm almost sure it can cause you more trouble and it burns more memory then simply using if(function_exist()). Don't forget the else branch and it will work fine.
If you want to check if a plugin is active then you should be using the is_plugin_active() function - you can find the docs at: http://codex.wordpress.org/Function_Reference/is_plugin_active
You can then also use if(function_exists()) as well just to doubly make sure :)

Why is Drupal not aware of my template file?

this is a question how to override themable items in Drupal 6.
According to the book "Pro Drupal Development", we can override themable items in two ways:
overriding via Theme functions
overriding via Template files
So for example, in order to reformat the breadcrumb, I can:
via function theme_breadcrumb($breadcrumb)
via breadcrumb.tpl.php
But on my local testing server, the second approach (i.e. via template file) is not working! I see no breadcrumbs at all, while the first approach works fine.
Any idea how could this happen? any special settings I need to configure my Drupal?
thanks!
My custom theme "greyscale":
sites\all\themes\custom\greyscale:
- breadcrumb.tpl.php
- greyscale.info
- node.tpl.php
- page.tpl.php
- style.css
- template.php
relevant file contents:
* template.php:
function phptemplate_preprocess_breadcrumb(&$variables) {
$variables['breadcrumb_delimiter'] = '#';
}
breadcrumb.tpl.php:
Theme functions are setup to either use a template or a function to generate the markup, it will never use both as that's pointless.
For a theme function to use a template, it must be defined when you define it in hook_theme.
A template + preprocess function and a theme function really does the same thing: produce markup. It depends on the situation which method is best to use, that's why we have two. The good thing about templates, is that it allows themers to change the markup, without know much about PHP or Drupal.
Cache
Drupal caches all templates and theme functions defined in your theme, when you create new ones, you need to clear the cache, this can be done by:
Use drush
Clearing cache in admin/settings/performance
Use devel to clear it on each page load. Usable during development, biut will kill performance.
Switching theme back and forth will work too, but it really not the desired way to do it.
I personally always find it easier to alter breadcrumbs through template.php using hook_breadcrumb()
function mytheme_breadcrumb($breadcrumb) {
$sep = ' > ';
if (count($breadcrumb) > 0) {
return implode($breadcrumb, $sep) . $sep;
}
else {
return t("Home");
}
}
Any particular reason why you wish to use a .tpl.php file?

Select a template through the admin area?

Up to now, I've always hard coded what page template a certain page should use, either based on the URL, or what type of node it is.
What would be really useful is if there was a way to select which tpl file to use, right there in the node edit form. This would allow the user to flick between different page layouts at will.
Does anyone know a good way to approach this problem, or a flat out solution to it?
ThemeKey will let you load a theme based on a path or other criteria. You can use other methods like utilize preprocesser functions of template.php, and hook it in with hook_form_alter and come up with a way to switch files.
I ended up adding a new vocabulary for template files (the VID for this is 2 in my case) , and then rolled this into the page preprocessor in my template.php:
function phptemplate_preprocess_page(&$vars) {
if (count($vars[node]->taxonomy)>0)
foreach ($vars[node]->taxonomy as $term)
$template = $term->vid == 2 ? $term->name : NULL;
if ($template) $vars['template_files'][] = "template-".preg_replace("/[^a-zA-Z0-9s]/", "", strtolower($template));
}
Now if I have a node in a taxonomy term called: A Green Page! it will look for template-agreenpage.tpl.php as a template file.
I was wanting this functionality as well, so I made a module to do this very thing for node templates. You can find it here: http://drupal.org/project/template-picker

Extending Contact Form 7 Wordpress plugin by using hooks

I would like to create a plugin that uses the contact form 7 hook, wpcf7_admin_after_mail. I want to use the plugin to interface with a CRM system. What I have thus far is the following:
//plugin header here
function add_to_CRM( $cf7 )
{
if (isset($cf7->posted_data["your-message"]))
{
full_contact($cf7);
} else {
quick_quote($cf7);
}
return $cf7;
}
add_action('wpcf7_admin_after_mail', 'add_to_CRM');
//other functions here
I can't seem to get this working. I can't even get the hook to work and do something like mail me. Anybody have any idea what I'm doing wrong here. Since I have limited Wordpress experience I might me missing the boat completely with what I'm trying to do here. I've Googled for answers to no end.
EDIT: I ended up adding this to the theme's functions.php file and it works perfectly. Thing is, I want to get it working as a plugin. Any help will be appreciated.
Try delaying the add_action() call, something like;
add_action('init', create_function('',
'add_action("wpcf7_admin_after_mail", "add_to_CRM");'));
This actually registers your CF7 hook once WordPress is ready (which is nearer the time functions.php gets loaded in).

Resources