Early on, I standardized everything defined or returned from a load function in Drupal Commerce to be an object. This was for simplicity's sake - a developer could always trust that the array of checkout panes, a single product type definition, etc. when requested would be returned in a standard form. To do this, I had to use some creative object merging to apply default properties to these objects when loaded:

  $checkout_pages[$page_id] = (object) ((array) $checkout_page + $defaults);

Obviously, if checkout pages were defined as arrays, a simpler solution would've worked:

  $checkout_pages[$page_id] += $defaults;

I wasn't too worried about the complexity, though, as casting objects from arrays like this is possible in PHP for a reason. ; ) However, it can be a little problematic when you have to use odd syntax for things defined in the Commerce modules specifically and not the rest of Drupal. Damien's requested that instead of objects, anything returned by hook_*_info() be represented as an array. This means your major entities, Products, Orders, Line Items, etc. would still be objects, but secondary items like Product Types, Line Item Types, Payment Methods, etc. would be arrays. It will simplify DX in some ways (no casting necessary to merge / iterate over properties) and revert to the complication of Drupal in others (really, it is easier to remember it's always an object). However, it seems like a documentation page could be written that lists every item defined by a hook_*_info() so developers would know when to expect an array vs. an object.

What do you think? If we're to change it, I'd like it to be in one fell swoop so the syntax of the hook_*_info() implementations, the "listing" functions (i.e. commerce_checkout_pages(), commerce_product_types()), and the single loading functions (i.e. commerce_checkout_page_load(), commerce_product_type_load()) is uniform.

Comments

pcambra’s picture

Assigned: Unassigned » pcambra

Here there are the info implementations to change:

Checkout
commerce_checkout_page_info #1042630: Convert commerce_checkout_page_info to array
commerce_checkout_pane_info #1042736: Convert commerce_checkout_pane_info to array

Commerce.module - Currencies
commerce_currency_info #1043440: Convert commerce_currency_info into array

Payment
commerce_payment_method_info #1044132: Convert commerce_payment_method_info to array
commerce_payment_transaction_status_info #1046182: Convert commerce_payment_transaction_status_info to array
commerce_payment_totals_row_info - Already an array.

Order
commerce_order_state_info & commerce_order_status_info #1046324: Convert commerce_order_state_info & commerce_order_status_info to array

Line items
commerce_line_item_info #1046432: Change commerce_line_item_info to commerce_line_item_type_info - only replace by hook_commerce_line_item_type_info
commerce_line_item_summary_link_info #1046882: Convert commerce_line_item_summary_link_info to array

Customer
commerce_customer_profile_info - only replace by hook_commerce_customer_profile_type_info #1046926: Replace commerce_customer_profile_info by commerce_customer_profile_type_info

Product
commerce_product_info - only replace by hook_commerce_product_type_info #1046936: Replace commerce_product_info by commerce_product_type_info

Tax
commerce_tax_type_info & commerce_tax_rate_info #1047314: Convert commerce_tax_type_info & commerce_tax_rate_info to array

As finish step we should see if we should remove commerce_sort_weight for sorting arrays of objects as drupal_sort_weight would probably be enough

pcambra’s picture

Status: Active » Needs review
rszrama’s picture

Removed the weight sorting function, though I'll be interested to see if we ever end up with a need for it again. : P

I've e-mailed Damien to get his input on the entity type hooks; CC'ed you. If he doesn't care one way or the other, I'll go with changing the name of the hooks, but if we're going to break node's precedent anyways I'll be tempted to also change their return types to arrays as well.

rszrama’s picture

Status: Needs review » Fixed

All of the above have been linked in. Additionally, I've converted entity type data structures to arrays instead of objects based on Damien's feedback and the ability for the Field UI to work with just a bundle name instead of a bundle object.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.