You are here

function ref_field_sync_field_update_field 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_field_update_field()

Implements hook_field_update_field().

Link fields back automatically And remove old link if one existed

File

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

Code

function ref_field_sync_field_update_field($field, $prior_field, $has_data) {

  // Add new association
  // Only add association if the sync field is not poiting to this one already.
  if (isset($field['settings']['sync'])) {
    $sync_field_name = $field['settings']['sync'];
    $sync_field = field_read_field($sync_field_name);
    if ($sync_field && $sync_field['settings']['sync'] != $field['field_name']) {
      $sync_field['settings']['sync'] = $field['field_name'];
      field_update_field($sync_field);
    }
  }

  // Check old association
  // If there was an association, call old sync field and remove association to
  // this one.
  if (isset($prior_field['settings']['sync']) && $prior_field['settings']['sync'] && $prior_field['settings']['sync'] != $field['settings']['sync']) {
    $prior_sync_field_name = $prior_field['settings']['sync'];
    $prior_sync_field = field_read_field($prior_sync_field_name);
    if ($prior_sync_field && $prior_sync_field['settings']['sync'] == $field['field_name']) {
      $prior_sync_field['settings']['sync'] = FALSE;
      field_update_field($prior_sync_field);
    }
  }
}