diff --git a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php index 8874c22..c9debba 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php +++ b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php @@ -271,13 +271,6 @@ public function validateReferenceableEntities(array $ids) { } /** - * {@inheritdoc} - */ - public function validateAutocompleteInput($input, &$element, FormStateInterface $form_state, $form, $strict = TRUE) { - return $this->validateAutocomplete($input, $element, $form_state, $form, $strict); - } - - /** * Builds an EntityQuery to get referenceable entities. * * @param string|null $match diff --git a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionValidateTrait.php b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionValidateTrait.php index 276e8fb..b77392a 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionValidateTrait.php +++ b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionValidateTrait.php @@ -15,26 +15,9 @@ trait SelectionValidateTrait { /** - * Validates input from an autocomplete widget that has no ID. - * - * @param string $input - * Single string from autocomplete widget. - * @param array $element - * The form element to set a form error. - * @param \Drupal\Core\Form\FormStateInterface $form_state - * The current form state. - * @param array $form - * The form. - * @param bool $strict - * Whether to trigger a form error if an element from $input (eg. an entity) - * is not found. Defaults to TRUE. - * - * @return integer|null - * Value of a matching entity ID, or NULL if none. - * - * @see \Drupal\entity_reference\Plugin\Field\FieldWidget::elementValidate() + * Implements \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface::validateAutocompleteInput(). */ - protected function validateAutocomplete($input, array &$element, FormStateInterface $form_state, array $form, $strict = TRUE) { + public function validateAutocompleteInput($input, &$element, FormStateInterface $form_state, $form, $strict = TRUE) { $bundled_entities = $this->getReferenceableEntities($input, '=', 6); $entities = array(); foreach ($bundled_entities as $entities_list) { diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php index a4bbec0..0956896 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php @@ -248,19 +248,25 @@ public function testFieldAdminHandler() { ); $this->drupalPostForm(NULL, $edit, t('Save settings')); - // Create a node. - $node = Node::create([ + // Create nodes. + $node1 = Node::create([ 'type' => $this->type, 'title' => 'Foo Node', ]); - $node->save(); + $node1->save(); + $node2 = Node::create([ + 'type' => $this->type, + 'title' => 'Foo Node', + ]); + $node2->save(); // Try to add a new node and fill the entity reference field. $this->drupalGet('node/add/' . $this->type); $result = $this->xpath('//input[@name="field_test_entity_ref_field[0][target_id]" and contains(@data-autocomplete-path, "/entity_reference_autocomplete/node/views/")]'); $target_url = $this->getAbsoluteUrl($result[0]['data-autocomplete-path']); $this->drupalGet($target_url, array('query' => array('q' => 'Foo'))); - $this->assertRaw($node->getTitle() . ' (' . $node->id() . ')'); + $this->assertRaw($node1->getTitle() . ' (' . $node1->id() . ')'); + $this->assertRaw($node2->getTitle() . ' (' . $node2->id() . ')'); $edit = array( 'title[0][value]' => 'Example', @@ -274,10 +280,23 @@ public function testFieldAdminHandler() { $edit = array( 'title[0][value]' => 'Test', - 'field_test_entity_ref_field[0][target_id]' => $node->getTitle() + 'field_test_entity_ref_field[0][target_id]' => $node1->getTitle() + ); + $this->drupalPostForm('node/add/' . $this->type, $edit, t('Save')); + + // Assert the results multiple times to avoid sorting problem of nodes with + // the same title. + $this->assertText(t('1 error has been found: Test Entity Reference Field')); + $this->assertText(t('Multiple entities match this reference;')); + $this->assertText(t("@node1", ['@node1' => $node1->getTitle() . ' (' . $node1->id() . ')'])); + $this->assertText(t("@node2", ['@node2' => $node2->getTitle() . ' (' . $node2->id() . ')'])); + + $edit = array( + 'title[0][value]' => 'Test', + 'field_test_entity_ref_field[0][target_id]' => $node1->getTitle() . '(' . $node1->id() . ')' ); $this->drupalPostForm('node/add/' . $this->type, $edit, t('Save')); - $this->assertLink($node->getTitle()); + $this->assertLink($node1->getTitle()); } /** diff --git a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php index acb2be0..47a946c 100644 --- a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php +++ b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php @@ -265,13 +265,6 @@ public function validateReferenceableEntities(array $ids) { /** * {@inheritdoc} */ - public function validateAutocompleteInput($input, &$element, FormStateInterface $form_state, $form, $strict = TRUE) { - return $this->validateAutocomplete($input, $element, $form_state, $form, $strict); - } - - /** - * {@inheritdoc} - */ public function entityQueryAlter(SelectInterface $query) {} /**