Verified Commit 18732679 authored by Jess's avatar Jess
Browse files

Issue #3383131 by WalkingDexter, xjm, allisonherodevs, ashley_herodev,...

Issue #3383131 by WalkingDexter, xjm, allisonherodevs, ashley_herodev, pradhumanjain2311, smustgrave, marcoliver, lauriii: Entity autocomplete form element ignores entities with label "0"

(cherry picked from commit 68dfec3c)
parent a9323c4f
Loading
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -203,7 +203,8 @@ public static function processEntityAutocomplete(array &$element, FormStateInter
  public static function validateEntityAutocomplete(array &$element, FormStateInterface $form_state, array &$complete_form) {
    $value = NULL;

    if (!empty($element['#value'])) {
    // Check the value for emptiness, but allow the use of (string) "0".
    if ((!empty($element['#value']) || (is_string($element['#value']) && strlen($element['#value'])))) {
      $options = $element['#selection_settings'] + [
        'target_type' => $element['#target_type'],
        'handler' => $element['#selection_handler'],
+5 −1
Original line number Diff line number Diff line
@@ -77,8 +77,12 @@ public static function create(ContainerInterface $container) {
   */
  public function handleAutocomplete(Request $request, $target_type, $selection_handler, $selection_settings_key) {
    $matches = [];

    // Get the typed string from the URL, if it exists.
    if ($input = $request->query->get('q')) {
    $input = $request->query->get('q');

    // Check this string for emptiness, but allow any non-empty string.
    if (is_string($input) && strlen($input)) {
      $tag_list = Tags::explode($input);
      $typed_string = !empty($tag_list) ? mb_strtolower(array_pop($tag_list)) : '';

+15 −0
Original line number Diff line number Diff line
@@ -92,6 +92,12 @@ protected function setUp(): void {
      $entity->save();
      $this->referencedEntities[] = $entity;
    }

    $entity = EntityTest::create([
      'name' => '0',
    ]);
    $entity->save();
    $this->referencedEntities[] = $entity;
  }

  /**
@@ -184,6 +190,11 @@ public function buildForm(array $form, FormStateInterface $form_state) {
      '#tags' => TRUE,
    ];

    $form['single_name_0'] = [
      '#type' => 'entity_autocomplete',
      '#target_type' => 'entity_test',
    ];

    return $form;
  }

@@ -211,6 +222,7 @@ public function testValidEntityAutocompleteElement() {
        'tags_autocreate_specific_uid' => $this->getAutocompleteInput($this->referencedEntities[0]) . ', tags - autocreated entity label with specific uid, ' . $this->getAutocompleteInput($this->referencedEntities[1]),
        'single_string_id' => $this->getAutocompleteInput($this->referencedEntities[2]),
        'tags_string_id' => $this->getAutocompleteInput($this->referencedEntities[2]) . ', ' . $this->getAutocompleteInput($this->referencedEntities[3]),
        'single_name_0' => $this->referencedEntities[4]->label(),
      ]);
    $form_builder = $this->container->get('form_builder');
    $form_builder->submitForm($this, $form_state);
@@ -271,6 +283,9 @@ public function testValidEntityAutocompleteElement() {
      ['target_id' => $this->referencedEntities[3]->id()],
    ];
    $this->assertEquals($expected, $form_state->getValue('tags_string_id'));

    // Test the 'single_name_0' element.
    $this->assertEquals($this->referencedEntities[4]->id(), $form_state->getValue('single_name_0'));
  }

  /**
+11 −3
Original line number Diff line number Diff line
@@ -106,9 +106,11 @@ public function testEntityReferenceAutocompletion() {
    ];
    $this->assertSame($target, reset($data), 'Autocomplete returns an entity label containing a comma and a slash.');

    $input = '';
    // Test empty input.
    foreach (['', NULL, FALSE, 0, 0.0] as $input) {
      $data = $this->getAutocompleteResult($input);
    $this->assertSame([], $data, 'Autocomplete of empty string returns empty result');
      $this->assertSame([], $data, 'Autocomplete of empty input returns empty result');
    }

    $input = ',';
    $data = $this->getAutocompleteResult($input);
@@ -130,6 +132,12 @@ public function testEntityReferenceAutocompletion() {
    $this->assertSame(Html::escape($entity_1->name->value), $data[0]['label'], 'Autocomplete returned the first matching entity');
    $this->assertSame(Html::escape($entity_2->name->value), $data[1]['label'], 'Autocomplete returned the second matching entity');
    $this->assertSame(Html::escape($entity_3->name->value), $data[2]['label'], 'Autocomplete returned the third matching entity');

    // Try to autocomplete an entity label with the '0' character.
    $input = '0';
    $data = $this->getAutocompleteResult($input);
    $this->assertSame(Html::escape($entity_1->name->value), $data[0]['label'], 'Autocomplete returned the first matching entity');
    $this->assertSame(Html::escape($entity_2->name->value), $data[1]['label'], 'Autocomplete returned the second matching entity');
  }

  /**