diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteTagsWidget.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteTagsWidget.php index 6d46c2f..20cb005 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteTagsWidget.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteTagsWidget.php @@ -24,7 +24,7 @@ * settings = { * "match_operator" = "CONTAINS", * "size" = 60, - * "autocomplete_path" = "entity_reference/autocomplete/tags", + * "autocomplete_type" = "tags", * "placeholder" = "" * }, * multiple_values = TRUE diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php index fb2b575..ef17efa 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php @@ -30,7 +30,7 @@ * settings = { * "match_operator" = "CONTAINS", * "size" = 60, - * "autocomplete_path" = "entity_reference/autocomplete/single", + * "autocomplete_type" = "single", * "placeholder" = "" * } * ) diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php index 3587046..fbe5714 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php @@ -74,23 +74,24 @@ public function formElement(FieldInterface $items, $delta, array $element, $lang global $user; $entity = $element['#entity']; - // Prepare the autocomplete path. - $autocomplete_path = $this->getSetting('autocomplete_path'); - $autocomplete_path .= '/' . $this->fieldDefinition->getFieldName() . '/' . $entity->entityType() . '/' . $entity->bundle() . '/'; - - // Use 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')), diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutoCreateTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutoCreateTest.php index 09fec90..f07a6ee 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutoCreateTest.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutoCreateTest.php @@ -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.