How To Override a Contributed Module’s Function

I wanted to change the message of the add-to-cart confirmation popup message on the gench site.

I’m using the Commerce Kickstart distribution of Drupal. Utilizing the Theme Devel module, I can see that a contributed module named commerce_add_to_cart_confirmation is delivering the message text I want to change.

Thanks to my newly minted DrupalizeMe knowledge, I know I can override a contributed module’s function(s) from the theme layer. So I find the function responsible for the message in profiles/commerce_kickstart/modules/contrib/commerce_add_to_cart_confirmation/commerce_add_to_cart_confirmation.rules.inc. I copy & paste the function into my theme’s template.php file. I preface the name of the pasted function with the name of my theme followed by an underscore (genchstyle_). I change the message text, save my new function, clear my cache and load the confirmation message: no change. Why?

I must be doing this wrong, so I re-watch the DrupalizeMe lesson on this. It reminds me that theme overrides of module functions rely on the module creator creating a theme hook function for the module. It turns out my version of commerce_add_to_cart_confirmation doesn’t include a theme hook – and that is why my override attempt failed.

Fortunately I found a patch that delivers a theme hook for this function. Once I applied that, I retraced my earlier steps and this time it worked!

I really love Drupal Commerce and all of its contributed modules, so I am surprised to learn that such a popular module can lack theme hooks initially. I don’t mean this as a criticism of the module developers. This may not have been an oversight on their part: they may have had to prioritize features to get the module out as quickly as possible. I’m mentioning this because as beginners, we assume we know nothing and are totally fallible. By the same token, we assume advanced practitioners know everything and are infallible. Neither assumption is correct.

Going forward: if my override function doesn’t work, I’ll check the contributed module’s files for a theme hook. If one doesn’t exist, I’ll look for a patch or upgrade that provides a theme hook. If I can’t find one, I’ll contribute a patch myself.

Screen Shot 2013-08-09 at 11.47.38 AM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s