You are here

public function ReferencesCiviCrmImportSettings::attachReference in CRM Core 7

Create reference between entities.

1 call to ReferencesCiviCrmImportSettings::attachReference()
ReferencesCiviCrmImportSettings::postImport in modules/crm_core_data_import/plugins/settings/ReferencesCiviCrmImportSettings.inc
Performs when import was successful.

File

modules/crm_core_data_import/plugins/settings/ReferencesCiviCrmImportSettings.inc, line 59

Class

ReferencesCiviCrmImportSettings

Code

public function attachReference(&$source_entity, $source_type, $destination_entity_info, $reference_settings, &$changed) {
  try {
    list(, , , $target_field) = explode(':', $reference_settings['reference_type']);
    $destination_entity = entity_load_single($destination_entity_info->entity_type, $destination_entity_info->id);
    $source_wrapper = entity_metadata_wrapper($source_type, $source_entity);
    if ($source_entity && $destination_entity) {
      $multivalue = FALSE;
      $field = field_info_field($target_field);
      if (!empty($field['cardinality']) && $field['cardinality'] != 1) {
        $multivalue = TRUE;
      }

      // Add value to entity.
      if ($source_entity && $destination_entity) {
        if ($multivalue) {
          $source_wrapper->{$target_field}[] = $destination_entity_info->id;
          $changed = TRUE;
        }
        else {
          $source_wrapper->{$target_field}
            ->set($destination_entity_info->id);
          $changed = TRUE;
        }
      }
    }
  } catch (Exception $e) {
    watchdog('crm_core_data_import', $e
      ->getMessage(), array(), WATCHDOG_ERROR);
  }
}