diff --git a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php index cb8c7d6..8874c22 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php +++ b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php @@ -41,6 +41,8 @@ */ class SelectionBase extends PluginBase implements SelectionInterface, ContainerFactoryPluginInterface { + use SelectionValidateTrait; + /** * The entity manager. * @@ -272,39 +274,7 @@ public function validateReferenceableEntities(array $ids) { * {@inheritdoc} */ 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) { - $entities += $entities_list; - } - $params = array( - '%value' => $input, - '@value' => $input, - ); - if (empty($entities)) { - if ($strict) { - // Error if there are no entities available for a required field. - $form_state->setError($element, $this->t('There are no entities matching "%value".', $params)); - } - } - elseif (count($entities) > 5) { - $params['@id'] = key($entities); - // Error if there are more than 5 matching entities. - $form_state->setError($element, $this->t('Many entities are called %value. Specify the one you want by appending the id in parentheses, like "@value (@id)".', $params)); - } - elseif (count($entities) > 1) { - // More helpful error if there are only a few matching entities. - $multiples = array(); - foreach ($entities as $id => $name) { - $multiples[] = $name . ' (' . $id . ')'; - } - $params['@id'] = $id; - $form_state->setError($element, $this->t('Multiple entities match this reference; "%multiple". Specify the one you want by appending the id in parentheses, like "@value (@id)".', array('%multiple' => implode('", "', $multiples)))); - } - else { - // Take the one and only matching entity. - return key($entities); - } + return $this->validateAutocomplete($input, $element, $form_state, $form, $strict); } /** diff --git a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionValidateTrait.php b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionValidateTrait.php new file mode 100644 index 0000000..276e8fb --- /dev/null +++ b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionValidateTrait.php @@ -0,0 +1,73 @@ +getReferenceableEntities($input, '=', 6); + $entities = array(); + foreach ($bundled_entities as $entities_list) { + $entities += $entities_list; + } + $params = array( + '%value' => $input, + '@value' => $input, + ); + if (empty($entities)) { + if ($strict) { + // Error if there are no entities available for a required field. + $form_state->setError($element, $this->t('There are no entities matching "%value".', $params)); + } + } + elseif (count($entities) > 5) { + $params['@id'] = key($entities); + // Error if there are more than 5 matching entities. + $form_state->setError($element, $this->t('Many entities are called %value. Specify the one you want by appending the id in parentheses, like "@value (@id)".', $params)); + } + elseif (count($entities) > 1) { + // More helpful error if there are only a few matching entities. + $multiples = array(); + foreach ($entities as $id => $name) { + $multiples[] = $name . ' (' . $id . ')'; + } + $params['@id'] = $id; + $form_state->setError($element, $this->t('Multiple entities match this reference; "%multiple". Specify the one you want by appending the id in parentheses, like "@value (@id)".', array('%multiple' => implode('", "', $multiples)))); + } + else { + // Take the one and only matching entity. + return key($entities); + } + } + +} diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php index eb8b85f..a4bbec0 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php @@ -9,6 +9,7 @@ use Drupal\Core\Entity\Entity; use Drupal\field_ui\Tests\FieldUiTestTrait; +use Drupal\node\Entity\Node; use Drupal\simpletest\WebTestBase; use Drupal\taxonomy\Entity\Vocabulary; @@ -248,21 +249,39 @@ public function testFieldAdminHandler() { $this->drupalPostForm(NULL, $edit, t('Save settings')); // Create a node. - $edit = array( - 'title[0][value]' => 'Foo Node', - ); - $this->drupalPostForm('node/add/' . $this->type, $edit, t('Save')); + $node = Node::create([ + 'type' => $this->type, + 'title' => 'Foo Node', + ]); + $node->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('Foo'); + $this->assertRaw($node->getTitle() . ' (' . $node->id() . ')'); + + $edit = array( + 'title[0][value]' => 'Example', + 'field_test_entity_ref_field[0][target_id]' => 'Test' + ); + $this->drupalPostForm('node/add/' . $this->type, $edit, t('Save')); + + // Assert that entity reference autocomplete field is validated. + $this->assertText(t('1 error has been found: Test Entity Reference Field'), 'Node save failed when required entity reference field was not correctly filled.'); + $this->assertText(t('There are no entities matching "@entity"', ['@entity' => 'Test'])); + + $edit = array( + 'title[0][value]' => 'Test', + 'field_test_entity_ref_field[0][target_id]' => $node->getTitle() + ); + $this->drupalPostForm('node/add/' . $this->type, $edit, t('Save')); + $this->assertLink($node->getTitle()); } /** - * Tests the formatters for the Entity References + * Tests the formatters for the Entity References. */ public function testAvailableFormatters() { // Create a new vocabulary. diff --git a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php index 782544c..acb2be0 100644 --- a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php +++ b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php @@ -9,6 +9,7 @@ use Drupal\Core\Database\Query\SelectInterface; use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionValidateTrait; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface; @@ -31,6 +32,8 @@ */ class ViewsSelection extends PluginBase implements SelectionInterface, ContainerFactoryPluginInterface { + use SelectionValidateTrait; + /** * The entity manager. * @@ -263,39 +266,7 @@ public function validateReferenceableEntities(array $ids) { * {@inheritdoc} */ 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) { - $entities += $entities_list; - } - $params = array( - '%value' => $input, - '@value' => $input, - ); - if (empty($entities)) { - if ($strict) { - // Error if there are no entities available for a required field. - $form_state->setError($element, $this->t('There are no entities matching "%value".', $params)); - } - } - elseif (count($entities) > 5) { - $params['@id'] = key($entities); - // Error if there are more than 5 matching entities. - $form_state->setError($element, $this->t('Many entities are called %value. Specify the one you want by appending the id in parentheses, like "@value (@id)".', $params)); - } - elseif (count($entities) > 1) { - // More helpful error if there are only a few matching entities. - $multiples = array(); - foreach ($entities as $id => $name) { - $multiples[] = $name . ' (' . $id . ')'; - } - $params['@id'] = $id; - $form_state->setError($element, $this->t('Multiple entities match this reference; "%multiple". Specify the one you want by appending the id in parentheses, like "@value (@id)".', array('%multiple' => implode('", "', $multiples)))); - } - else { - // Take the one and only matching entity. - return key($entities); - } + return $this->validateAutocomplete($input, $element, $form_state, $form, $strict); } /**