Problem/Motivation

Apply the patch 15 from #2472421: Entity reference, views filter reference method is broken., do all the steps mentioned in "What are the steps required to reproduce the bug?" and:
8. Select reference method "Views: Filter...", for the "View used to select the entities" select created view and save the field settings.
9. Create a new node and try to fill autocomplete field. There will be no suggestions.
If we follow the URL used by entity reference field we'll get an error: Undefined index: unknown in Drupal\entity_reference\Plugin\views\display\EntityReference->query() (line 143 of core/modules/entity_reference/src/Plugin/views/display/EntityReference.php).

From #2521738: Using Views as Reference method for Entity reference field in a content type is not working:

Steps to Reproduce:
1.Create a user Entity Reference view.With the below options

  • Format: Entity Reference list | settings : Search fields: user:name (select)
  • Show:Entity Reference inline fields
  • Fields: User: Name

Leave the rest field to deafult options.

2.Add User reference field to any of the content types.
3.Select Reference Method as Views: Filter by an entity reference view.
4.Select above created Entity Reference View and save the field.
5.Now try to create the content by typing in the autocomplete field.
6.The ajax request is not responding as the response to the ajax call is database error.

Expected Behavior:
1.Auto complete field should fetch the view results as per the keys entered in the field.

Reason for the issues:
From the database log i can guess field name is missing in the query at "LIKE" where condition.

Drupal\Core\Database\DatabaseExceptionWrapper: Exception in user names[user_names]: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIKE '%a%' ESCAPE '\\') ))AND(( (users_field_data.status <> '0') ))) LIMIT 10 OF' at line 1: SELECT users_field_data.uid AS uid FROM {users_field_data} users_field_data WHERE ((( (. LIKE :db_condition_placeholder_0 ESCAPE '\\') ))AND(( (users_field_data.status <> :db_condition_placeholder_1) ))) LIMIT 10 OFFSET 0; Array ( [:db_condition_placeholder_0] => %a% [:db_condition_placeholder_1] => 0 ) in Drupal\views\Plugin\views\query\Sql->execute() (line 1457 of /var/www/html/drupal-8012/core/modules/views/src/Plugin/views/query/Sql.php).

Drupal Version:drupal-8.0.0-beta12
PHP Version:PHP 5.5.9
Operation System: Ubuntu 14.04
Web Server: Apache/2.4.7

Proposed resolution

Remaining tasks

User interface changes

API changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mbovan’s picture

Status: Active » Needs review
FileSize
1005 bytes

Here is the first patch. Added calling for addField() to get non-existing field alias.

Berdir’s picture

Issue tags: +Needs tests

And.. this will need a test too.

dawehner’s picture

+++ b/core/modules/entity_reference/src/Plugin/views/display/EntityReference.php
@@ -140,7 +140,8 @@ public function query() {
       foreach ($style_options['options']['search_fields'] as $field_alias) {
...
+          $field_alias = $this->view->query->addField($this->view->field[$field_alias]->table, $field_alias);
+          $field = $this->view->query->fields[$field_alias];

well $field_alias then in the foreach loop seems to be the wrong variable name. What about using field_id ?

mbovan’s picture

Changed foreach variable from $field_alias to $field_id. After #2472421: Entity reference, views filter reference method is broken. is committed, I'll extend the tests from that issue.

mbovan’s picture

Provided tests for the missing feature. Tried to extend previous tests to use entity_test view and to create entity_test entities, but get some (strange) errors (like 'user_role' is missing when I try to create an entity_test entity), so I created this huge test by creating views and fields manually through UI. :)

Also, made the changes in views/style/EntityReference class, as I get errors when I try to open a Settings tab in order to set Entity Reference View.
The problem was that #default_value is set to NULL, and in the Checkboxes class we try to change it using += operation. By PHP array docs it will not work in case: "The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored."

Status: Needs review » Needs work

The last submitted patch, 5: views_entity_reference-2482625-5-TEST-ONLY.patch, failed testing.

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 5: views_entity_reference-2482625-5-TEST-ONLY.patch, failed testing.

jibran’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests +VDC

It has tests now so removing the tag.

holist’s picture

#5 works on beta11.

Status: Needs review » Needs work

The last submitted patch, 5: views_entity_reference-2482625-5-TEST-ONLY.patch, failed testing.

mbovan’s picture

Status: Needs work » Needs review

I can confirm that I still get an error and failing tests (locally).

On the autocomplete path I get "Internal Server Error" and error message:
"A fatal error occurred: Exception in view_name[view_name]: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax"

PHP 5.5.25 (cli)
MySQL 5.6.24
Latest Drupal 8.0.x core.

mbovan’s picture

Uploading a new test-only patch with debug() line to see what the page on the autocomplete path returns...

Status: Needs review » Needs work

The last submitted patch, 16: views_entity_reference-2482625-16-TEST-ONLY.patch, failed testing.

xjm’s picture

Closed #2521738: Using Views as Reference method for Entity reference field in a content type is not working as a duplicate of this issue. Edit: now with the correct node.

xjm’s picture

Issue summary: View changes

Adding the detailed information from the other issue's summary by @gopagoninarsing (feel free to update as needed).

The last submitted patch, 16: views_entity_reference-2482625-16-TEST-ONLY.patch, failed testing.

Berdir’s picture

As discussed last week, the problem with the test is likely that you don't parse it correctly, so it looks like this: /checkout/checkout/..

Look at clickLink() and similar methods to check how they parse the URL and use it.

mbovan’s picture

The last submitted patch, 23: views_entity_reference-2482625-23-TEST-ONLY.patch, failed testing.

Berdir’s picture

Assigned: Unassigned » amateescu

Nice, now it failed as expected.

Assigning to the entity reference maintainer for review :)

amateescu’s picture

Assigned: amateescu » Unassigned
Status: Needs review » Reviewed & tested by the community

Looks great to me! It's nice that we finally have some test coverage for the views integration :)

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 5db1865 and pushed to 8.0.x. Thanks!

diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php
index 03daf1f..eb8b85f 100644
--- a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php
+++ b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php
@@ -8,7 +8,6 @@
 namespace Drupal\entity_reference\Tests;
 
 use Drupal\Core\Entity\Entity;
-use Drupal\entity_test\Entity\EntityTest;
 use Drupal\field_ui\Tests\FieldUiTestTrait;
 use Drupal\simpletest\WebTestBase;
 use Drupal\taxonomy\Entity\Vocabulary;

Unsed use statement removed on commit.

  • alexpott committed 5db1865 on 8.0.x
    Issue #2482625 by mbovan, Berdir, dawehner: Views entity reference...

Status: Fixed » Closed (fixed)

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