You are here

public function BynderTagSearchService::searchTags in Bynder 8.3

Same name and namespace in other branches
  1. 8 src/Controller/BynderTagSearchService.php \Drupal\bynder\Controller\BynderTagSearchService::searchTags()
  2. 8.2 src/Controller/BynderTagSearchService.php \Drupal\bynder\Controller\BynderTagSearchService::searchTags()
  3. 4.0.x src/Controller/BynderTagSearchService.php \Drupal\bynder\Controller\BynderTagSearchService::searchTags()

Route callback for tags search.

1 string reference to 'BynderTagSearchService::searchTags'
bynder.routing.yml in ./bynder.routing.yml
bynder.routing.yml

File

src/Controller/BynderTagSearchService.php, line 63

Class

BynderTagSearchService
Class BynderTagSearchService.

Namespace

Drupal\bynder\Controller

Code

public function searchTags(Request $request) {
  $results = [];
  $keyword = $request
    ->get('term');
  try {
    $results = array_map(function ($tag) {
      return [
        'id' => $tag['tag'],
        'text' => $tag['tag'],
      ];
    }, $this->bynder
      ->getTags([
      'limit' => self::TAG_LIST_LIMIT,
      'keyword' => $keyword,
      'minCount' => 1,
    ]));
  } catch (\Exception $e) {
    (new TagSearchException($e
      ->getMessage()))
      ->logException()
      ->displayMessage();
    return FALSE;
  }
  usort($results, function ($first, $second) {
    return $first['text'] < $second['text'] ? -1 : 1;
  });
  $response['results'] = $results;
  return new JsonResponse($response);
}