When creating an autocomplete reference, and using drupal fields as 'search fields' in the settings for "Entity reference list" results in a 500 error on the ajax call.
Steps to reproduce
Add a new plain text field to the user profile: "firstname"
Add a user reference field to the article content type.
Create a views "Entity refrernce display" on the users base table. Add the firstname as a field, and enable it as a 'search field' in the settings for "Entity reference list".
Modify the user reference to use the entity reference view as "Reference method"
Create a new article node, and try to fill in the user reference
The same bug occurs when referencing content instead of users.
Logs
The following error is shown in the logs:
Drupal\Core\Database\DatabaseExceptionWrapper: Exception in User reference[user_reference]: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user__field_firstname.field_firstname' in 'field list': SELECT users_field_data.uid AS uid, users_field_data.name AS users_field_data_name, user__field_firstname.field_firstname AS user__field_firstname_field_firstname FROM {users_field_data} users_field_data LEFT JOIN {user__field_firstname} user__field_firstname ON users_field_data.uid = user__field_firstname.entity_id AND (user__field_firstname.deleted = :views_join_condition_0 AND user__field_firstname.langcode = users_field_data.langcode) WHERE ((( (users_field_data.name LIKE :db_condition_placeholder_2 ESCAPE '\\') OR (user__field_firstname.field_firstname LIKE :db_condition_placeholder_3 ESCAPE '\\') ))AND(( (users_field_data.status <> :db_condition_placeholder_4) ))) LIMIT 10 OFFSET 0; Array ( [:db_condition_placeholder_2] => %jan% [:db_condition_placeholder_3] => %jan% [:db_condition_placeholder_4] => 0 [:views_join_condition_0] => 0 ) in Drupal\views\Plugin\views\query\Sql->execute() (line 1457 of /var/www/eight/core/modules/views/src/Plugin/views/query/Sql.php).
The column that should be used is user__field_firstname_value
Looks like the code in \Drupal\views\Plugin\views\display\EntityReference does not contain the correct column name
// Build the condition using the selected search fields.
foreach ($style_options['options']['search_fields'] as $field_id) {
if (!empty($field_id)) {
// Get the table and field names for the checked field.
$field_alias = $this->view->query->addField($this->view->field[$field_id]->table, $field_id);
$field = $this->view->query->fields[$field_alias];
// Add an OR condition for the field.
$conditions->condition($field['table'] . '.' . $field['field'], $value, 'LIKE');
}
}
| Comment | File | Size | Author |
|---|---|---|---|
| #10 | entityref_autocomplete-2643942-10.patch | 6.52 KB | lendude |
| #10 | interdiff-2643942-8-10.txt | 9.14 KB | lendude |
| #8 | entityref_autocomplete-2643942-8.patch | 10.04 KB | lendude |
| #8 | interdiff-2643942-4-8.txt | 6.23 KB | lendude |
| #4 | entityref_autocomplete-2643942-4.patch | 8.41 KB | lendude |
Comments
Comment #2
stefaan@calibrate.be commentedHaving the same issue.
The problem occurs only when you set the display widget as autocomplete (and autocomplete tags style), not when using check boxes or select list and showing first name instead of user name.
Comment #3
lendudeThis fails because the provided field name is not right. It needs the realField, not the field_id apparently.
Patch fixes the issue with manual testing but this needs tests.
Setting to major because it throws an SQL error and there is no way around that (or any indication that anything is wrong in the UI other then a spinner that keeps spinning).
Comment #4
lendudeFirst stab at some tests.
Needs some more tests for results and the like, but it's a start.
Comment #6
dawehnerI'm sorry but accessing the same handlers twice in a different way in the same line is super confusing. What about using
$field_handler = $this->view->field[$field_id];To be honest I kinda prefer to not leverage node for tests but rather for entity_test, its often helps us to see some edge cases
Comment #7
dawehnerFeel free to disagree with me!
Comment #8
lendudeThanks for looking at this @dawehner!
1. Fixed. Yeah that line was pretty unreadable (plus, I never realised it was accessing the same handler).
2. Agree, will look into converting it. Just easier to export a test view using node :)
Expanded the tests to include actual tests and shuffled the setup code around. Still using node at the moment.
Comment #9
lendudeComment #10
lendudeWell that was easier then I thought...tests, now powered by entity_test!
Comment #11
no_angel commentedComment #12
no_angel commentedMy results for the pre-patch and post-patch. Not sure my errors are exactly the same as yours.
Pre-patch log and error:


After-patch result for creating new article:

Comment #13
stefaan@calibrate.be commentedPatch confirmed working here on php 5.5
Comment #14
jibran@dawehner are you happy with current patch?
Comment #15
no_angel commentedComment #16
alexpottAll of @dawehner's feedback in #6 has been addressed by @Lendude. Nice.
Committed e2861e6 and pushed to 8.0.x and 8.1.x. Thanks!