ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
with_autocomplete_endpoint.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25
41function with_autocomplete_endpoint()
42{
44 global $DIC;
45 $ui = $DIC->ui()->factory();
46 $renderer = $DIC->ui()->renderer();
47 $refinery = $DIC->refinery();
48 $http = $DIC->http();
49
50 $df = new \ILIAS\Data\Factory();
51
52 [$url_builder, $term_token] = (new URLBuilder($df->uri($http->request()->getUri()->__toString())))
53 ->acquireParameter(['examples'], 'term');
54
55 $search_term = $http->wrapper()->query()->retrieve(
56 $term_token->getName(),
57 $refinery->byTrying([
58 $refinery->kindlyTo()->string(),
59 $refinery->always('')
60 ])
61 );
62
63 if ($search_term !== '') {
64 $response = json_encode(
65 array_reduce(
66 ['Interesting', 'Boring', 'Animating', 'Repetitious'],
67 static function (array $c, string $v) use ($refinery, $search_term): array {
68 if (stristr($v, $search_term)) {
69 $c[] = [
70 'value' => urlencode($refinery->encode()->htmlSpecialCharsAsEntities()->transform($v)),
71 'display' => $v,
72 'searchBy' => $v
73 ];
74 }
75 return $c;
76 },
77 []
78 )
79 );
80 $http->saveResponse(
81 $http->response()->withBody(
83 )
84 );
85 $http->sendResponse();
86 $http->close();
87 }
88
89 $tag_input = $ui->input()->field()->tag(
90 "Tag Input with Autocomplete",
91 []
92 )->withAsyncAutocomplete(
93 $url_builder,
94 $term_token
95 )->withUserCreatedTagsAllowed(false);
96
97 return $renderer->render(
98 $ui->input()->container()->form()->standard("#", [$tag_input])
99 );
100}
$renderer
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:41
$http
Definition: deliver.php:30
$c
Definition: deliver.php:25
global $DIC
Definition: shib_login.php:26
$response
Definition: xapitoken.php:93