3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00

Fix reference prepopulation for area fields.

This commit is contained in:
Michael Stenta 2017-07-10 12:54:24 -04:00
parent f641a65631
commit 3fd34b6bfd

View file

@ -33,8 +33,20 @@ function farm_fields_prepopulate_entityreference(&$form, $entity_type, $field_na
// Load the field instance definition. // Load the field instance definition.
$form_entity_type = $form['#entity_type']; $form_entity_type = $form['#entity_type'];
$form_entity_bundle = $form['#bundle']; $form_entity_bundle = $form['#bundle'];
$field_base = field_info_field($field_name);
$field_instance = field_info_instance($form_entity_type, $field_name, $form_entity_bundle); $field_instance = field_info_instance($form_entity_type, $field_name, $form_entity_bundle);
// Set the field value key based on the field type.
$value_key = 'value';
switch ($field_base['type']) {
case 'entity_reference';
$value_key = 'target_id';
break;
case 'taxonomy_term_reference':
$value_key = 'tid';
break;
}
// Validate the entity IDs by loading them. Rebuild the list of IDs using // Validate the entity IDs by loading them. Rebuild the list of IDs using
// only the entities that loaded. // only the entities that loaded.
$entities = entity_load($entity_type, $entity_ids); $entities = entity_load($entity_type, $entity_ids);
@ -71,7 +83,7 @@ function farm_fields_prepopulate_entityreference(&$form, $entity_type, $field_na
foreach ($labels as $key => $label) { foreach ($labels as $key => $label) {
// If the item isn't empty, skip it. // If the item isn't empty, skip it.
if (!empty($form[$field_name][LANGUAGE_NONE][$key]['target_id']['#default_value'])) { if (!empty($form[$field_name][LANGUAGE_NONE][$key][$value_key]['#default_value'])) {
continue; continue;
} }
@ -85,13 +97,13 @@ function farm_fields_prepopulate_entityreference(&$form, $entity_type, $field_na
$form[$field_name][LANGUAGE_NONE][$key] = $form[$field_name][LANGUAGE_NONE][0]; $form[$field_name][LANGUAGE_NONE][$key] = $form[$field_name][LANGUAGE_NONE][0];
// Set the default, delta, and weight values. // Set the default, delta, and weight values.
$form[$field_name][LANGUAGE_NONE][$key]['target_id']['#default_value'] = $label; $form[$field_name][LANGUAGE_NONE][$key][$value_key]['#default_value'] = $label;
$form[$field_name][LANGUAGE_NONE][$key]['target_id']['#delta'] = $key; $form[$field_name][LANGUAGE_NONE][$key][$value_key]['#delta'] = $key;
$form[$field_name][LANGUAGE_NONE][$key]['target_id']['#weight'] = $key; $form[$field_name][LANGUAGE_NONE][$key][$value_key]['#weight'] = $key;
// Only make the first one required. // Only make the first one required.
if ($key > 0) { if ($key > 0) {
$form[$field_name][LANGUAGE_NONE][$key]['target_id']['#required'] = 0; $form[$field_name][LANGUAGE_NONE][$key][$value_key]['#required'] = 0;
} }
$form[$field_name][LANGUAGE_NONE]['#max_delta'] = $key; $form[$field_name][LANGUAGE_NONE]['#max_delta'] = $key;
@ -116,12 +128,12 @@ function farm_fields_prepopulate_entityreference(&$form, $entity_type, $field_na
foreach ($entities as $id => $entity) { foreach ($entities as $id => $entity) {
// If the item isn't empty, skip it. // If the item isn't empty, skip it.
if (!empty($form[$field_name][LANGUAGE_NONE][$id]['target_id'])) { if (!empty($form[$field_name][LANGUAGE_NONE][$id][$value_key])) {
continue; continue;
} }
// Add the checkbox element. // Add the checkbox element.
$form[$field_name][LANGUAGE_NONE][$id]['target_id'] = array( $form[$field_name][LANGUAGE_NONE][$id][$value_key] = array(
'#type' => 'checkbox', '#type' => 'checkbox',
'#return_value' => $id, '#return_value' => $id,
'#value' => $id, '#value' => $id,