You are here

function entity_metadata_wrapper in Entity API 7

Returns a property wrapper for the given data.

If an entity is wrapped, the wrapper can be used to retrieve further wrappers for the entity properties. For that the wrapper support chaining, e.g. you can use a node wrapper to get the node authors mail address:

echo $wrappedNode->author->mail
  ->value();

Parameters

$type: The type of the passed data.

$data: The data to wrap. It may be set to NULL, so the wrapper can be used without any data for getting information about properties.

$info: (optional) Specify additional information for the passed data:

  • langcode: (optional) If the data is language specific, its language code. Defaults to NULL, what means language neutral.
  • bundle: (optional) If an entity is wrapped but not passed, use this key to specify the bundle to return a wrapper for.
  • property info: (optional) May be used to use a wrapper with an arbitrary data structure (type 'struct'). Use this key for specifying info about properties in the same structure as used by hook_entity_property_info().
  • property info alter: (optional) A callback for altering the property info before it is utilized by the wrapper.
  • property defaults: (optional) An array of defaults for the info of each property of the wrapped data item.

Return value

EntityMetadataWrapper Dependent on the passed data the right wrapper is returned.

33 calls to entity_metadata_wrapper()
Entity::wrapper in includes/entity.inc
Returns the EntityMetadataWrapper of the entity.
EntityAPIController::buildContent in includes/entity.controller.inc
Implements EntityAPIControllerInterface.
EntityAPIi18nItegrationTestCase::testDefaultController in ./entity.test
Tests the provided default controller.
EntityDefaultViewsController::optionsListCallback in views/entity.views.inc
A callback returning property options, suitable to be used as views options callback.
EntityFieldHandlerHelper::extract_property_multiple in views/handlers/entity_views_field_handler_helper.inc
Extracts data from several metadata wrappers based on a data selector.

... See full list

File

./entity.module, line 1420

Code

function entity_metadata_wrapper($type, $data = NULL, array $info = array()) {
  if ($type == 'entity' || ($entity_info = entity_get_info()) && isset($entity_info[$type])) {

    // If the passed entity is the global $user, we load the user object by only
    // passing on the user id. The global user is not a fully loaded entity.
    if ($type == 'user' && is_object($data) && $data == $GLOBALS['user']) {
      $data = $data->uid;
    }
    return new EntityDrupalWrapper($type, $data, $info);
  }
  elseif ($type == 'list' || entity_property_list_extract_type($type)) {
    return new EntityListWrapper($type, $data, $info);
  }
  elseif (isset($info['property info'])) {
    return new EntityStructureWrapper($type, $data, $info);
  }
  else {
    return new EntityValueWrapper($type, $data, $info);
  }
}