2

So thankful for all the help on here, but still so baffled as to how Im not able to just quickly determine how to pull these values out of certain arrays and objects.

Trying to get the order id (and other values) from the frickin $variables array in hook_preprocess_hook

Code afc.theme

function afc_preprocess_commerce_order_receipt(&$variables) {
  dpm($variables['order_entity']);
  \Drupal::logger('afc_theme')->notice('running');
}

Some of the Output

stdClass Object
(
    [__CLASS__] => Drupal\commerce_order\Entity\Order
    [values:protected] => Array
        (
            [order_id] => Array
                (
                    [x-default] => 182
                )

            [type] => Array
                (
                    [x-default] => default
                )

            [uuid] => Array
                (
                    [x-default] => c8b1c210-0e6e-4efb-a59d-84b28d4dadfe
                )

            [order_number] => Array
                (
                    [x-default] => 
                )

            [store_id] => Array
                (
                    [x-default] => 1
                )

            [uid] => Array
                (
                    [x-default] => Array
                        (
                            [0] => Array
                                (
                                    [target_id] => 1
                                )

                        )

                )

Ive tried various wrong things like:

$variables['order_entity'] = $order;
dpm($order->get("order_id")->getValue());
dpm($variables->get("order_id")->getValue());
dpm($variables['order_entity']['order_id'];

1. How do I get the order_id from this $variables object?

2. How/where did you find this information telling you how to get this value out?

1 Answer 1

2

How do I get the order_id from this $variables object?

All entities have an id method, inherited (or perhaps overridden) from Entity::id. From your debug output $variables['order_entity'] appears to be one such entity, so:

$order_id = $variables['order_entity']->id();

How/where did you find this information telling you how to get this value out?

Short answer, in the source and the docs (following/finding isn't always straightforward though). For example if you've read the Introduction to Entity API in Drupal 8, you'll intuitively know that a Drupal\commerce_order\Entity\Order is an entity type, both by the folder structure and its annotation, and you can start thinking about how to address an object of that type in more generic terms.

That leads to searches like "Drupal 8 get entity id" or "Drupal 8 get entity field value programatically", and Google will dutifully tell you everything you need to know.

If you can spare a bit of time to follow the class hierarchy up the chain, and get at least a broad idea of what each class does and how it works, that will probably take some pain out of future encounters with the entity API too.

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.