While working with Commerce in the last year, we had a lot of benefits from working in an object oriented approach, extending EntityDrupalWrapper for almost every Entity Type in Commerce and creating a lot of improvements that made our development work a lot easier.
Things like $order->getLineItems(), $order->getLastTransaction(), and a lot of other possibilities are making our code a lot more readable too, that makes me think: could this be useful to be considered when refactoring Commerce for Drupal 8, the 2.x branch?
We have a concrete code to show, that is the MoIP module: http://drupal.org/project/moip. MoIP is a payment provider from Brazil, so this module makes the integration with Drupal Commerce.
We have our MoipDrupal class(http://drupalcode.org/project/moip.git/blob/HEAD:/includes/MoipDrupal.inc), that make everything in a highly granulated way, making it easy to extend for each projects specific needs.
To make Drupal aware of this extended classes, we use Reflection, a great tool already available in newer versions of PHP, where we let users specify in the admin form which class will be used:
$moip_classes = array('MoipDrupal' => 'MoipDrupal');
foreach (get_declared_classes() as $class_name) {
$reflection_class = new ReflectionClass($class_name);
if ($reflection_class->isSubclassOf('MoipDrupal')) {
$moip_classes[$class_name] = $class_name;
}
}
$form['advanced_settings']['moip_class'] = array(
'#type' => 'select',
'#title' => t('MoIP Class'),
'#options' => $moip_classes,
'#description' => t('MoIP PHP Class to use in this site. It is useful if you have some special use case to deal with.'),
'#default_value' => variable_get('moip_class'),
);
And to make this works, here is our hook_api_redirect_form() (http://drupalcode.org/project/moip.git/blob/HEAD:/moip.module):
function moip_api_redirect_form($form, &$form_state, $order, $payment_method) {
try {
$moip_class = variable_get('moip_class', 'MoipDrupal');
$moipDrupal = new $moip_class($order);
$moipDrupal->processOrder();
} catch (Exception $e) {
drupal_set_message($e->getMessage(), 'error');
}
}
I think that this approach could make Drupal Commerce API even more awesome.
Comments
Comment #1
bojanz CreditAttribution: bojanz at Centarro commented