Commit 75b8c067 authored by alexpott's avatar alexpott

Issue #2084081 by amateescu: Fixed ER autocomplete doesn't work.

parent 34f0d6db
......@@ -24,7 +24,7 @@
* settings = {
* "match_operator" = "CONTAINS",
* "size" = 60,
* "autocomplete_path" = "entity_reference/autocomplete/tags",
* "autocomplete_type" = "tags",
* "placeholder" = ""
* },
* multiple_values = TRUE
......
......@@ -30,7 +30,7 @@
* settings = {
* "match_operator" = "CONTAINS",
* "size" = 60,
* "autocomplete_path" = "entity_reference/autocomplete/single",
* "autocomplete_type" = "single",
* "placeholder" = ""
* }
* )
......
......@@ -75,23 +75,24 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
$entity = $items->getEntity();
// Prepare the autocomplete path.
$autocomplete_path = $this->getSetting('autocomplete_path');
$autocomplete_path .= '/' . $this->fieldDefinition->getFieldName() . '/' . $entity->entityType() . '/' . $entity->bundle() . '/';
// Use <NULL> as a placeholder in the URL when we don't have an entity.
// Most web servers collapse two consecutive slashes.
$id = 'NULL';
if ($entity && $entity_id = $entity->id()) {
$id = $entity_id;
// Prepare the autocomplete route parameters.
$autocomplete_route_parameters = array(
'type' => $this->getSetting('autocomplete_type'),
'field_name' => $this->fieldDefinition->getFieldName(),
'entity_type' => $entity->entityType(),
'bundle_name' => $entity->bundle(),
);
if ($entity_id = $entity->id()) {
$autocomplete_route_parameters['entity_id'] = $entity_id;
}
$autocomplete_path .= $id;
$element += array(
'#type' => 'textfield',
'#maxlength' => 1024,
'#default_value' => implode(', ', $this->getLabels($items)),
'#autocomplete_path' => $autocomplete_path,
'#autocomplete_route_name' => 'entity_reference.autocomplete',
'#autocomplete_route_parameters' => $autocomplete_route_parameters,
'#size' => $this->getSetting('size'),
'#placeholder' => $this->getSetting('placeholder'),
'#element_validate' => array(array($this, 'elementValidate')),
......
......@@ -17,7 +17,7 @@ class EntityReferenceAutoCreateTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Entity Reference auto-create',
'name' => 'Entity Reference auto-create and autocomplete UI',
'description' => 'Tests creating new entity (e.g. taxonomy-term) from an autocomplete widget.',
'group' => 'Entity Reference',
);
......@@ -76,12 +76,16 @@ function setUp() {
}
/**
* Assert creation on a new entity.
* Tests that the autocomplete input element appears and the creation of a new
* entity.
*/
public function testAutoCreate() {
$user1 = $this->drupalCreateUser(array('access content', "create $this->referencing_type content"));
$this->drupalLogin($user1);
$this->drupalGet('node/add/' . $this->referencing_type);
$this->assertFieldByXPath('//input[@id="edit-test-field-0-target-id" and contains(@class, "form-autocomplete")]', NULL, 'The autocomplete input element appears.');
$new_title = $this->randomName();
// Assert referenced node does not exist.
......
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