public function SearchController::search in Gutenberg 8
Same name and namespace in other branches
- 8.2 src/Controller/SearchController.php \Drupal\gutenberg\Controller\SearchController::search()
Search content.
Parameters
\Symfony\Component\HttpFoundation\Request $request: The request.
Return value
\Symfony\Component\HttpFoundation\JsonResponse The JSON response.
1 string reference to 'SearchController::search'
File
- src/
Controller/ SearchController.php, line 24
Class
- SearchController
- Returns responses for our image routes.
Namespace
Drupal\gutenberg\ControllerCode
public function search(Request $request) {
$search = $request->query
->get('search');
// [{"id":1,"title":"Hello world!","url":"http:\/\/localhost\/wordpress\/2019\/03\/04\/hello-world\/","type":"post","subtype":"post","_links":{"self":[{"embeddable":true,"href":"http:\/\/localhost\/wordpress\/wp-json\/wp\/v2\/posts\/1"}],"about":[{"href":"http:\/\/localhost\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"collection":[{"href":"http:\/\/localhost\/wordpress\/wp-json\/wp\/v2\/search"}]}},{"id":2,"title":"Sample Page","url":"http:\/\/localhost\/wordpress\/sample-page\/","type":"post","subtype":"page","_links":{"self":[{"embeddable":true,"href":"http:\/\/localhost\/wordpress\/wp-json\/wp\/v2\/pages\/2"}],"about":[{"href":"http:\/\/localhost\/wordpress\/wp-json\/wp\/v2\/types\/page"}],"collection":[{"href":"http:\/\/localhost\/wordpress\/wp-json\/wp\/v2\/search"}]}}]
$query = \Drupal::entityQuery('node');
$query
->condition('title', $search, 'CONTAINS');
$query
->condition('status', 1);
$query
->sort('created', 'DESC');
$node_ids = $query
->execute();
$nodes = Node::loadMultiple($node_ids);
$result = [];
foreach ($nodes as $node) {
$result[] = [
'id' => $node
->id(),
'title' => $node
->getTitle(),
'url' => $node
->toUrl('canonical', [
'absolute' => FALSE,
])
->toString(),
];
}
return new JsonResponse($result);
}