public function ReferencesImportSettingsBase::formAttachFieldReference in CRM Core 7
Attach one reference field to form.
1 call to ReferencesImportSettingsBase::formAttachFieldReference()
- ReferencesImportSettingsBase::formAttachReferences in modules/
crm_core_data_import/ plugins/ settings/ ReferencesImportSettingsBase.inc - Attach reference fields to form.
File
- modules/
crm_core_data_import/ plugins/ settings/ ReferencesImportSettingsBase.inc, line 110 - Base references settings handler for CRM Core Data Import.
Class
- ReferencesImportSettingsBase
- @file Base references settings handler for CRM Core Data Import.
Code
public function formAttachFieldReference(&$form, &$form_state, $settings, $entity_type, $bundle, $field_name, $delta, $entity_delta) {
$destination_values = array();
$field_info = field_info_field($field_name);
$field_instance = field_info_instance($entity_type, $field_name, $bundle);
// If field node author.
if ($entity_type == 'node' && $field_name == 'uid') {
$list = $this
->getMatchMappingEntity('user', 'user', $settings);
foreach ($list as $item) {
$key = $item['entity_type'] . ':user:' . $item['delta'];
$destination_values[$key] = t('User') . ' ' . $item['delta'];
}
$field_instance['label'] = t('Node author');
$field_info['field_name'] = 'uid';
}
if (!empty($field_info['settings']['handler_settings']['target_bundles'])) {
$target_entity_type = $field_info['settings']['target_type'];
}
elseif (empty($field_info['settings']['handler_settings']['target_bundles']) && !empty($field_info['settings']['target_type']) && $field_info['settings']['target_type'] == 'crm_core_contact') {
// Use default value for crm_core_contact and individual bundle.
$target_entity_type = 'crm_core_contact';
}
if (!empty($target_entity_type)) {
$entity_info = entity_get_info($target_entity_type);
if (!empty($field_info['settings']['handler_settings']['target_bundles'])) {
foreach ($field_info['settings']['handler_settings']['target_bundles'] as $target_bundle) {
$list = $this
->getMatchMappingEntity($target_entity_type, $target_bundle, $settings);
foreach ($list as $item) {
$key = $item['entity_type'] . ':' . $item['entity_bundle'] . ':' . $item['delta'];
$destination_values[$key] = $entity_info['label'] . ' ' . $target_bundle . ' ' . $item['delta'];
}
}
}
else {
// Use default value for crm_core_contact and individual bundle.
$target_bundle = 'individual';
$list = $this
->getMatchMappingEntity($target_entity_type, $target_bundle, $settings);
foreach ($list as $item) {
$key = $item['entity_type'] . ':' . $target_bundle . ':' . $item['delta'];
$destination_values[$key] = $entity_info['label'] . ' ' . $target_bundle . ' ' . $item['delta'];
}
}
}
$form['references']['fields'][$delta] = array(
'#type' => 'fieldset',
'#attributes' => array(
'class' => array(
'settings-fieldset',
),
),
);
$form['references']['fields'][$delta]['label'] = array(
'#type' => 'item',
'#markup' => t('@field_name (@bundle) should be reference', array(
'@field_name' => $field_instance['label'],
'@bundle' => $bundle,
)),
);
$form['references']['fields'][$delta]['source'] = array(
'#type' => 'value',
'#value' => $entity_type . ':' . $bundle . ':' . $entity_delta,
);
$form['references']['fields'][$delta]['destination'] = array(
'#type' => 'select',
'#options' => $destination_values,
'#default_value' => !empty($form_state['values']['references']['fields'][$delta]['destination']) ? $form_state['values']['references']['fields'][$delta]['destination'] : FALSE,
);
$form['references']['fields'][$delta]['reference_type'] = array(
'#type' => 'value',
'#value' => $entity_type . ':' . $bundle . ':' . $entity_delta . ':' . $field_info['field_name'],
);
$form['references']['fields'][$delta]['remove_field'] = array(
'#type' => 'button',
'#value' => t('Remove'),
'#name' => drupal_html_id('references-settings-remove-button-' . $entity_delta),
'#ajax' => array(
'callback' => '_crm_core_data_import_references_callback',
'method' => 'replace',
'wrapper' => 'references-form-fields-wrapper',
),
);
}