I'm trying to implement a shopping cart feature that requires retrieving the data (total value of cart) from a Commerce order. The problem is that this is only possible with a hook. But how am I supposed to pass this data from the hook to a service/controller? I can't find any documentation on where the return value is sent from a hook and if it exists, it's certainly not placed in a very easy to access place since I've been searching for a few days now.
Trying to implement this hook:
function commerce_cart_order_load($uid = 0) {
// Retrieve the order ID for the specified user's current shopping cart.
$order_id = commerce_cart_order_id($uid);
// If a valid cart order ID exists for the user, return it now.
if (!empty($order_id)) {
return commerce_order_load($order_id);
}
return FALSE;
}
This should give me an assoc array (or some other data object) of the different elements of an order. Not sure how I can pass this around or access it anywhere.
\Drupal::service('my_service')->foo($bar);
, where$bar
is initialised in the hook. Passing to a controller from a hook doesn't make sense, a controller's job is to prepare the page, it doesn't directly invoke any hooks. If it did, you'd be responsible for passing the data along anyway, so by definition you'd already have it. Maybe if you edit the question and describe exactly what data you need available in the controller, someone will be able to tell you how to get access to itdrupal_set_message
. I know how to inject services into a controller but that doesn't help me with getting the data from a hook