diff --git a/entityreference_prepopulate.module b/entityreference_prepopulate.module index 9c06140a4eef43ff5386e9bccfced524c2cc8541..0d60d198a4baa38c2a1c35ebe47fe9199168bebf 100644 --- a/entityreference_prepopulate.module +++ b/entityreference_prepopulate.module @@ -64,12 +64,6 @@ function entityreference_prepopulate_field_update_instance($instance, $prior_ins function entityreference_prepopulate_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) { list($id,, $bundle) = entity_extract_ids($entity_type, $entity); - if (!empty($form_state['triggering_element']['#ajax'])) { - // We are inside AJAX, so values can't be taken from URL at the - // moment, so we prevent applying any "fallback" behaviors such as redirect. - return; - } - // Check if there is a field that needs to be prepopulated attached to the // given entity. $found = FALSE; @@ -104,6 +98,11 @@ function entityreference_prepopulate_field_attach_form($entity_type, $entity, &$ // in case the form is rebuilt by AJAX requests. $field_name = $field['field_name']; + if (isset($form_state['entityreference_prepopulate'][$instance['entity_type']][$instance['bundle']][$field_name])) { + $form[$field_name][$lang][0]['default']['#default_value'] = $form_state['entityreference_prepopulate'][$instance['entity_type']][$instance['bundle']][$field_name]; + continue; + } + if ($ids = entityreference_prepopulate_get_values($field, $instance)) { $form_state['entityreference_prepopulate'][$instance['entity_type']][$instance['bundle']][$field_name] = $ids; } @@ -122,6 +121,13 @@ function entityreference_prepopulate_field_attach_form($entity_type, $entity, &$ } } elseif (in_array($settings['fallback'], array('form_error', 'redirect', 'hide'))) { + + if (!empty($form_state['triggering_element']['#ajax'])) { + // We are inside AJAX, so values can't be taken from URL at the + // moment, so we prevent applying any "fallback" behaviors such as redirect. + return; + } + $message = t('Field @label must be populated via URL.', array('@label' => $instance['label'])); if ($settings['fallback'] == 'form_error') { form_error($form, $message);