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

merge-requests/5955/head
Lee Rowlands 2024-01-31 15:20:50 +10:00
parent 8c89656bdd
commit d75377535a
No known key found for this signature in database
GPG Key ID: 2B829A3DF9204DC4
4 changed files with 33 additions and 5 deletions

View File

@ -203,7 +203,8 @@ class EntityAutocomplete extends Textfield {
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'],

View File

@ -77,8 +77,12 @@ class EntityAutocompleteController extends ControllerBase {
*/
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)) : '';

View File

@ -92,6 +92,12 @@ class EntityAutocompleteElementFormTest extends EntityKernelTestBase implements
$entity->save();
$this->referencedEntities[] = $entity;
}
$entity = EntityTest::create([
'name' => '0',
]);
$entity->save();
$this->referencedEntities[] = $entity;
}
/**
@ -184,6 +190,11 @@ class EntityAutocompleteElementFormTest extends EntityKernelTestBase implements
'#tags' => TRUE,
];
$form['single_name_0'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'entity_test',
];
return $form;
}
@ -211,6 +222,7 @@ class EntityAutocompleteElementFormTest extends EntityKernelTestBase implements
'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 @@ class EntityAutocompleteElementFormTest extends EntityKernelTestBase implements
['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'));
}
/**

View File

@ -106,9 +106,11 @@ class EntityAutocompleteTest extends EntityKernelTestBase {
];
$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 @@ class EntityAutocompleteTest extends EntityKernelTestBase {
$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');
}
/**