diff --git a/entityreference_prepopulate.module b/entityreference_prepopulate.module index b6346c6a899da292fe2b1669cbb3504862a496db..2747ee4397623d7dbd0f111b396d5dcba57fa861 100644 --- a/entityreference_prepopulate.module +++ b/entityreference_prepopulate.module @@ -128,7 +128,14 @@ function entityreference_prepopulate_field_attach_form($entity_type, $entity, &$ } $field = $value['field']; - if (entityreference_prepopulate_get_values($field, $instance) || ($id && !empty($settings['action_on_edit']))) { + + // Store prepopulated values in the form state to make them persistent, + // in case the form is rebuilt by AJAX requests. + if ($values = entityreference_prepopulate_get_values($field, $instance)) { + $form_state['entityreference_prepopulate'][$instance['entity_type']][$instance['bundle']][$field['field_name']] = $values; + } + + if ($values || ($id && !empty($settings['action_on_edit']))) { // New entity with prepopualte values, or an existing entity, // we might need to disable/ hide the group-audience field. if ($settings['action'] == 'disable') { @@ -249,17 +256,24 @@ function entityreference_prepopulate_get_values_from_url($field, $instance, $fla if (isset($cache[$identifier])) { return $cache[$identifier]; } - $cache[$identifier] = FALSE; - if (empty($_GET[$field_name]) || !is_string($_GET[$field_name])) { - return; + // Check to see if we cached the values in a $form_state object (and we are + // refreshing the form) + $form_build_id = isset($_GET['form_build_id']) ? $_GET['form_build_id'] : isset($_POST['form_build_id']) ? $_POST['form_build_id'] : NULL; + if (!empty($form_build_id)) { + $form_state = array(); + $form = form_get_cache($form_build_id, $form_state); + if (isset($form_state['entityreference_prepopulate'][$instance['entity_type']][$instance['bundle']][$field_name])) { + $ids = $form_state['entityreference_prepopulate'][$instance['entity_type']][$instance['bundle']][$field_name]; + } } - - if (empty($instance['settings']['behaviors']['prepopulate']['status'])) { + elseif (empty($_GET[$field_name]) || !is_string($_GET[$field_name]) || empty($instance['settings']['behaviors']['prepopulate']['status'])) { + $cache[$identifier] = FALSE; return; } - - $ids = explode(',', $_GET[$field_name]); + else { + $ids = explode(',', $_GET[$field_name]); + } // Check if the IDs are valid, and get filter out the ones that are not valid. $handler = entityreference_get_selection_handler($field, $instance);