function EntityAutocompleteTest::getAutocompleteResult

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityAutocompleteTest.php \Drupal\KernelTests\Core\Entity\EntityAutocompleteTest::getAutocompleteResult()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityAutocompleteTest.php \Drupal\KernelTests\Core\Entity\EntityAutocompleteTest::getAutocompleteResult()
  3. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityAutocompleteTest.php \Drupal\KernelTests\Core\Entity\EntityAutocompleteTest::getAutocompleteResult()

Returns the result of an Entity reference autocomplete request.

Parameters

string $input: The label of the entity to query by.

int $entity_id: The label of the entity to query by.

Return value

mixed The JSON value encoded in its appropriate PHP type.

1 call to EntityAutocompleteTest::getAutocompleteResult()
EntityAutocompleteTest::testEntityReferenceAutocompletion in core/tests/Drupal/KernelTests/Core/Entity/EntityAutocompleteTest.php
Tests autocompletion edge cases with slashes in the names.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityAutocompleteTest.php, line 192

Class

EntityAutocompleteTest
Tests the autocomplete functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function getAutocompleteResult($input, $entity_id = NULL) {
    // Use "entity_test_all_except_host" EntityReferenceSelection
    // to also test passing an entity to autocomplete requests.
    $request = Request::create('entity_reference_autocomplete/' . $this->entityType . '/entity_test_all_except_host');
    $request->query
        ->set('q', $input);
    $selection_settings = [];
    if ($entity_id) {
        $request->query
            ->set('entity_type', $this->entityType);
        $request->query
            ->set('entity_id', $entity_id);
    }
    $selection_settings_key = Crypt::hmacBase64(serialize($selection_settings) . $this->entityType . 'entity_test_all_except_host', Settings::getHashSalt());
    \Drupal::keyValue('entity_autocomplete')->set($selection_settings_key, $selection_settings);
    $entity_reference_controller = EntityAutocompleteController::create($this->container);
    $result = $entity_reference_controller->handleAutocomplete($request, $this->entityType, 'entity_test_all_except_host', $selection_settings_key)
        ->getContent();
    return Json::decode($result);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.