You are here

function ref_field_sync_entity_update in (Entity)Reference Field Synchronization 7.2

Same name and namespace in other branches
  1. 7 ref_field_sync/ref_field_sync.module \ref_field_sync_entity_update()

Implements hook_entity_update().

File

./ref_field_sync.module, line 227
Hooks and main functions for ref_field module.

Code

function ref_field_sync_entity_update($entity, $type) {
  $info = entity_get_info($type);

  // If entity support entity_save()
  if (entity_type_supports($type, 'save')) {

    // Save original entity in variable.
    $entity_id = $info['entity keys']['id'];
    $original = isset($entity->original) ? $entity->original : entity_load_unchanged($type, $entity->{$entity_id});

    // 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 = $old = array();

        // Save values from new entity.
        $items = field_get_items($type, $entity, $field_name);
        if ($items) {
          foreach ($items as $value) {

            // Do not modify calling entity.
            if (!isset($entity->ref_field_caller) || $entity->ref_field_caller != $value['target_id']) {
              $new[] = $value['target_id'];
            }
          }
        }

        // Save values from original entity.
        if ($items = field_get_items($type, $original, $field_name)) {
          foreach ($items as $value) {

            // Do not modify calling entity.
            if (!isset($entity->ref_field_caller) || $entity->ref_field_caller != $value['target_id']) {
              $old[] = $value['target_id'];
            }
          }
        }

        // Get removed values.
        $removed = array_diff($old, $new);

        // Get added values.
        $added = array_diff($new, $old);
        $entity_ids = array_merge($removed, $added);
        $entities = array();
        if (!empty($entity_id)) {
          $entities = entity_load($field['settings']['target_type'], $entity_ids);
        }
        if (!empty($removed)) {
          foreach ($removed as $target_id) {

            // Remove reference from synced field.
            if (isset($entities[$target_id])) {
              ref_field_sync_remove_reference($field['settings']['target_type'], $entities[$target_id], $entity->{$info['entity keys']['id']}, $field['settings']['sync']);
            }
          }
        }
        if (!empty($added)) {
          foreach ($added as $target_id) {

            // Add reference to synced field.
            if (isset($entities[$target_id])) {
              ref_field_sync_add_reference($field['settings']['target_type'], $entities[$target_id], $entity->{$info['entity keys']['id']}, $field['settings']['sync']);
            }
          }
        }
      }
    }
  }
}