function civicrm_entity_reference_field_inline_entity_form_entity_form_alter in CiviCRM Entity 7.2
Implements hook_inline_entity_form_entity_form_alter().
Parameters
$entity_form:
$form_state:
File
- modules/
civicrm_entity_reference_field/ civicrm_entity_reference_field.module, line 680 - Provide CiviCRM entity reference field type
Code
function civicrm_entity_reference_field_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
$ief_id = $entity_form['#ief_id'];
$host_entity_type = $form_state['inline_entity_form'][$ief_id]['instance']['entity_type'];
$field_info = array();
foreach ($entity_form['#parents'] as $key => $parent) {
if (strpos($parent, 'field_') === 0) {
$field = field_info_field($parent);
$instances = db_select('field_config_instance', 'fi')
->fields('fi', array(
'id',
))
->condition('field_id', $field['id'])
->condition('entity_type', $host_entity_type)
->condition('bundle', $host_entity_type)
->execute();
if ($instances
->rowCount()) {
$field_name = $parent;
$field_info = $field;
}
}
}
if (!empty($field_info) && $field_info['type'] == 'civicrm_entity_reference') {
// Generally you want to set the target_id_column to the host_id_prop
$target_id_column = isset($field_info['settings']['target_id_column']) ? $field_info['settings']['target_id_column'] : '';
$host_id_prop = isset($field_info['settings']['host_source_id']) ? $field_info['settings']['host_source_id'] : '';
// this doesn't handle a field that is nested on an entityreference field that is on say a node....
// this handles the 'multi' widget
if (isset($form_state['complete form'][$host_id_prop]['#default_value']) && isset($entity_form[$target_id_column]) && is_array($entity_form[$target_id_column])) {
if ($entity_form['#op'] == 'add' || $entity_form['#op'] == 'create') {
$entity_form[$target_id_column]['#default_value'] = $form_state['complete form'][$host_id_prop]['#default_value'];
}
$entity_form[$target_id_column]['#disabled'] = TRUE;
}
elseif (isset($form_state['field'][$field_name][LANGUAGE_NONE]['instance']['entity_type'])) {
$host_entity_type = $form_state['field'][$field_name][LANGUAGE_NONE]['instance']['entity_type'];
if (isset($form_state[$host_entity_type]->{$host_id_prop})) {
if ($entity_form['#op'] == 'add' || $entity_form['#op'] == 'create') {
$entity_form[$target_id_column]['#default_value'] = $form_state[$host_entity_type]->{$host_id_prop};
}
$entity_form[$target_id_column]['#disabled'] = TRUE;
}
}
//temporary ... necessary to disable target_id_column when civicrm_entity_reference field is embedded on a entityreference field which is embedded on a node
if (!empty($entity_form[$target_id_column]['#default_value'])) {
$entity_form[$target_id_column]['#disabled'] = TRUE;
}
// special handling of address fields on location blocks, because in this case the contact_id is not required
$contact_id_entities = array(
'civicrm_address',
'civicrm_email',
'civicrm_phone',
'civicrm_im',
);
if ($host_entity_type == 'civicrm_loc_block' && in_array($entity_form['#entity_type'], $contact_id_entities)) {
$entity_form['contact_id']['#required'] = FALSE;
$entity_form['contact_id']['#default_value'] = 1;
$entity_form['contact_id']['#disabled'] = TRUE;
$entity_form['contact_id']['#description'] = t('The value of 1 for this field is only a placeholder and will be set to NULL on save.');
}
}
}