0

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']);
      }
    }
  }
}
spiderplant0
  • 3,872
  • 12
  • 52
  • 91

2 Answers2

4

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.

Community
  • 1
  • 1
Pete Watts
  • 1,014
  • 10
  • 10
3

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.

Clive
  • 36,918
  • 8
  • 87
  • 113
  • Thanks Clive +1. For some reason themeName_form_alter doesnt get called (I added a dd() at the top of the function to check). In the end I overrode themeName_uc_cart_view_form() which generates the HTML for the shopping cart. – spiderplant0 Aug 10 '12 at 09:23
  • Hi Clive, i have tried hook_form_alter in my custom module. But it is not working. is there any specific reason for this issue ? – Ananth May 07 '14 at 12:09