Commit f0a40c9c authored by catch's avatar catch
Browse files

Issue #3138631 by Matroskeen, snehalgaikwad, shetpooja04, z.stolar, amateescu,...

Issue #3138631 by Matroskeen, snehalgaikwad, shetpooja04, z.stolar, amateescu, alexpott, Berdir: Improve entity reference validation error messages
parent f43ed87d
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -323,17 +323,18 @@ protected static function matchEntityByTitle(SelectionInterface $handler, $input
    $params = [
      '%value' => $input,
      '@value' => $input,
      '@entity_type_plural' => \Drupal::entityTypeManager()->getDefinition($element['#target_type'])->getPluralLabel(),
    ];
    if (empty($entities)) {
      if ($strict) {
        // Error if there are no entities available for a required field.
        $form_state->setError($element, t('There are no entities matching "%value".', $params));
        $form_state->setError($element, t('There are no @entity_type_plural matching "%value".', $params));
      }
    }
    elseif (count($entities) > 5) {
      $params['@id'] = key($entities);
      // Error if there are more than 5 matching entities.
      $form_state->setError($element, t('Many entities are called %value. Specify the one you want by appending the id in parentheses, like "@value (@id)".', $params));
      $form_state->setError($element, t('Many @entity_type_plural 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.
@@ -342,7 +343,7 @@ protected static function matchEntityByTitle(SelectionInterface $handler, $input
        $multiples[] = $name . ' (' . $id . ')';
      }
      $params['@id'] = $id;
      $form_state->setError($element, t('Multiple entities match this reference; "%multiple". Specify the one you want by appending the id in parentheses, like "@value (@id)".', ['%multiple' => strip_tags(implode('", "', $multiples))] + $params));
      $form_state->setError($element, t('Multiple @entity_type_plural match this reference; "%multiple". Specify the one you want by appending the id in parentheses, like "@value (@id)".', ['%multiple' => strip_tags(implode('", "', $multiples))] + $params));
    }
    else {
      // Take the one and only matching entity.
+2 −2
Original line number Diff line number Diff line
@@ -172,7 +172,7 @@ public function testFieldAdminHandler() {
    $this->drupalPostForm('node/add/' . $this->type, $edit, 'Save');

    // Assert that entity reference autocomplete field is validated.
    $this->assertText('There are no entities matching "Test"');
    $this->assertText('There are no content items matching "Test"');

    $edit = [
      'title[0][value]' => 'Test',
@@ -182,7 +182,7 @@ public function testFieldAdminHandler() {

    // Assert the results multiple times to avoid sorting problem of nodes with
    // the same title.
    $this->assertText('Multiple entities match this reference;');
    $this->assertText('Multiple content items match this reference;');
    $this->assertText($node1->getTitle() . ' (' . $node1->id() . ')');
    $this->assertText($node2->getTitle() . ' (' . $node2->id() . ')');
    $this->assertText('Specify the one you want by appending the id in parentheses, like "' . $node2->getTitle() . ' (' . $node2->id() . ')' . '".');
+1 −1
Original line number Diff line number Diff line
@@ -264,7 +264,7 @@ protected function checkVariousAuthoredByValues(NodeInterface $node, $form_eleme
      $form_element_name => 'invalid-name',
    ];
    $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, 'Save');
    $this->assertRaw(t('There are no entities matching "%name".', ['%name' => 'invalid-name']));
    $this->assertRaw(t('There are no users matching "%name".', ['%name' => 'invalid-name']));

    // Change the authored by field to an empty string, which should assign
    // authorship to the anonymous user (uid 0).
+5 −5
Original line number Diff line number Diff line
@@ -106,7 +106,7 @@ public function testAdminUserInterface() {
      'options[value]' => implode(', ', $users),
    ];
    $this->drupalPostForm($path, $edit, 'Apply');
    $this->assertRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
    $this->assertRaw(t('There are no users matching "%value".', ['%value' => implode(', ', $users)]));

    // Pass in an invalid username and a valid username.
    $random_name = $this->randomMachineName();
@@ -117,7 +117,7 @@ public function testAdminUserInterface() {
    ];
    $users = [$users[0]];
    $this->drupalPostForm($path, $edit, 'Apply');
    $this->assertRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
    $this->assertRaw(t('There are no users matching "%value".', ['%value' => implode(', ', $users)]));

    // Pass in just valid usernames.
    $users = $this->names;
@@ -126,7 +126,7 @@ public function testAdminUserInterface() {
      'options[value]' => implode(', ', $users),
    ];
    $this->drupalPostForm($path, $edit, 'Apply');
    $this->assertNoRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
    $this->assertNoRaw(t('There are no users matching "%value".', ['%value' => implode(', ', $users)]));
  }

  /**
@@ -142,7 +142,7 @@ public function testExposedFilter() {
    $users = array_map('strtolower', $users);
    $options['query']['uid'] = implode(', ', $users);
    $this->drupalGet($path, $options);
    $this->assertRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
    $this->assertRaw(t('There are no users matching "%value".', ['%value' => implode(', ', $users)]));

    // Pass in an invalid target_id in for the entity_autocomplete value format.
    // There should be no errors, but all results should be returned as the
@@ -162,7 +162,7 @@ public function testExposedFilter() {
    $users = [$users[0]];

    $this->drupalGet($path, $options);
    $this->assertRaw(t('There are no entities matching "%value".', ['%value' => implode(', ', $users)]));
    $this->assertRaw(t('There are no users matching "%value".', ['%value' => implode(', ', $users)]));

    // Pass in just valid usernames.
    $users = $this->names;
+2 −2
Original line number Diff line number Diff line
@@ -279,7 +279,7 @@ public function testInvalidEntityAutocompleteElement() {
      ]);
    $form_builder->submitForm($this, $form_state);
    $this->assertCount(1, $form_state->getErrors());
    $this->assertEqual($form_state->getErrors()['single'], t('There are no entities matching "%value".', ['%value' => 'single - non-existent label']));
    $this->assertEqual($form_state->getErrors()['single'], t('There are no test entity entities matching "%value".', ['%value' => 'single - non-existent label']));

    // Test 'single' with an entity ID that doesn't exist.
    $form_state = (new FormState())
@@ -302,7 +302,7 @@ public function testInvalidEntityAutocompleteElement() {
    // The element without 'autocreate' support still has to emit a warning when
    // the input doesn't end with an entity ID enclosed in parentheses.
    $this->assertCount(1, $form_state->getErrors());
    $this->assertEqual($form_state->getErrors()['single_no_validate'], t('There are no entities matching "%value".', ['%value' => 'single - non-existent label']));
    $this->assertEqual($form_state->getErrors()['single_no_validate'], t('There are no test entity entities matching "%value".', ['%value' => 'single - non-existent label']));

    $form_state = (new FormState())
      ->setValues([