3

I would like to integrate the Barcode (http://drupal.org/project/barcode) module functionality into a custom shipping module that I am creating.

I would like to make calls the genBarCode method or something similar so I may generate barcodes within my module.

Is this ok within the context or custom/contrib modules and if so how can it be done?

8

There are two issues:

  1. Is the function you're going to call intended to be an API function? In general, functions that are not intended to be called by other modules are prefixed by an underscore as in _mymodule_private_function().
  2. Is the function in a file that is guaranteed to be loaded when you need it to be. If it's in a .module file and the module is enabled, then it will be available. If it's in a class that's declared in a file listed in a files[] declaration of an enabled module's .info file, it will also be available. If it's a regular function in a .inc file, you may have to force it to be loaded with module_load_include() or require_once() or similar techniques.

How do you do it? Just call the function the same way as that module calls the function, the same way as you call any Drupal core function.

Don't forget to make the module a dependency of your module.

1

The Barcode defines a CCK field, so I recommend going the CCK path: a barcode field in your content type, an appropriate widget to set values and an appropriate formatter to display them.

If you want to do a bit of a theming and put the barcodes where you want them within your node output, you can use

print content_format('field_my_barcode', $field_my_barcode[0]);

More info at Theming Individual CCK Fields.

If you want to run free, however, and put random barcodes at random places, you can leverage the existing formatters and use the following snippet:

if (module_exists('barcode')) {
  print theme('barcode_formatter_default', array('#item' => array('barcode' => '0123456789')));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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