2

I'm struggling to determine which entity_metadata_wrapper methods are available for a given field. For instance:

<?php
  $fields = entity_metadata_wrapper('node', $nid);
  $body = $fields->body->value->value();
  $title = $fields->title->value();
  $type = $fields->type->value();
?>

$body has a different method, where you have to do a ->value->value() for it to work right.

In my situation I'm trying to get the type of the node in Human readable form: "Custom Type" vs. "custom_type". I'm wondering if there is some kind of value() method that returns the human readable version. print_r($fields) avails me not.

Edit: To get the Human readable node type I ended up just using node_type_load. It seems like there should be a more efficient way of doing this using the Entity API, but I can't figure it out.

6

i think the wrapper doesnt provide the type label, at least i havent found it. One way to find the label :

$info = entity_get_info($fields->type());
echo $info['label'];

To learn more about available fields, try $fields->getPropertyInfo().

The diference of the body field is in the fact it is formatted, so the "first" value() function returns the object which allows you to retreive other metadata about the field. The difference can be spotted in the result of the aforementioned getPropertyInfo. While a "plain text" field is set to a callback function "entity_metadata_field_property_get", which returns the value immiediately, the body will have something like "entity_metadata_field_verbatim_get" which returns the whole array of the field.

according to the docs, you can also use value() on the wrapper itself and that will allow you to see what there is (so print_r($fields->value()); or dpm() the same will help :)

  • This was helpful, thanks! I'm still not able to easily discern which entity functions are available... for instance, to get the value of the body field I have to use ->value->value() instead of just ->value()... – Johnathan Elmore Apr 20 '12 at 16:53
  • sorry it took me a while to improve my answer with an explanation on the body problem – mojzis May 1 '12 at 18:54
0

It is possible to query a wrapper for all available fields:

$fields->getPropertyInfo();
  • Welcome to Drupal Answers! What reported in this answer has already been said in the accepted answer. If you agree with the given answer, when your reputation reaches 15 points, you will be able to up-vote it. – kiamlaluno Nov 7 '13 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.