diff --git a/entityreference_prepopulate.module b/entityreference_prepopulate.module index c8e88a1c155e76f7813168da95d389626962bc9a..bc9ccd6d13ede04fb19a896a93e5ca1fc77b3375 100644 --- a/entityreference_prepopulate.module +++ b/entityreference_prepopulate.module @@ -131,7 +131,7 @@ function entityreference_prepopulate_field_attach_form($entity_type, $entity, &$ // 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, TRUE)) { + if ($values = entityreference_prepopulate_get_values($field, $instance)) { $form_state['entityreference_prepopulate'][$instance['entity_type']][$instance['bundle']][$field['field_name']] = $values; } @@ -208,17 +208,17 @@ function entityreference_prepopulate_field_access($op, $field, $entity_type, $en * TRUE if the group IDs should be sent as a simple array. FALSE, if we * need to build array suited for field API's $items value. */ -function entityreference_prepopulate_get_values($field, $instance, $flat_array = FALSE, $validate = TRUE) { +function entityreference_prepopulate_get_values($field, $instance, $flat_array = FALSE) { $settings = $instance['settings']['behaviors']['prepopulate']; if (!empty($settings['og_context']) && module_exists('og_context')) { - return entityreference_prepopulate_get_values_from_og_context($field, $instance, $flat_array, $validate); + return entityreference_prepopulate_get_values_from_og_context($field, $instance, $flat_array); } - return entityreference_prepopulate_get_values_from_url($field, $instance, $flat_array, $validate); + return entityreference_prepopulate_get_values_from_url($field, $instance, $flat_array); } -function entityreference_prepopulate_get_values_from_og_context($field, $instance, $flat_array = FALSE, $validate = TRUE) { +function entityreference_prepopulate_get_values_from_og_context($field, $instance, $flat_array = FALSE) { $field_name = $field['field_name']; if (!og_is_group_audience_field($field_name) || !$og_context = og_context()) { @@ -249,7 +249,7 @@ function entityreference_prepopulate_get_values_from_og_context($field, $instanc * @see * entityreference_prepopulate_get_values() */ -function entityreference_prepopulate_get_values_from_url($field, $instance, $flat_array = FALSE, $validate = TRUE) { +function entityreference_prepopulate_get_values_from_url($field, $instance, $flat_array = FALSE) { $cache = &drupal_static(__FUNCTION__, array()); $field_name = $field['field_name']; $identifier = $instance['entity_type'] . ':' . $instance['bundle'] . ':' . $field_name . ':' . $flat_array; @@ -263,37 +263,18 @@ function entityreference_prepopulate_get_values_from_url($field, $instance, $fla return; } - // Get the value from the URL if possible. - if (!empty($_GET[$field_name]) && is_string($_GET[$field_name])) { - $ids = explode(',', $_GET[$field_name]); - } - else { - // Try to get the form out of cache. - $form_build_id = isset($_GET['form_build_id']) ? $_GET['form_build_id'] : isset($_POST['form_build_id']) ? $_POST['form_build_id'] : NULL; - $form_state = array(); - $form = form_get_cache($form_build_id, $form_state); - - // If successful, get the value from the 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 not, do nothing and return. - else { - $cache[$identifier] = FALSE; - return; - } + if (!$ids = entityreference_prepopulate_get_url_or_cached_values($field_name, $instance)) { + $cache[$identifier] = FALSE; + return; } - // Check if the IDs are valid, and get filter out the ones that are not valid. - if ($validate) { - $handler = entityreference_get_selection_handler($field, $instance); - if (!$ids = $handler->validateReferencableEntities($ids)) { - $cache[$identifier] = FALSE; - return; - } + // Check if the IDs are valid, and filter out the invalid ones. + $handler = entityreference_get_selection_handler($field, $instance); + if (!$ids = $handler->validateReferencableEntities($ids)) { + $cache[$identifier] = FALSE; + return; } - // Check access to the provided entities. $target_type = $field['settings']['target_type']; entity_load($target_type, $ids); @@ -308,6 +289,27 @@ function entityreference_prepopulate_get_values_from_url($field, $instance, $fla return $items; } + +/** + * Helper function to get the values from the URL or cached form. + * + * @param $field_name + * The field name. + */ +function entityreference_prepopulate_get_url_or_cached_values($field_name, $instance) { + // Get the value from the URL if possible. + if (!empty($_GET[$field_name]) && is_string($_GET[$field_name])) { + return explode(',', $_GET[$field_name]); + } + // Try to get the form out of cache. + $form_build_id = isset($_GET['form_build_id']) ? $_GET['form_build_id'] : isset($_POST['form_build_id']) ? $_POST['form_build_id'] : NULL; + $form_state = array(); + $form = form_get_cache($form_build_id, $form_state); + + // If successful, get the value from the form_state. + return isset($form_state['entityreference_prepopulate'][$instance['entity_type']][$instance['bundle']][$field_name]) ? isset($form_state['entityreference_prepopulate'][$instance['entity_type']][$instance['bundle']][$field_name]) : FALSE; +} + /** * Return a form element with crafted links to create nodes for a group. *