diff --git a/multifield.field.inc b/multifield.field.inc index a10305d..569cbba 100644 --- a/multifield.field.inc +++ b/multifield.field.inc @@ -33,7 +33,9 @@ function multifield_field_info() { 'settings' => array( 'hide_blank_items' => TRUE, 'entity_translation_sync' => array('id'), + 'property_type' => 'multifield', ), + 'property_callbacks' => array('multifield_field_property_callback'), ); // Deprecated field types from the CTools exports. @@ -59,6 +61,36 @@ function multifield_field_info() { } /** + * Property callback for the Entity Metadata framework. + */ +function multifield_field_property_callback(&$info, $entity_type, $field, $instance, $field_type) { + // Set the property type based on the target type. + $field_type['property_type'] = $field['settings']['property_type']; + $instance['property info'] = array( + 'getter callback' => 'multifield_field_property_get', + ); + // Then apply the default. + entity_metadata_field_default_property_callback($info, $entity_type, $field, $instance, $field_type); +} + +/** + * Callback for getting field property values. + */ +function multifield_field_property_get($entity, array $options, $name, $entity_type, $info) { + $field = field_info_field($name); + $langcode = isset($options['language']) ? $options['language']->language : LANGUAGE_NONE; + $langcode = entity_metadata_field_get_language($entity_type, $entity, $field, $langcode, TRUE); + $values = array(); + if (isset($entity->{$name}[$langcode])) { + foreach ($entity->{$name}[$langcode] as $delta => $data) { + $values[$delta] = entity_metadata_wrapper('multifield', entity_load_single('multifield', $data['id'])); + } + } + // For an empty single-valued field, we have to return NULL. + return $field['cardinality'] == 1 ? ($values ? reset($values) : NULL) : $values; +} + +/** * Implements hook_field_settings_form(). */ function multifield_field_settings_form($field, $instance) { diff --git a/multifield.module b/multifield.module index fa3a1c6..fc71b3b 100644 --- a/multifield.module +++ b/multifield.module @@ -112,6 +112,7 @@ function multifield_entity_info() { 'id' => 'id', 'bundle' => 'type', ), + 'access callback' => 'multifield_access_callback', ); // Bundles must provide a human readable name so we can create help and error @@ -131,6 +132,14 @@ function multifield_entity_info() { return $info; } +/** + * Access callback for entity access. + */ +function multifield_access_callback($op, $entity, $account, $entity_type) { + // For now grant access to anyone asking for multifield entities. + return TRUE; +} + function multifield_load($name) { $result = multifield_load_all(); return isset($result[$name]) ? $result[$name] : FALSE;