Commit f0489f78 authored by alexpott's avatar alexpott
Browse files

Issue #2643942 by Lendude, no_angel, dawehner: Entityreference autocomplete...

Issue #2643942 by Lendude, no_angel, dawehner: Entityreference autocomplete with search fields uses wrong column name
parent e662dece
......@@ -138,7 +138,8 @@ public function query() {
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_handler = $this->view->field[$field_id];
$field_alias = $this->view->query->addField($field_handler->table, $field_handler->realField);
$field = $this->view->query->fields[$field_alias];
// Add an OR condition for the field.
$conditions->condition($field['table'] . '.' . $field['field'], $value, 'LIKE');
......
<?php
/**
* @file
* Contains \Drupal\views\Tests\Plugin\DisplayEntityReferenceTest.
*/
namespace Drupal\views\Tests\Plugin;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\views\Views;
/**
* Tests the entity reference display plugin.
*
* @group views
*
* @see \Drupal\views\Plugin\views\display\EntityReference
*/
class DisplayEntityReferenceTest extends PluginTestBase {
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = array('test_display_entity_reference');
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('entity_test', 'field', 'views_ui');
/**
* The used field name in the test.
*
* @var string
*/
protected $fieldName;
/**
* The field storage.
*
* @var \Drupal\field\Entity\FieldStorageConfig
*/
protected $fieldStorage;
/**
* The field config.
*
* @var \Drupal\field\Entity\FieldConfig
*/
protected $field;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->drupalLogin($this->drupalCreateUser(array('administer views')));
// Create the text field.
$this->fieldName = 'field_test_entity_ref_display';
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $this->fieldName,
'entity_type' => 'entity_test',
'type' => 'text',
]);
$this->fieldStorage->save();
// Create an instance of the text field on the content type.
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'bundle' => 'entity_test',
]);
$this->field->save();
// Create some entities to search. Add a common string to the name and
// the text field in two entities so we can test that we can search in both.
for ($i = 0; $i < 5; $i++) {
EntityTest::create([
'bundle' => 'entity_test',
'name' => 'name' . $i,
$this->fieldName => 'text',
])->save();
EntityTest::create([
'bundle' => 'entity_test',
'name' => 'name',
$this->fieldName => 'text' . $i,
])->save();
}
}
/**
* Tests the entity reference display plugin.
*/
public function testEntityReferenceDisplay() {
// Add the new field to the fields.
$this->drupalPostForm('admin/structure/views/nojs/add-handler/test_display_entity_reference/default/field', ['name[entity_test__' . $this->fieldName . '.' . $this->fieldName . ']' => TRUE], t('Add and configure fields'));
$this->drupalPostForm(NULL, [], t('Apply'));
// Test that the right fields are shown on the display settings form.
$this->drupalGet('admin/structure/views/nojs/display/test_display_entity_reference/entity_reference_1/style_options');
$this->assertText('Test entity: Name');
$this->assertText('Test entity: ' . $this->field->label());
// Add the new field to the search fields.
$this->drupalPostForm(NULL, ['style_options[search_fields][' . $this->fieldName . ']' => $this->fieldName], t('Apply'));
$this->drupalPostForm(NULL, [], t('Save'));
$view = Views::getView('test_display_entity_reference');
$view->setDisplay('entity_reference_1');
// Add the required settings to test a search operation.
$options = [
'match' => '1',
'match_operator' => 'CONTAINS',
'limit' => 0,
'ids' => NULL,
];
$view->display_handler->setOption('entity_reference_options', $options);
$this->executeView($view);
// Test that we have searched in both fields.
$this->assertEqual(count($view->result), 2, 'Search returned two rows');
}
}
langcode: en
status: true
dependencies: { }
id: test_display_entity_reference
module: views
description: ''
tag: ''
base_table: entity_test
base_field: id
core: '8'
display:
default:
display_options:
access:
type: none
cache:
type: tag
fields:
name:
id: name
table: entity_test
field: name
plugin_id: field
entity_type: entity_test
entity_field: name
style:
type: html_list
display_plugin: default
display_title: Master
id: default
position: 0
entity_reference_1:
display_plugin: entity_reference
id: entity_reference_1
display_title: 'Entity Reference'
position: 1
display_options:
display_extenders: { }
style:
type: entity_reference
options:
search_fields:
name: name
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- 'user.node_grants:view'
tags: { }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment