diff --git a/entityreference.module b/entityreference.module index e0bc51d..eb1fd38 100644 --- a/entityreference.module +++ b/entityreference.module @@ -641,7 +641,10 @@ function entityreference_field_widget_form(&$form, &$form_state, $field, $instan '#entity_type' => NULL, '#entity' => NULL, ); - $handler = entityreference_get_selection_handler($field, $instance, $element['#entity_type'], $element['#entity']); + + $entity_type = $instance['entity_type']; + $entity = $element['#entity']; + $handler = entityreference_get_selection_handler($field, $instance, $entity_type, $entity); if ($instance['widget']['type'] == 'entityreference_autocomplete' || $instance['widget']['type'] == 'entityreference_autocomplete_tags') { @@ -667,8 +670,8 @@ function entityreference_field_widget_form(&$form, &$form_state, $field, $instan // Load those entities and loop through them to extract their labels. $entities = entity_load($field['settings']['target_type'], $entity_ids); - foreach ($entities as $entity_id => $entity) { - $label = $handler->getLabel($entity); + foreach ($entities as $entity_id => $entity_item) { + $label = $handler->getLabel($entity_item); $key = "$label ($entity_id)"; // Labels containing commas or quotes must be wrapped in quotes. if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) { @@ -677,25 +680,37 @@ function entityreference_field_widget_form(&$form, &$form_state, $field, $instan $entity_labels[] = $key; } + // Prepare the autocomplete path. + if (!empty($instance['widget']['settings']['path'])) { + $autocomplete_path = $instance['widget']['settings']['path']; + } + else { + $autocomplete_path = $instance['widget']['type'] == 'entityreference_autocomplete' ? 'entityreference/autocomplete/single' : 'entityreference/autocomplete/tags'; + } + + $autocomplete_path .= '/' . $field['field_name'] . '/' . $instance['entity_type'] . '/' . $instance['bundle']; + if (!empty($entity)) { + list($id) = entity_extract_ids($entity_type, $entity); + $autocomplete_path .= '/' . $id; + } + if ($instance['widget']['type'] == 'entityreference_autocomplete') { - $path = !empty($instance['widget']['settings']['path']) ? $instance['widget']['settings']['path'] : 'entityreference/autocomplete/single'; $element += array( '#type' => 'textfield', '#maxlength' => 1024, '#default_value' => implode(', ', $entity_labels), - '#autocomplete_path' => $path . '/' . $field['field_name'] . '/' . $instance['entity_type'] . '/' . $instance['bundle'], + '#autocomplete_path' => $autocomplete_path, '#size' => $instance['widget']['settings']['size'], '#element_validate' => array('_entityreference_autocomplete_validate'), ); return array('target_id' => $element); } else { - $path = !empty($instance['widget']['settings']['path']) ? $instance['widget']['settings']['path'] : 'entityreference/autocomplete/tags'; $element += array( '#type' => 'textfield', '#maxlength' => 1024, '#default_value' => implode(', ', $entity_labels), - '#autocomplete_path' => $path . '/' . $field['field_name'] . '/' . $instance['entity_type'] . '/' . $instance['bundle'], + '#autocomplete_path' => $autocomplete_path, '#size' => $instance['widget']['settings']['size'], '#element_validate' => array('_entityreference_autocomplete_tags_validate'), ); @@ -747,8 +762,22 @@ function entityreference_field_widget_error($element, $error) { /** * Menu callback: autocomplete the label of an entity. - */ -function entityreference_autocomplete_callback($type, $field_name, $entity_type, $bundle_name, $string = '') { + * + * @param $type + * The widget type (i.e. 'single' or 'tags'). + * @param $field_name + * The name of the entity-reference field. + * @param $entity_type + * The entity type. + * @param $bundle_name + * The bundle name. + * @param $entity_id + * Optional; The entity ID the entity-reference field is attached to. + * Defautls to NULL. + * @param $string + * The label of the entity to query by. + */ +function entityreference_autocomplete_callback($type, $field_name, $entity_type, $bundle_name, $entity_id = NULL, $string = '') { $field = field_info_field($field_name); $instance = field_info_instance($entity_type, $field_name, $bundle_name); $matches = array(); @@ -757,7 +786,8 @@ function entityreference_autocomplete_callback($type, $field_name, $entity_type, return MENU_ACCESS_DENIED; } - $handler = entityreference_get_selection_handler($field, $instance); + $entity = !empty($entity_id) ? entity_load_single($entity_type, $entity_id) : NULL; + $handler = entityreference_get_selection_handler($field, $instance, $entity_type, $entity); if ($type == 'tags') { // The user enters a comma-separated list of tags. We only autocomplete the last tag.