Score:0

How do I know what properties and methods are available to an entity_metadata_wrapper object?

us flag

It may be easiest to start with an example.

The entity_metadata_wrapper() function seems to do a lot of complex magic behind the scenes in order to return an object with all data related to the entity in question. But how do I know what properties and methods are available to me once the object is returned?

Let's say I have Commerce Order. (This is not necessarily a Commerce question, it's just good for an example). On checkout I want to check that the order contains a line item of a given product type and notify the user. Maybe I also want to compare the calculated lined item price with the original price in the product type.

Ok, so that's Order, Line Item(s), Product Type, and User that are all referenced entities. The Product type has multiple custom fields, maybe those are references to another entity like the billing information.

$order_wrapper = entity_metadata_wrapper('commerce_order', $order);

According to the Entity API docs, all of those referenced entities are now included in the object ready for me to use thanks to the wrapper.

But how do I know how to access a value I'm looking for or where it is in the object structure? Are all referenced drupal fields their machine name by design? I know chaining has to play a part, so how does that factor in? And wouldn't this structure change every time entity_metadata_wrapper is called given that the entity in question could arbitrarily reference other entities (that reference other entities, etc)? And if the structure is arbitrary, any code that relied on that structure at a given time, if it changes, won't that break the code?

Finally, I see references to things like ->save() and ->value() in the Entity API docs, but I cannot find a reference to the full set of methods available to me. Is there any logical structure to figuring this out or do I need to use something like xdebug each time (and if I do, that still doesn't let me know what methods are available to me to act on the data). This is for D7, but if a D8+ answer could add clarity, please provide.

misterdidi avatar
de flag
Is this what you are looking for ? [https://www.drupal.org/docs/7/api/entity-api/entity-metadata-wrappers#debugging](https://www.drupal.org/docs/7/api/entity-api/entity-metadata-wrappers#debugging)
cn flag
Short answer: Use an IDE, and take the time to get it set up properly. For example, I use PHPStorm: https://drupalize.me/topic/phpstorm
cn flag
IDEs don’t really help here, it’s all magic methods so the properties/methods aren’t know until runtime. See https://drupal.stackexchange.com/questions/33473/is-there-a-good-way-to-inspect-objects-whose-type-is-derived-from-the-entitydrup for some ideas
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.