38

I'm sure this question applies to other complex object types, but EntityDrupalWrapper is the one I'm currently working with. This is the type of object returned by entity_metadata_wrapper() (and probably others).

When using code like the following:

$order_wrapper = entity_metadata_wrapper('commerce_order', $order_object);
dpm($order_wrapper);

the output from Devel is none too helpful:

screenshot

It doesn't show what you'd expect to see for a call to dpm() with, for example, a stdClass object.

A similar call to dvm() does print the object, but in a manner I'm not familiar with, and it's not very readable.

I've tried debug() and it produces a PHP user notice that looks something like this:

screenshot 2

Incidentally the HTML output contained in that warning is the same output that dvm() prints to the screen.

How can I inspect these types of objects (preferably with Krumo), so I can see what properties are available? Or am I stuck using dvm()?

5
  • try debug() introduced in Drupal 7 Jun 7, 2012 at 20:06
  • Thanks @ShuaibNawaz, even less success with that one though I'm afraid (I've updated the question)
    – Clive
    Jun 7, 2012 at 20:10
  • 2
    Yep, that's exactly one of the problems with the wrapper classes. dpm()/krumo only sees public properties. Everything in the wrapper classes is protected and access through magic __get() and __set() methods.
    – Berdir
    Jun 7, 2012 at 20:12
  • @Berdir I was afraid of that; I hadn't looked but it did smell a lot like __get() and __set() were involved. Out of interest do you have any preferred way of finding out what class properties are available for what wrapper? Or is it just a matter of 'knowing' based on experience?
    – Clive
    Jun 7, 2012 at 20:17
  • 1
    @Berdir I don't think you'll get notified of this, you can use dpm($wrapper->getPropertyInfo());, it really helps things
    – Clive
    Sep 13, 2012 at 13:17

7 Answers 7

38

Use dpm($wrapper->getPropertyInfo());

It's in the docs.

1
  • And to inspect particular fields from your node you can use kpr($wrapper->my_custom_field->value());
    – wranvaud
    Apr 28, 2016 at 11:08
4

I had been playing with EntityDrupalWrapper objects. debug() usually returns the output that may have prepared by __toString()

I iterated EntityDrupalWrapper object using foreach and it helped to list down the name of properties.

foreach($order_wrapper as $name => $obj){
  debug($name);
  debug(get_class($obj)); //EntityValueWrapper
}

Here $obj is an object of type EntityValueWrapper

$obj can be read by $obj->value() and can be write by $obj->set('value');

EDIT:

If you have created your wrapper without passing second param
i.e. entity_metadata_wrapper('commerce_order');
then set and value methods will throw exception as they are not implemented.

0
4

I wound up writing my own little widget to unroll the wrapper:

function _wrapper_debug($w) {
  $values = array();
  foreach ($w->getPropertyInfo() as $key => $val) {
    $values[$key] = $w->$key->value();
  }
  return $values;
}

dpm(_wrapper_debug($some_object_wrapper));

Hope someone finds it useful.

3

The Inspect module provides a nice structural view of the data you want to debug.

Get instructive and well formatted variable dumps, deep stack traces, and execution time profiles - to database log (Reports / Recent log messages), to file or to screen.

3

If you want to inspect the original object that is "powering" the wrapper, try:

dpm($wrapper->raw())

This works well for me.

2

There is a devel patch the enables Krumo to show all private and protected properties.

Here is the thread on drupal.org

I've found this patch very helpful.

0

Use the drupal cli

$ drupal debug:entity
// Displays current events 

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.