public function SearchController::search in Gutenberg 8.2
Same name and namespace in other branches
- 8 src/Controller/SearchController.php \Drupal\gutenberg\Controller\SearchController::search()
Return a list of nodes containing a piece of search text.
Used for link auto-completion.
Parameters
\Symfony\Component\HttpFoundation\Request $request: The request.
Return value
\Symfony\Component\HttpFoundation\JsonResponse The JSON response.
Throws
\Drupal\Core\Entity\EntityMalformedException
1 string reference to 'SearchController::search'
File
- src/
Controller/ SearchController.php, line 28
Class
- SearchController
- Controller for handling node entity queries to use as URLs.
Namespace
Drupal\gutenberg\ControllerCode
public function search(Request $request) {
$search = (string) $request->query
->get('search');
$limit = (int) $request->query
->get('per_page', 20);
$query = \Drupal::entityQuery('node');
$query
->condition('title', $search, 'CONTAINS')
->condition('status', 1)
->sort('created', 'DESC')
->range(0, $limit);
$node_ids = $query
->execute();
$nodes = Node::loadMultiple($node_ids);
$result = [];
foreach ($nodes as $node) {
$result[] = [
'id' => $node
->id(),
'title' => $node
->getTitle(),
'type' => $node
->getType(),
'url' => $node
->toUrl('canonical', [
'absolute' => FALSE,
])
->toString(),
];
}
return new JsonResponse($result);
}