I would like to see some additional functionality added to the autocomplete widget:
1. Using user-supplied value when user does not use the autocomplete functionality.
Currently the autocomplete only works if the user chooses one of the suggested values so that an entity id is placed behind it in parenthesis.
When a user chooses to ignore the autocomplete and fills in a valid term/name/title/value it is ignored.
2. Validating invalid input on required fields.
When a field is marked as required the autocomplete widget makes invalid entries empty but does this after the form API has checked the required field for being empty. Therefore making an invalid choice result in an empty value in the required field without any errors or warnings.

I have attached my attempt at a patch that addresses these functionalities.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

amitaibu’s picture

> When a user chooses to ignore the autocomplete and fills in a valid term/name/title/value it is ignored.

The problem is there is no uniqueness. If there are two same node titles, ER can select the correct one, only if there is the entity ID as part of the string. So I think this patch should deal only with (2).

JvE’s picture

Hm, I think the non-uniqueness of labels is another problem altogether.

Since the selection presented to the user does not differentiate between entities with the same label either, the chance that a user selects the "correct" one is the same as the chance that ER gets it right.
i.e. if a user types 'app' and is presented with the choice between 'apple', 'apple' and 'apple' you basically have the same situation. (and presenting them with the choice between 'apple (1)', 'apple (5)' and 'apple (12563)' is really not going to help ;)

The patch ensures that when a user types 'apple' they at least get a reference to an apple if one exists (expected behaviour) and not a silently ignored empty field (unexpected behaviour).

I made this patch for me because my customers were complaining that typing in titles they knew did not work (they can type a lot faster than autocomplete can keep up) and the logs were complaining that there were missing items in fields marked as required.
I could make it into three patches, one for (1), one for (2) and one for (1+2) if that would help you.

amitaibu’s picture

Yes, I think it's better to split into two separate issues.

ransom’s picture

Status: Needs review » Needs work

It wont error on multiple items/unlimited items past the first. Does work with single entries though.

JvE’s picture

Status: Needs work » Needs review
FileSize
3.17 KB

New patch:
- always errors on invalid input, not only on required fields
- handles multiple value fields.
- errors on ambiguous input

Juryiel’s picture

I like this patch as an option for fields but it doesn't work good in all situations.

For example, one of my entity references is a select list that's in a field collection with multiple values such that every time I edit a page, a new set of fields in teh field collection appears. If I select -None- (default) this patch will complain and won't let me edit any of the other fields on the node that are not part of this field collection. However, if I select a value, it will obviously add a new set of field-collection fields to the node.

I'm nto sure if that was clear, but basically it makes it impossible to edit any part of a node with a multiple value field-collection that contains an entity reference field without adding an additional set of fields from that collection.

JvE’s picture

You are correct. The validation is way too broad, it should limit itself to autocomplete fields.
I'll see if I can whip up a new patch tomorrow.

Status: Needs review » Needs work

The last submitted patch, entityreference_autocomplete.patch, failed testing.

JvE’s picture

I'm learning more every day..

JvE’s picture

Status: Needs work » Needs review
Juryiel’s picture

I tried the patch again and it doesn't seem to let me just type a node that I know exists as described. It seems to have standard function where if I type in a node without teh corresponding node id number, it will leave the field blank.

JvE’s picture

@juryiel : that is the standard behaviour without my patch. Can you doublecheck that the patch is actually applied?

Juryiel’s picture

I'll try and check later today, but when I applied it I clearly remember it said it patched 2 uh.. heaps? chunks? 2 units of something successfully.

Juryiel’s picture

Sorry about the delay. I checked and the patch was indeed applied. Is there anything additional I have to do other than patch the files? If not, it's still not working, just typing in something doesn't seem to do anything different than the default behavior on my end (the nodes do exist).

Juryiel’s picture

I also tested, and sometimes it will give me a server error when entering a node without its id number in parentheses. I'm not sure what the conditions for this are but it seems to be that the first time I attempt to save the form it will just go back to teh form edit screen without a value saved. Any subsequent times it will return a 500 error and will continue to do that until I enter nothing in the field or enter a node with its id in parentheses.

JvE’s picture

Due to changes Damien made to the module the patch from #10 no longer works.
I will have to make a new patch.

JvE’s picture

New patch with minor changes to follow changes in the module.

Status: Needs review » Needs work

The last submitted patch, entityreference-autocomplete-1389238-18.patch, failed testing.

JvE’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, entityreference-autocomplete-1389238-18.patch, failed testing.

JvE’s picture

Status: Needs work » Needs review
FileSize
3.01 KB

Bah, first the test-bot git crashes and now I've got windows line endings. New patch.

Status: Needs review » Needs work

The last submitted patch, entityreference-autocomplete-1389238-22.patch, failed testing.

JvE’s picture

Status: Needs work » Needs review
FileSize
3.01 KB
Juryiel’s picture

Had a chance to test the patch and it seems to be working. In my brief tests. As I use it more I'll report any issues.

JvE’s picture

Status: Needs review » Reviewed & tested by the community

So far no issues reported by my clients either.

JvE’s picture

Status: Reviewed & tested by the community » Needs work

Latest entityreference code bypasses the patch's check for invalid values in required fields.

JvE’s picture

Status: Needs work » Needs review
FileSize
1.9 KB

New leaner and cleaner patch, works on RC-1.
- support for values not entered through autocomplete (no-js or fast user)
- disallows references to non-existing entities

Fixes bugs #1398364: Autocomplete: Required fields are not alidated properly and #1398360: Autocomplete: User supplied values are being ignored.

geek-merlin’s picture

Status: Needs review » Needs work

Good work JvE!!!
Just found this issue while working on creating an entityrefcreate module (like noderefcreate).

think what this patch needs:
* the patch only works in _entityreference_autocomplete_validate(), it should ALSO work in _entityreference_autocomplete_tags_validate()

also i have the gut feeling that the logic should live in a handler to be pluggable
(but i'm not sure where)

geek-merlin’s picture

Status: Needs work » Needs review
FileSize
4.93 KB

here is an improved patch that
* also works for autocomplete-tags-style
* makes the logic pluggable

i also have the feeling that the common logic in _entityreference_autocomplete_validate() & _entityreference_autocomplete_tags_validate() should be factored out, but do not want to make changes even more complex.

and the tests should be written to get this in.

geek-merlin’s picture

sorry, a minor omission, this is the right one.

geek-merlin’s picture

again a minor improvement: added needed pass by reference

JvE’s picture

Status: Needs review » Reviewed & tested by the community

Still works like a charm.

chasingmaxwell’s picture

Are there plans to commit this patch? I'm hoping to utilize it for a project, but am hesitant if it wont be making its way into a released version of Entity Reference.

baby.hack’s picture

I'm also wondering if this will be committed. I need this functionality, and would prefer an *official* release.

Damien Tournoud’s picture

Status: Reviewed & tested by the community » Fixed

Merged into 7.x-1.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

mfoda’s picture

Has this patch been merged into the rc3 release?

User input validation for required fields was working for me when entity reference was referencing a vocabulary (gave an error: entity X does not exist). When I switched to an entity reference view it stopped validating. I tried applying the patch but it gave me "reversed or previously applied patch detected."

Any ideas how to fix this?

morbiD’s picture

mfoda's problem in #38 possibly related to the issue I just opened: #1819618: Incorrect autocomplete validation with views selection mode

The validation for the "Views" selection mode needs to be updated, similar to the "Simple" selection mode.

heatherwoz’s picture

Status: Closed (fixed) » Active

I am noticing the behavior described in #1398360: Autocomplete: User supplied values are being ignored, which this issue's patch was supposed to fix. It's kind of strange, because I have multiple sites using required entityreference fields, and only recently have I noticed that users are able to create nodes without a value in those fields. I am able to reproduce it by typing a valid value (node title) without selecting the option that pops up in the autocomplete. When I save, the new node is created but the entityreference field has no value. I am not using the views selection mode.

lionfish-dupe’s picture

I customised the module somewhat to hide the (nid) brackets and to allow the functionality described. I found it didn't seem to work if the field was referencing users, rather than nodes and was being filled in using a hook_form_alter with a username. Deleting the added username and typing it by hand makes it work.

Anyway, the code below makes it work either way - thought it might be useful to post here for reference.

It's a bit hacky (e.g. probably need to replace the db_query with field access functions etc?). It works for me. I'm not sure why the current 7.x-1.0 version doesn't work with my site - I'll have to compare the code, not had a chance yet.

function _entityreference_autocomplete_validate($element, &$form_state, $form) {
	$value = '';
	if (!empty($element['#value'])) {
		//Now trying to guess the value from the name...
		$nid = _er_find_nid($element['#value'], $element['#field_name']);
		if ($nid>=0)
		{
			$value = $nid;
		}
	}
  	// Update the value of this element so the field can validate the product IDs.
  	form_set_value($element, $value, $form_state);
}

//get nid from string
//if field is found, look for the title in that field
//otherwise just look for a node with that title.
//if that's not found, look for a user with that username.
function _er_find_nid($title,$fieldname)
{
	if (db_table_exists('field_data_'.$fieldname) > 0) {	
		$query = db_select('node','n');
		$query->fields('n',array('nid','title'));
		$query->join("field_data_".$fieldname,"f",$fieldname."_target_id = n.nid");
		$query->condition("title",$title,"=");
		$query->condition($fieldname."_target_id","n.nid","=");
		$results = $query->execute(); 
		$nid = 'not found'; //not found.
		while($record = $results->fetchAssoc()) { $nid=$record['nid']; }
	}
	else {
		$result = db_query("SELECT n.nid FROM {node} n WHERE n.title = :title", array(":title"=> $title));  
		$nid = $result->fetchField();
	}
	if (!is_numeric($nid))	{
		$result = db_query("SELECT n.uid FROM {users} n WHERE n.name = :name", array(":name"=> $title));  
		$nid = $result->fetchField();
		if (!is_numeric($nid))	{
			$nid = -1;
		}
	}	
	return $nid;
}
jrb’s picture

Status: Active » Needs review
FileSize
1.16 KB

The "Using user-supplied value when user does not use the autocomplete" functionality was broken with this commit:

http://drupalcode.org/project/entityreference.git/blobdiff/428f635411e75...

The format of the array returned by getReferencableEntities() was changed. The attached patch fixes the problem for me-- it just puts the array back in the same format that it used to be in.

I'm not sure if getReferencableEntities() can ever return two different types to entities, but my patch would cause a problem if two different entities had the same id.

sammys’s picture

FileSize
1.03 KB

The if statement is unnecessary since the called function always returns an array. Here is a re-rolled patch. #1472596: Free tagging entity reference to a vocabulary does not create the term, only allows existing terms depends on this patch.

jetwodru’s picture

Hi,
Are these included in the latest version ? If not, cumulative patch or just apply the latest patch only ? I'm a bit confused, thanks

jennypanighetti’s picture

Issue summary: View changes

Same question from jet six months ago - is this resolved yet? Is this still an issue?

AdamPS’s picture

This issue is for a completed feature request and its title and category reflect that. I suspect many people have not noticed that a bug is being discussed in the last few comments. I have created a new issue to cover the bug that has been subsequently introduced #2375957: Autocomplete widget allows manual entry, but it fails and can corrupt data

AdamPS’s picture

Status: Needs review » Fixed

To tidy up - I'm marking this as fixed because the original issue was committed years ago, and we have the new related issue I opened to track the subsequent bug.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.