nodeStorage = $entity_type_manager->getStorage('node'); } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { // Instantiates this form class. return new static( $container->get('entity_type.manager') ); } /** * Handler for autocomplete request. */ public function handleAutocomplete(Request $request) { $results = []; $input = $request->query->get('q'); // Get the typed string from the URL, if it exists. if (!$input) { return new JsonResponse($results); } $input = Xss::filter($input); $query = $this->nodeStorage->getQuery() ->condition('type', 'article') ->condition('title', $input, 'CONTAINS') ->groupBy('nid') ->sort('created', 'DESC') ->range(0, 10); $ids = $query->execute(); $nodes = $ids ? $this->nodeStorage->loadMultiple($ids) : []; foreach ($nodes as $node) { switch ($node->isPublished()) { case TRUE: $availability = '✅'; break; case FALSE: default: $availability = '🚫'; break; } $label = [ $node->getTitle(), '(' . $node->id() . ')', $availability, ]; $results[] = [ 'value' => EntityAutocomplete::getEntityLabels([$node]), 'label' => implode(' ', $label), ]; } return new JsonResponse($results); } }