function ref_field_sync_entity_insert in (Entity)Reference Field Synchronization 7.2
Same name and namespace in other branches
- 7 ref_field_sync/ref_field_sync.module \ref_field_sync_entity_insert()
Implements hook_entity_insert().
File
- ./
ref_field_sync.module, line 182 - Hooks and main functions for ref_field module.
Code
function ref_field_sync_entity_insert($entity, $type) {
$info = entity_get_info($type);
// If entity support entity_save() and is not called by this module.
if (entity_type_supports($type, 'save') && (!isset($entity->ref_field_caller) || $entity->ref_field_caller != TRUE)) {
// Get fields of type ref_field
$fields = field_read_fields(array(
'type' => 'entityreference',
));
// Loop through every field.
foreach ($fields as $field_name => $field) {
// Get more info for the field (bundles where exist)
$field = field_info_field_by_id($field['id']);
// Update fields array with new info.
$fields[$field_name] = $field;
// Go through fields that exist in this entity/bundle
if (isset($entity->{$field_name}) && isset($field['settings']['sync']) && $field['settings']['sync']) {
$new = array();
// Save values from new entity.
$fields_name = field_get_items($type, $entity, $field_name);
if (isset($fields_name)) {
foreach ($fields_name as $value) {
$new[] = $value['target_id'];
}
}
if (!empty($new)) {
$entities = entity_load($field['settings']['target_type'], $new);
if (!empty($entities)) {
foreach ($entities as $entity_id => $e) {
// Add reference to synced field.
ref_field_sync_add_reference($field['settings']['target_type'], $e, $entity->{$info['entity keys']['id']}, $field['settings']['sync']);
}
}
}
}
}
}
}