0

There a few days ago, someone in this website advised me to use the function entity_metadata_wrapper. Thanks that, I can get and set fields of entity without use heavy code.

Now, I would like find the equivalent when I use a form_content from an alter form hook. After several google research, I didn't find the equivalent... can you help me ? =)

Concretely, When I get a fields, i do :

$exemple = $form_state['field']['field_one']['und']['entity'][2]->sub_field['und'][0]['value'];

(my form content contains a collection field). It's very ugly...

1
  • Those are just very deep, associative arrays; there isn't an equivalent to entity_metadata_wrapper for associative form (render) arrays – Clive Jul 14 '14 at 8:27
1

If you've got a complete entity in that form, or some type of structure that the Entity API knows about, because it's been defined in hook_entity_info and/or hook_property_info, you can wrap virtually anything, for example:

$data = array('pears', 'apples', 'oranges');
$wrapper = entity_metadata_wrapper('struct', $data);
dpm($wrapper->value());

But you won't have all the goodies of the wrappers, which are the smart metadata information about the values you're retrieving, you can find how to build your own in the entity module, for example in entity_metadata_site_wrapper:

function entity_metadata_site_wrapper() {
  $site_info = entity_get_property_info('site');
  $info['property info'] = $site_info['properties'];
  return entity_metadata_wrapper('site', FALSE, $info);
}

site is not an entity or field structure at all, but it has been defined by a hook_entity_property_info, in entity_metadata_system_entity_property_info so you can wrap it normally and Entity API will go and fetch the properties you define by using the callbacks you've defined:

function entity_metadata_system_entity_property_info() {
  $info = array();

  // There is no site entity, but still add metadata for global site properties
  // here. That way modules can alter and add further properties at this place.
  // In order to make use of this metadata modules may use the wrapper returned
  // by entity_metadata_site_wrapper().
  $properties = &$info['site']['properties'];
  $properties['name'] = array(
    'label' => t("Name"),
    'description' => t("The name of the site."),
    'getter callback' => 'entity_metadata_system_get_properties',
    'sanitize' => 'check_plain',
  );
  $properties['slogan'] = array(
    'label' => t("Slogan"),
    'description' => t("The slogan of the site."),
    'getter callback' => 'entity_metadata_system_get_properties',
    'sanitize' => 'check_plain',
  );
(...)

So if you've got an entity (saved or not, this only will limit how the data is accessed), you can wrap it normally and access to it, and if you have another type of structure, Entity Metadata Wrappers will still know how to deal with this if you or another module defines so.

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.