9

Firstly, sorry if this answer is covered elsewhere. I've done a lot of searching and can only find info on overring theme functions and hooks.

I am using a module that builds a price table for Drupal Commerce items. There is a function that formats the table headers:

/**
 * Helper function that takes care of the quantity displayed in the headers of 
 * the price table.
 */
function commerce_price_table_display_quantity_headers($item) {
  // Set the quantity text to unlimited if it's -1.
  $max_qty = $item['max_qty'] == -1 ? t('Unlimited') : $item['max_qty'];
  // If max and min qtys are the same, only show one.
  if ($item['min_qty'] == $max_qty) {
    $quantity_text = $item['min_qty'];
  }
  else {
    $quantity_text = $item['min_qty'] . ' - ' . $max_qty;
  }
  return $quantity_text;
}

As you can see, this is not a theme function where I can override it in template.php but I can to tweak some of the output.

Obviously I don't want to edit the module itself in case it gets updated in the future, so, how can I redefine this function so I can chop and change a few things?

My work so far...

So far, I have tried to create it as a seperate module with a few subtle changes to show if it's working or not, but it's not overriding any of the output.

Info file

; $id$
name = Price Table: Tweaked Display
description = A different layout for the price table as shown on the product display nodes
package = Commerce (contrib)
core = 7.x

dependencies[] = commerce_product
dependencies[] = commerce_price
dependencies[] = commerce_price_table

Module File

 /**
 * Override of the helper function that takes care of the quantity displayed in the headers of 
 * the price table.
 */
function commerce_table_tweak_display_quantity_headers($item) {
  // Set the quantity text to unlimited if it's -1.
  $max_qty = $item['max_qty'] == -1 ? t('Unlimited gnhh') : $item['max_qty'];
  // If max and min qtys are the same, only show one.
  if ($item['min_qty'] == $max_qty) {
    $quantity_text = $item['min_qty'];
  }
  else {
    $quantity_text = $item['min_qty'] . ' - this is working - ' . $max_qty;
  }
  return $quantity_text;
}

3 Answers 3

12

It's Drupal...there's always a way, but the amount of time it will take to do might make you think twice :)

If you look a bit further up the food chain so-to-speak you'll see that this function is exclusively used by commerce_price_table_field_formatter_view(), which declares a field formatter that is used for the commerce_price_table field type.

With that in mind, you can quite easily implement your own field formatter, assign it to the commerce_price_table field type, and use as much custom code as you want, all the time keeping in line with best practices.

Basically you need to implement hook_field_formatter_info():

function MYMODULE_field_formatter_info() {
  return array(
    'MYMODULE_commerce_multiprice_default' => array(
      'label' => t('MyModule Price chart'),
      'field types' => array('commerce_price_table'),
      'settings' => array(
        'calculation' => FALSE,
        'price_label' => t('Price'),
        'quantity_label' => t('Quantity'),
        'table_orientation' => t('Orientation'),
      ),
    ),
  );
}

And then implement hook_field_formatter_view(), field_formatter_settings_form() and (optionally) hook_field_formatter_summary().

For each of these functions just take the code from the same function in the contrib module, and make the changes where you need to.

4
  • Thanks for a great answer. I'll wade through the code and see if it's the kind of job my mind can handle on a friday afternoon!
    – user9359
    Commented Aug 17, 2012 at 15:12
  • @Clive, you answer is absolutely correct from the point of view of best practice of Drupal development. But in case when you only need to change one small string in some function - it is not good approach to create custom formatter. Because the more custom code you write - the more bugs you add. And you suggest user9359 to create 4 hooks most of them will be copypasted from existing commerce module!!! I think that using small patch is much more appropriate for this situation. Commented Aug 21, 2012 at 16:48
  • 2
    @Eugene Yeah it's a judgement call really, each person would probably have a different definition of 'appropriate' in this situation. Personally I prefer the long method as it means I don't have to maintain patch files, and any fancy version control logic I have won't get affected by this rogue 'changed' module file; but that's just me, if you're comfortable maintaining patch files then it would be a lot less effort to do that than re-implement all this functionality. This answer was definitely along the lines of the how, not necessarily the why :)
    – Clive
    Commented Aug 21, 2012 at 16:58
  • again clive to the resuce
    – vishal
    Commented Oct 12, 2013 at 7:22
2

Seems, you can’t override this function because it doesn’t use theme or hook workflow.

The only way - is to directly change commerce_price_table_display_quantity_headers() function. Then create a patch with you changes.

Later if you update the Commerce module - you will need apply your patch.

1
  • Yeah, that's what I was trying to avoid but it does seem tempting after a quick look at Clive's suggestion!
    – user9359
    Commented Aug 17, 2012 at 15:13
2

I think Eugene’s answer is correct that you can't do it without overwriting directly.

However, what I have found useful is that if it’s absolutely necessary to do this, move this module from your sites/all/modules/contrib directory to sites/all/modules/custom dir so that you can be aware of and keep track of the fact that you made custom changes.

1
  • Yeah thanks for the tip, I'd read about that earlier
    – user9359
    Commented Aug 17, 2012 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.